使用Python图像库和光晕效果调整透明度 [英] Transparent png resizing with Python Image Library and the halo effect

查看:555
本文介绍了使用Python图像库和光晕效果调整透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SO上有几个类似的问题,但没有一个真的有帮助。基本上我正在尝试调整一个简单的png图像,如下所示:





感谢madlag



所以我的代码最终看起来像这样:

 >> import image,numpy 
>> img = Image.open('swordorig.png')
>> premult = numpy.fromstring(img.tostring(),dtype = numpy.uint8)
>> alphaLayer = premult [3 :: 4] / 255.0
>> premult [:: 4] * = alphaLayer
>>前提[1 :: 4] * = alphaLayer
>>前提[2 :: 4] * = alphaLayer
>> img = Image.fromstring(RGBA,img.size,premult.tostring())
>> img.resize((36,36),Image.ANTIALIAS).save('swordresize.png')

你得到了表格底部的图标。仍然比PHP版本更多的颜色,和一个非常温和的戒指,但总体来说,更好,更好。我确信可以调整算法以进一步改进它。再次感谢大家的回复!


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:

http://media.spiralknights.com/wiki-images/3/3e/Equipment-Proto_Sword_icon.png (from the mmo Spiral Knights, copyright Three Rings Entertainment)

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.

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:

(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)

The image is already in RGBA mode when it is being resized.

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.

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.

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.

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:

>> import Image
>> img = Image.open('swordorig.png')
>> img
<PngImagePlugin.PngImageFile image mode=RGBA size=256x256 at 0x2A3AF58>
>> img.resize((36,36), Image.ANTIALIAS).save('swordresize.png')

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?

解决方案

Thanks to everyone for answering! And especially to rotoglup for telling me the term for what I was actually looking for. Going through the stack overflow pages for premultiplied alpha in PIL I found a solution that works; here is an updated sheet of resize graphics:

The gray bars are just to give an impression of what they would look like on a lighter background.

https://stackoverflow.com/a/6882161/1189554

Thanks to madlag

So my code ends up looking like this:

>> import Image, numpy
>> img = Image.open('swordorig.png')
>> premult = numpy.fromstring(img.tostring(), dtype=numpy.uint8)
>> alphaLayer = premult[3::4] / 255.0
>> premult[::4] *= alphaLayer
>> premult[1::4] *= alphaLayer
>> premult[2::4] *= alphaLayer
>> img = Image.fromstring("RGBA", img.size, premult.tostring())
>> img.resize((36,36), Image.ANTIALIAS).save('swordresize.png')

And you get the icon that is on the bottom of the sheet. Still more colours than the PHP version, and a very mild ring, but overall much, much nicer. I'm sure the algorithm could be tweaked to improve it even more. Thanks again to everyone for responding!

这篇关于使用Python图像库和光晕效果调整透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆