A, Numpy

Reshape (1, 2, 3, 3, 3, 3) img = img. I can't reshape(1, 2, 3, 3, 4, 4) img = img. I can't reshape(1, 3, 4, 4). In addition, the "img =" cannot be removed, otherwise there will be no reshape effect. # new_W*new_H*new_C must be equal to old_H*old_W*old_C. This reshape is not recommended for image, because it will mess up the relationship between HW and C and destroy the image
Img =np. Resize (img,(new_H,new_W,new_C)) img. Resize (new_H,new_W,new_C) And you cannot add "img =", otherwise an error will be reported. # new_W*new_H*new_C must be equal to old_H*old_W*old_C. This type of resize is also not recommended for images, because it will mess up the relationship between HW and C and destroy the image
img = np.zeros((H,W),dtype=np.uint8)

Second, the CV2

Img = cv2.imread('test.jpg', 0) img = cv2.imread_grayscale ('test.jpg', 0
Img = cv2.resize(img, (new_W, new_H)); The result is that the deformation # can not write the "img =" part because the real shape of the image CV2.RESize is changed. The interpolation is different from the interpolation. Not on the copy
# W (width) direction is X axis; 1. Cv2. circle(img, (x, x, x)) Circle y), radius, (255255255), 1) (2) cv2. A rectangle (img, (the upper left corner x, y) at the upper left, the top right-hand corner (top right corner x, y), (255255255), 1) 3. Img [y][x] img[y][x] That is, the rows are iterated first, and then the columns, in the normal order of arrays in C or Python

Third, PIL

Img = image.open ('test.jpg').convert('L') # convert = img.size
IMG = img.resize((new_W, new_H), image.bilinear) # new_W*new_H can be less than old_H*old_W, and the result is that the deform # is modified on the copy, and must be preceded by "img =".

Fourth, the matplotlib

from PIL import Image
img=Image.open('1.png')
img.show()

PIL uses the open() function to open the image and the show() function to display the image. This way of picture display is to call the operating system’s own picture browser to open the picture, sometimes this way is not very convenient, so we can also use another way, let the program to draw the picture.

PIL import Image img= image.open ('1.png') import matplotlib.pyplot as PLT plt.ion() # The next one that opens this sequence is plt.imShow (img) plt.show() # Picture Show img.save('1.jpg') # Image Save plt.axis('off') # Remove the axis plt.saveFig ('1.jpg')# Picture save, will generate coordinate axis, and will change the original size of the edge image, not easy to use

This method is a bit more complicated, but recommended one, which uses a Matplotlib library to draw images for display. Matplotlib is a professional plotting library, equivalent to MATLAB plot.

It is also important to note that if you want to use Matplotlib to display or save images, it is recommended to use PIL to read them instead of CV2.

Because PIL reads images in the order of R, G, B channels, in line with the requirements of Matplotlib;

If you read it in CV2, the order of the channels is B, G, R, which can be displayed and saved, but the color will get weird.

Five, the tensor

Shape # from torchvision import utils as vutils vutils. Save_image (tensor_img, './test.jpg', '. normalize=True)

*, PIL and CV2 transform each other

Img = v2.cvtColor(np.asarray(img), cv2.color_rgb2bgr) img = Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))