forked from KuKuXia/OpenCV_for_Beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_Basics_Read_and_Write_Image.py
37 lines (29 loc) · 1.2 KB
/
01_Basics_Read_and_Write_Image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""
Read and write image
- cv2.imread("imageName", flag): Reade the image
Color image loaded by OpenCV is in BGR mode
flag:
- cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will be neglected. It is the default flag.
- cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode
- cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel
Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively.
A transparent image has four channels — 3 for color, and one for transparency. Such as tiff and png format image files. These images can be read in OpenCV using the IMREAD_UNCHANGED flag.
- cv2.imshow("windowName", image): Show the image
The window automatically fits to the image size
"""
# Import the package
import cv2
# Create a window to contain the image
cv2.namedWindow("Image", cv2.WINDOW_NORMAL)
# Read the image
img = cv2.imread("./images/lena.jpg", 1)
# Show the image
cv2.imshow('Image', img)
# Wait until a key pressed
k = cv2.waitKey(0) & 0xFF
print(k)
# If button 's' pressed, save the image
if k == ord('s'):
cv2.imwrite('lena_copy.png', img)
# Destroy all the windows opened before
cv2.destroyAllWindows()