In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use python to find out the RGB color of each pixel of a picture". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Tools:
Pillow library
If you have PyCharm, you can install pillow in Project Interpreter in Preferences.
If you don't have PyCharm. Please check the installation process online, it is very simple.
A few more important functions to use (you can skip it first, come back and check if you encounter a function you don't know):
It is recommended to read the English document of PIL directly: http://effbot.org/imagingbook/image.htm.
1.image.new (mode,size,color)
Generate a new image using the given variables mode and size. Mode is a picture mode, such as "RGB", "P". Size is a given width / height binary, which is calculated in terms of the number of pixels. For single-channel images, the variable color is given only one value; for multi-channel images, the variable color is given a tuple (one value for each channel).
2.im.convert (mode)
Convert the picture you open into a certain format.
3.im.getpixel (xy)
Returns the pixels at the given location.
4.im.putpixel (xy,color)
Modifies the pixels at a given location.
5.im.size ()
Returns two elements, width and height. Im.size ⇒ (width, height)
If the variable name .size [0] is wide, size [1] means high.
Pictures used:
From PIL import Image
Im = Image.open ("1.jpg")
For y in range (im.size [1]):
For x in range (im.size [0]):
Pix = im.getpixel ((xQuery y))
Print (pix)
The result of the run is very long, and we intercept some of it:
I have to say that Python is really powerful.
However, it is really troublesome to look at it this way, because there are so many pixels that it is impossible for us to analyze the data manually.
If we want to calculate the number of RGB bits less than 100, we can do this:
From PIL import Image
Im = Image.open ("1.jpg")
Count = 0
For y in range (im.size [1]):
For x in range (im.size [0]):
Pix = im.getpixel ((xQuery y))
If (pix [0] < 100 and pix [1] < 100 and pix [2] < 100):
Count = count + 1
Print (count)
The result is 265743.
We can even convert them to other colors.
It is best not to operate on the original image, we can new a picture of the same size.
Im2 = Image.new ("RGB", im.size,255)
It means to create a new RGB image as big as it, with a red background.
We convert the pixels that have just been less than 100 bits to white.
From PIL import Image
Im = Image.open ("1.jpg")
Im2 = Image.new ("RGB", im.size,255)
Count = 0
For y in range (im.size [1]):
For x in range (im.size [0]):
Pix = im.getpixel ((xQuery y))
If (pix [0] < 100 and pix [1] < 100 and pix [2] < 100):
Im2.putpixel ((xQuery y), (255255255))
Im2.show ()
Results:
If we change the color of the original image:
Well, it's acceptable.
This is the end of the content of "how to use python to find out the RGB color of each pixel of a picture". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.