# Window shown waits for any key pressing eventĬv2. transforms as transforms from PIL import Image Read the image img Image.open('laptop.jpg') define a transform to convert the image to grayscale transform transforms. import required libraries import torch import torchvision. Gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) The following Python3 program converts the input PIL image to grayscale. # Use the cvtColor() function to grayscale the image If the flag value is equal to 0, it will read the image as grayscale, and if it is equal to -1, the method will read the image including the alpha channel information. If the flag value of the cv2.imread() method is equal to 1, it will read the image excluding the alpha channel. The default method of converting a greyscale (L) or RGB image into a bilevel (mode 1) image uses Floyd-Steinberg dither to approximate the original image. The cv2.imread(path, flag) method accepts two parameters: path and flag. For using this, first we have to import the cv2 library in the Python file using the import statement. The cv2.imread() method loads an image from the specified file. It provides good support for Machine Learning, Face Recognition, Deep Learning, etc. Gray Scale - 'L' Mode In 14: ayscale(img) Out 14: In 16: img. As a matter of fact, ayscale (img) directly calls img.convert ('L') according to the implementation. So to convert the color image to grayscale we will be using cv2.imread(image-name.png,0) or you can also write cv2.IMREADGRAYSCALE in the place of 0 as it. ayscale (img) is equivalent to img.convert ('L'). OpenCV is a free, open source library that is used for computer vision. You can use the method nvert to convert a PIL.Image to different modes. Convert image to grayscale using OpenCV Library
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |