I would like to change a single color with Python.
If a fast solution with PIL exists, I would prefer this solution.
At the moment, I use
convert -background black -opaque '#939393' MyImage.png MyImage.png
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If
numpyis available on your machine, try doing something like:It will use a bit (3x) more memory, but it should be considerably (~5x, but more for bigger images) faster.
Also note that the code above is slightly more complicated than it needs to be if you only have RGB (and not RGBA) images. However, this example will leave the alpha band alone, whereas a simpler version wouldn’t have.