In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use Pillow image color processing, the editor thinks it is very practical, so share it for you to do a reference, I hope you can get something after reading this article.
Pillow provides a color processing module ImageColor, which supports colors in different formats, such as color triples in RGB format, hexadecimal color names (# ff0000), and color English words ("red"). At the same time, it can also convert CSS (cascading stylesheets, used to decorate web pages) style colors into RGB format.
Note that the ImageColor module is not sensitive to the size of the color. For example, "Red" can also be written as "red".
Color naming
ImageColor supports the naming of a variety of color modes (that is, the representation of faces in a fixed format), such as the well-known RGB color mode, as well as HSL (hue-saturation-lightness) and HSB (also known as HSV, hue-saturation-luminance) color mode. Here is a brief introduction to HSL:
H: Hue tone. Values range from 0 to 360. 0 means "red", 120th means "green", and 240indicates "blue".
S: Saturation saturation, which represents the purity of the color. The value is 0: 100%, where 0 represents gry, and 100% indicates the most saturated color.
L: Lightness lightness, with a value of 0,100%, where 0 means "black" black, 50% for normal color, and 100% for white.
The following uses the HSL color mode to represent red, in the following format:
HSL (0100% 50%)
The color at this time is "pure red", which is equivalent to RGB. To learn more about HSL/HSB, click the link to go there.
The ImageColor module is relatively simple, providing only two common methods, the getrgb () and getcolor () functions.
Getrgb () method
As the name implies, this function is used to get the RGB value of the color. The syntax format is as follows:
PIL.ImageColor.getrgb (color)
Examples of use are as follows:
From PIL import Image,ImageColor# getrgb () method color1=ImageColor.getrgb ("blue") print (color1) color2=ImageColor.getrgb ('# DCDCDC') print (color2) # uses HSL mode red color3=ImageColor.getrgb ('HSL (0100%) 50%)) print (color3)
The output is as follows:
(0,0,255)
(220, 220, 220)
(255,0,0)
You can create a new image through the new () method, or you can use ImageColor.getrgb () at this time, as follows:
# use new () to draw a new image im= Image.new ("RGB", (200,200), ImageColor.getrgb ("# EEB4B4")) im.save ("C:/Users/Administrator/Desktop/xin.jpg")
The picture is displayed as follows:
Figure 1: new image
Getcolor ()
Similar to getrgb (), this method is also used to get the color value, but it has an extra mode parameter, so the function can get the color value of the specified color mode. The syntax format is as follows:
PIL.ImageColor.getcolor (color, mode)
The parameters are described as follows:
Color: a color name, string format, can be an English word for color, or a hexadecimal color name. If it is an unsupported color, a ValueError error will be reported
Mode: specifies the color mode. If it is an unsupported mode, a KeyError error will be reported.
Examples of use are as follows:
Color4=ImageColor.getcolor ('# EEA9B8','L') print (color4) color5=ImageColor.getcolor ('yellow','RGBA') print (color5)
Output result:
one hundred and ninety one
(255,255,0,255)
Example-modify the background color of the document import PIL.Image as Image # = 1. Open the picture = img = Image.open ('1.jpg') # = 2. Process the picture = # divide the picture into small squares img_array = img.load () # iterate through each pixel block And process the color width, height = img.size# to get width and height for x in range (0Magnum width): for y in range (0Magna height): rgb = img_array [x Magi y] # get a pixel rgb r = rgb [0] g = rgb [1] b = rgb [2] if b > 130 and r
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.