Scene
I need to replace the dark Yellow (255,255,0) in the following image (255,204,0). Use to the Pillow Library.
Code
fromPILImportImageimg = Image.open ("Logo.png") forIinchRange2048): forJinchRange2048):Try: R,g,b,alpha = Img.getpixel ((i,j))ifr==255 andg==255 andb==0: g=204Img.putpixel ((i,j), (R,g,b,alpha))exceptException asE:ContinueImg.show ()
Problem
The search results do not containalpha
, run a problem, print out found there are four values, see the code you wrote together fourth calledalpha
Is通道
, which is not available here.
Start running after the background color has been from white to pink ... Puzzled, and then looked at the white RGB value (255,255,255) so you need to add a judgment:and b==0
Can.
Be sure to add a try statement, because not allimg.getpixel((i,j))
All return 4 values, for example, in my case, White has only 3 values.
Important InformationIn fact, I have two basic pictures (): One yellow is (255,255,0), the other yellow is (255,204,0) + (255,255,0) This I animated a part of the color up. Actually run the program when only the second successfully replaced the color, the first attempt a few times not, the reason is unknown.
Effect
The effect is as follows, the original (left) new diagram (right). Photo stickers come from picsart app
. This section refers to the Python image processing library: Pillow Beginner's Tutorial
?
Replace a single color in a picture with Python-pillow