In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use Python to convert color images into grayscale images, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
The first method
Python's cv2 library has its own color-to-grayscale method, and it is very simple, with 9 lines of code and 1 line of core code.
The big idea is to read a color picture first, and then display it in the window, and then let cv2 deal with it and convert it into a grayscale image, at this time it is a two-dimensional grayscale matrix, so we want to save it from array to image, and finally show it in another window, in order to avoid the window flashing by, we need to add waitKey (0) this sentence.
Import cv2from PIL import Image# reads the color image color_img = cv2.imread (rroomdataset3 _ original image',color_img _ image. Jpg') # displays the image in the window, the window and the original size of the image adaptive cv2.imshow ('image) # the first parameter of the image is the processed image The second is RGB2GRAYgray_img=cv2.cvtColor (color_img,cv2.COLOR_RGB2GRAY) # gray_img, which is still a two-dimensional matrix representation at this time, so to convert array to image, gray=Image.fromarray (gray_img) # saves the picture to the current path with the saved file name gray.save ('gray.jpg') cv2.imshow (' Gray Image',gray_img) # if you want to keep the window persistent, you need to use this function cv2.waitKey (0)
The second method
The Image library in PIL also comes with a way to convert grayscale images, and then use pyplot to display them on the canvas. A total of 8 lines of code, the core code is 1 line.
From PIL import Imagefrom matplotlib import pyplot as pltcolor_img=Image.open ('dataset3/beauty.jpg') gray_img=color_img.convert (' L') plt.figure ('beauty') plt.imshow (gray_img,cmap='gray') plt.axis (' off') plt.show ()
Python batch turns pictures into grayscale images from PIL import Imageimport ospath = 'F:/QUEXIANJIANCESHIYAN/posdata'file_list = os.listdir (path) for file in file_list: I = Image.open (path+ "/" + file) L = I.convert (' L') L.save (path+ "/" + file) # print (file) all the contents of this article "how to use Python to convert color images into grayscale images", thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.