11

Say you want to scale a transparent image but do not yet know the color(s) of the background you will composite it onto later. Unfortunately PIL seems to incorporate the color values of fully transparent pixels leading to bad results. Is there a way to tell PIL-resize to ignore fully transparent pixels?

import PIL.Image

filename = "trans.png"
size = (25,25)

im = PIL.Image.open(filename)
print im.mode  # RGBA

im = im.resize(size, PIL.Image.LINEAR)  # the same with CUBIC, ANTIALIAS, transform
# im.show()  # does not use alpha
im.save("resizelinear_"+filename)


# PIL scaled image has dark border

original image with (0,0,0,0) black but fully transparent background output image with black halo proper output scaled with gimp

original image with (0,0,0,0) (black but fully transparent) background (left)

output image with black halo (middle)

proper output scaled with gimp (right)

It looks like to achieve what I am looking for I would have to modify the sampling of the resize function itself such that it would ignore pixels with full transparency.

I have found a very ugly solution. It sets the color values of fully transparent pixels to the average of the surrounding non fully transparent pixels to minimize impact of fully transparent pixel colors while resizing. It is slow in the simple form but I will post it if there is no other solution. Might be possible to make it faster by using a dilate operation to only process the necessary pixels.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Gonzo
  • 2,023
  • 3
  • 21
  • 30
  • What mode is your file when loaded into Image? Maybe the alpha channel has been dropped? – Ber Oct 23 '12 at 09:39
  • It says RGBA and the scaled image looks ok in Gimp besides the border, too. – Gonzo Oct 23 '12 at 09:46
  • Did you try blending the scaled image over something else, like a white background? Is the scaling is okay then the the dark fringe will disappear. – Ber Oct 23 '12 at 09:56
  • The output image with border flattened onto a white background in Gimp still has the problem. – Gonzo Oct 23 '12 at 10:15
  • 1
    related/similar: http://stackoverflow.com/questions/9142825/transparent-png-resizing-with-python-image-library-and-the-halo-effect – Gonzo Nov 04 '12 at 09:54

4 Answers4

6

It appears that PIL doesn't do alpha pre-multiplication before resizing, which is necessary to get the proper results. Fortunately it's easy to do by brute force. You must then do the reverse to the resized result.

def premultiply(im):
    pixels = im.load()
    for y in range(im.size[1]):
        for x in range(im.size[0]):
            r, g, b, a = pixels[x, y]
            if a != 255:
                r = r * a // 255
                g = g * a // 255
                b = b * a // 255
                pixels[x, y] = (r, g, b, a)

def unmultiply(im):
    pixels = im.load()
    for y in range(im.size[1]):
        for x in range(im.size[0]):
            r, g, b, a = pixels[x, y]
            if a != 255 and a != 0:
                r = 255 if r >= a else 255 * r // a
                g = 255 if g >= a else 255 * g // a
                b = 255 if b >= a else 255 * b // a
                pixels[x, y] = (r, g, b, a)

Result: result of premultiply, resize, unmultiply

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Much better than my solution. I wonder how bad the loss of information is. It should be possible to speed these up nicely with numpy arrays. – Gonzo Nov 04 '12 at 09:46
  • on premultiplied alpha loss: http://www.quasimondo.com/archives/000665.php conclusion: best to only use on final output – Gonzo Nov 04 '12 at 10:23
  • 1
    @Phelix the loss of precision is only a problem if you're reducing the transparency for some reason. Most people don't do that. – Mark Ransom Nov 04 '12 at 18:15
3

You can resample each band individually:

im.load()
bands = im.split()
bands = [b.resize(size, Image.LINEAR) for b in bands]
im = Image.merge('RGBA', bands)

EDIT

Maybe by avoiding high transparency values like so (need numpy)

import numpy as np

# ...

im.load()
bands = list(im.split())
a = np.asarray(bands[-1])
a.flags.writeable = True
a[a != 0] = 1
bands[-1] = Image.fromarray(a)
bands = [b.resize(size, Image.LINEAR) for b in bands]
a = np.asarray(bands[-1])
a.flags.writeable = True
a[a != 0] = 255
bands[-1] = Image.fromarray(a)
im = Image.merge('RGBA', bands)
Nicolas Barbey
  • 6,639
  • 4
  • 28
  • 34
0

Maybe you can fill the whole image with the color you want, and only create the shape in the alpha channnel?

Ber
  • 40,356
  • 16
  • 72
  • 88
  • If I understand you correctly, this would loose antialiasing on the border. I would like to be the image pasted onto the background image to still have a smooth transition. – Gonzo Oct 23 '12 at 10:13
  • The AA could be done in the alpha channel instead (unless it is not already there) – Ber Oct 23 '12 at 19:41
  • That's the problem, I want to preserve the antialiasing from the original image as much as possible – Gonzo Oct 24 '12 at 14:13
0

sorry for answering myself but this is the only working solution that I know of. It sets the color values of fully transparent pixels to the average of the surrounding non fully transparent pixels to minimize impact of fully transparent pixel colors while resizing. There are special cases where the proper result will not be achieved.

It is very ugly and slow. I'd be happy to accept your answer if you can come up with something better.

# might be possible to speed this up by only processing necessary pixels
#  using scipy dilate, numpy where

import PIL.Image

filename = "trans.png"
size = (25,25)

import numpy as np
   
im = PIL.Image.open(filename)

npImRgba = np.asarray(im, dtype=np.uint8)
npImRgba2 = np.asarray(im, dtype=np.uint8)
npImRgba2.flags.writeable = True
lenY = npImRgba.shape[0]
lenX = npImRgba.shape[1]
for y in range(npImRgba.shape[0]):
    for x in range(npImRgba.shape[1]):
        if npImRgba[y, x, 3] != 0:  # only change completely transparent pixels
            continue        
        colSum = np.zeros((3), dtype=np.uint16)
        i = 0
        for oy in [-1, 0, 1]:
            for ox in [-1, 0, 1]:
                if not oy and not ox:
                    continue
                iy = y + oy
                if iy < 0:
                    continue
                if iy >= lenY:
                    continue
                ix = x + ox
                if ix < 0:
                    continue
                if ix >= lenX:
                    continue
                col = npImRgba[iy, ix]
                if not col[3]:
                    continue
                colSum += col[:3]
                i += 1
        npImRgba2[y, x, :3] = colSum / i
                                
im = PIL.Image.fromarray(npImRgba2)
im = im.transform(size, PIL.Image.EXTENT, (0,0) + im.size, PIL.Image.LINEAR)
im.save("slime_"+filename)

result: enter image description here

Gonzo
  • 2,023
  • 3
  • 21
  • 30