1. Reading pictures
Import= cv2.imread ('./test.jpg')
The picture that is read is Numpy.ndarray format, the value is 0~255, img shape is (picture height, picture width, picture channel number), print value come out to discover channel order is BGR, not RGB.
2. Change the image size
Import Cv2 = + = Cv2.imread ('./test.jpg ' = Cv2.resize (img , (width, height), interpolation=cv2. Inter_area) cv2.imshow (", img) cv2.waitkey (0) cv2.destroyallwindows ()
The size of this transformation, you need to put the specified width of the picture in front, the height of the image is placed behind, and the size of the IMG shape is inconsistent.
3. Grayscale Conversion
Import= cv2.imread ('./test.jpg'= Cv2.cvtcolor (img, Cv2. Color_bgr2gray)
Shape after conversion (picture height, picture width)
Some other conversions:
Import= cv2.imread ('./test.jpg'== = = Cv2.cvtcolor (Img_gray, Cv2. COLOR_GRAY2RGB)
4. Save the picture
Import=+ = Cv2.imread ('./test.jpg'= Cv2.resize (IMG, (width, height), interpolation=cv2. Inter_area) cv2.imwrite ('test_changed.png', IMG)
Implement CV2 Yourself
from Import Image Import NumPy as NP def imread (filename): return np.array (image.open (filename)) [:,:,;:-1]
The Cv2 of Python