[Image-SIG] resizing an image with alphachannel: dirty edges

Alexey Borzenkov snaury at gmail.com
Fri Nov 20 14:17:15 CET 2009


On Thu, Nov 12, 2009 at 3:59 PM, Ivan Elchin <ivangermes at gmail.com> wrote:
> I have a problem, when i resizing an image with alphachannel.

You can try converting your image to RGBa (premultiplied alpha),
resizing it, then converting back to RGBA. The problem here is that
there are dirty pixels with 0 alpha, and they get interpolated like
everything else. On my PIL this works:

from PIL import Image

im = Image.open("sega.png")
im = im.convert("RGBa")

cur_width, cur_height = im.size
new_width, new_height = (200, 200)

if not new_width == 0 and not new_height == 0:
   ratio = min(float(new_width)/cur_width,
               float(new_height)/cur_height)
else:
   if new_width == 0:
       ratio = float(new_height)/cur_height
   else:
       ratio = float(new_width)/cur_width


new_dimensions = (int(round(cur_width*ratio)),
                 int(round(cur_height*ratio)))

new_im = im.resize(new_dimensions, Image.ANTIALIAS)
new_im = new_im.convert("RGBA")

new_im.save('rez.png')

Though my PIL has many modifications, I'm not sure if RGBA->RGBa->RGBA
is implemented in vanilla 1.1.6. (after checking) Ah, yes, it's not.
Though you can try recompiling PIL with this patch:
http://git.kitsu.ru/patched/pil.git?a=commitdiff;h=b8f1c572430b06b5d4294fb2bf29327275120554


More information about the Image-SIG mailing list