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

Ivan Elchin ivangermes at gmail.com
Fri Nov 27 15:59:23 CET 2009


Thank you!
It's works fine!

2009/11/20 Alexey Borzenkov <snaury at gmail.com>

> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/image-sig/attachments/20091127/09e361f9/attachment-0001.htm>


More information about the Image-SIG mailing list