Replace a single color in a picture with Python-pillow

Source: Internet
Author: User
Tags image processing library

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 calledalphaIs通道, 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==0Can.
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.