python picture read, add, etc.

一 opencv

Copy the code
import cv2 as cv
# load 
img = cv.imread(imagepath)
# shape=(height, width, channel)
h,w,c = img.shape
# show
cv.imshow('window_title', img)
# save
cv.imwrite(savepath, img)
Copy the code

二 matplotlib

Copy the code
import matplotlib.pyplot as plt # plt for displaying an image 
import matplotlib.image as mpimg # mpimg for reading image 
Import numpy NP AS 

# Load 
img = mpimg.imread ( 'dog.jpg') 
# this time was already img a np.array, and it can be disposable 
# height, width, Channel = (360, 480,. 3) 
H, W, C = img.shape 

# Show 
plt.imshow (IMG) # display picture 
plt.axis ( 'off') # coordinate axes are not displayed 
plt.show () 

# save 
# applicable to any image stored matplotlib drawn, corresponding to a ScreenCapture 
plt.savefig ( 'fig_dog.png')
Copy the code

Note:

(1) plt.imshow (image) of the image data type allows type np.array

         imshow () function format: matplotlib.pyplot.imshow(X-, CMap = None)

         X: image or array to be drawn.

         cmap: Color Atlas (colormap), plotted as a default RGB (A) color space.

         Other optional color map list below:

Color maps description
autumn Red - Orange - Yellow
bone Black - white, x-ray
cool Green - Magenta
copper Black - Copper
flag Red - White - Blue - Black
gray Black - White
hot Black - red - yellow - white
hsv hsv color space, the red - yellow - green - blue - blue - Magenta - Red
inferno Black - red - yellow
jet Blue - green - yellow - red
magma Black - red - white
pink Black - Powder - White
plasma Green - red - yellow
prism  Red - yellow - green - blue - purple -...- green mode
spring Magenta - Yellow
summer Green - yellow
viridis Blue - green - yellow
winter Blue - Green

Are used more gray, jet, such as:

plt.imshow(image,plt.cm.gray)

plt.imshow(img,cmap=plt.cm.jet)

(2) mpimg provides both load image function imread, but also provides the function to save the imageimsave

(. 3)  plt.savefig function may be displayed in the image saved in fig

 Extension: display image scaling

Misc SciPy Import from 
lena_new_sz = misc.imresize (IMG, 0.5) # If the second parameter is an integer, or percentage, if a tuple, compared with the size of the output image 
plt.imshow (img_new_sz) 
plt.axis ( 'OFF' ) 
plt.show ()

Three PIL

PIL display image is loaded in two ways:

First: call the operating system built-in picture browser to open the picture

Copy the code
the PIL Import Image from 
# Load 
IM = Image.open ( 'cat.jpg') 

# Show 
im.show () 

"" " 
# apos size W Back Image, H = (480, 360) 
W, H = img.size 

# the PIL .JpegImagePlugin.JpegImageFile 
of the type (img) 

the Save method # direct calls to the Image class 
img.save ( 'new_cat.png') 
"" "
Copy the code

Second: let the program to draw pictures

Copy the code
from PIL import Image
import matplotlib.pyplot as plt
img=Image.open('/home/wanghao/Pictures/001.jpg')
plt.figure("head")
plt.imshow(img)
plt.show()
Copy the code

Some rendering and display pictures summary of some commonly used functions:

Function name Features Call format
figure Create a display window plt.figure(num=1,figsize=(8,8)
imshow Draw pictures plt.imshow(image)
show Display window plt.show()
subplot Allotting map plt.subplot (2,2,1)
title FIG sub-title set (in combination with the subplot) plt.title('origin image')
axis Whether sitting ruler plt.axis ( 'off')
subplots Creates a window with multiple sub-graph fig,axes=plt.subplots(2,2,figsize=(8,8))
ravel FIG variables for each sub-set ax0,ax1,ax2,ax3=axes.ravel()
set_title FIG sub-title set (in combination with the axes) ax0.set_title('first window')
tight_layout Automatically adjusting the display layout submap plt.tight_layout()

Reference: https://blog.csdn.net/u010472607/article/details/78855816

Guess you like

Origin www.cnblogs.com/abels0025/p/11301976.html