Note that there are some explanatory texts on larger screens.

plurals
  1. POTransparent png resizing with Python Image Library and the halo effect
    text
    copied!<p>There are a couple similar questions on SO, but none of them really helped. Basically I am trying to resize a simple png image, as seen here:</p> <p><a href="http://media.spiralknights.com/wiki-images/3/3e/Equipment-Proto_Sword_icon.png" rel="nofollow noreferrer">http://media.spiralknights.com/wiki-images/3/3e/Equipment-Proto_Sword_icon.png</a> (from the mmo Spiral Knights, copyright Three Rings Entertainment)</p> <p>I had originally implemented a utility which uses these images in php, and the resizing utility there worked perfectly well. I used the method described on the imagecopyresampled page in PHP's documentation.</p> <p>Honestly I can't even get it to resize better in Photoshop, but the results are almost disastrous in python. I consistently get a halo effect, and I believe this is at least in part to the actual RGBA values of the transparent pixels. Here, this picture tells it better:</p> <p><img src="https://i.imgur.com/kru1g.png" alt="resize examples"></p> <p>(the second to last resize was just a suggestion I saw on another forum to resize first to twice the final size, and it DID help at least a little, but not enough)</p> <p>The image is already in RGBA mode when it is being resized. </p> <p>As you can see the PHP and Photoshop resizes are halo-free. Honestly everything but the PHP resize does TOO much work, I like the minimal palette in the php image (if you look at the larger versions you can see that the PHP resize uses less in between colours), but I could live with the way Photoshop has resized it, or even the inner part of the python resize, but the halo is unacceptable.</p> <p>It seems to me -- and correct me if I'm wrong -- that PHP and Photoshop seem to know not to use the colour of the pixels in the alpha channel when interpolating, but python is using that light border, which is otherwise transparent, in its resize.</p> <p>Unfortunately there are a lot of different icons that I need to resize, with varying profiles, so they're not all as simple as this circular one, but this was just the first one I was using while experimenting.</p> <p>It's not much of a code question in and of itself, but if you need something to look at then this is the gist:</p> <pre><code>&gt;&gt; import Image &gt;&gt; img = Image.open('swordorig.png') &gt;&gt; img &lt;PngImagePlugin.PngImageFile image mode=RGBA size=256x256 at 0x2A3AF58&gt; &gt;&gt; img.resize((36,36), Image.ANTIALIAS).save('swordresize.png') </code></pre> <p>Eventual question being: is there a way to tell PIL NOT to use the colour of a pixel that has an alpha of 0 while resampling?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload