Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: PIL replace a single RGBA color

I have already taken a look at this question: SO question and seem to have implemented a very similar technique for replacing a single color including the alpha values:

c = Image.open(f)
c = c.convert("RGBA")
w, h = c.size
cnt = 0
for px in c.getdata():
    c.putpixel((int(cnt % w), int(cnt / w)), (255, 0, 0, px[3]))
    cnt += 1                                                                                                   

However, this is very slow. I found this recipe out on the interwebs, but have not had success using it thus far.

What I am trying to do is take various PNG images that consist of a single color, white. Each pixel is 100% white with various alpha values, including alpha = 0. What I want to do is basically colorize the image with a new set color, for instance #ff0000<00-ff>. SO my starting and resulting images would look like this where the left side is my starting image and the right is my ending image (NOTE: background has been changed to a light gray so you can see it since it is actually transparent and you wouldn't be able to see the dots on the left.)

alt text

Any better way to do this?

like image 402
sberry Avatar asked Sep 20 '10 14:09

sberry


People also ask

Does PIL read RGB or BGR?

But for PIL, the input is RGB, while it's BGR for cv2.

What can I use instead of PIL in Python?

Support for Python Imaging Library got discontinued in 2011, but a project named pillow forked the original PIL project and added Python3. x support to it. Pillow was announced as a replacement for PIL for future usage. Pillow supports a large number of image file formats including BMP, PNG, JPEG, and TIFF.


3 Answers

If you have numpy, it provides a much, much faster way to operate on PIL images.

E.g.:

import Image
import numpy as np

im = Image.open('test.png')
im = im.convert('RGBA')

data = np.array(im)   # "data" is a height x width x 4 numpy array
red, green, blue, alpha = data.T # Temporarily unpack the bands for readability

# Replace white with red... (leaves alpha values alone...)
white_areas = (red == 255) & (blue == 255) & (green == 255)
data[..., :-1][white_areas.T] = (255, 0, 0) # Transpose back needed

im2 = Image.fromarray(data)
im2.show()

Edit: It's a slow Monday, so I figured I'd add a couple of examples:

Just to show that it's leaving the alpha values alone, here's the results for a version of your example image with a radial gradient applied to the alpha channel:

Original: alt text

Result: alt text

like image 196
Joe Kington Avatar answered Oct 19 '22 20:10

Joe Kington


Try this , in this sample we set the color to black if color is not white .

#!/usr/bin/python
from PIL import Image
import sys

img = Image.open(sys.argv[1])
img = img.convert("RGBA")

pixdata = img.load()

# Clean the background noise, if color != white, then set to black.

for y in xrange(img.size[1]):
    for x in xrange(img.size[0]):
        if pixdata[x, y] == (255, 255, 255, 255):
            pixdata[x, y] = (0, 0, 0, 255)

you can use color picker in gimp to absorb the color and see that's rgba color

like image 11
Yuda Prawira Avatar answered Oct 19 '22 20:10

Yuda Prawira


The Pythonware PIL online book chapter for the Image module stipulates that putpixel() is slow and suggests that it can be sped up by inlining. Or depending on PIL version, using load() instead.

like image 4
T.P. Avatar answered Oct 19 '22 19:10

T.P.