Python OpenCV reading and displaying images

Source: Internet
Author: User

Read in image

The read-in image uses Cv2.imread ().

Read mode:

Cv2. Imread_color: Read a pair of color images, the transparency of the image is ignored, this is the default parameter

Cv2. Imread_grayscale: Reading into images in grayscale mode

Cv2. Imread_unchanged: Reads a pair of images and includes an alpha channel of the image

# -*-coding:utf-8-*- Import Cv2 Import  = cv2.imread ('test.jpg', 0)

The read path is wrong and there is no hint. Can only get a none

Display image

Use Cv2.imshow () to display the image.

The window is automatically resized to the image size.

The first parameter is the name of the window, followed by our image.

You can create multiple windows with different names.

Cv2.waitkey () is a keyboard binding function.

Its time scale is the millisecond level.

The function waits for a certain number of milliseconds to see if there is a keyboard input.

If any input returns the ASCII value of the key, continue running.

If there is no input, return-1.

If the setting parameter is 0, the key will wait indefinitely.

Cv2.destroyallwindows ()

You can delete the created window (you can specify the window name)

# -*-coding:utf-8-*- Import Cv2 Import  = cv2.imread ('test.jpg', 0) cv2.imshow ('image ' , IMG) cv2.waitkey (0) cv2.destroyallwindows ()

You can also create a window before loading the image.

Cv2.namedwindow ('image', Cv2. Window_normal) cv2.imshow ('image', img) cv2.waitkey (0) cv2.destroyallwindows () 

Save Image

Cv2.imwrite (' name ', object)

Precautions:

Cv2.waitkey () when used, if it is a 64-bit system, change to Cv2.waitkey () & 0xFF

Python OpenCV reading and displaying images

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.