如何在Python中增加PNG文件中的像素大小?

时间:2012-10-06 21:48:37

标签: python png python-imaging-library pypng

假设我有一个这样的PNG图像: https://www.box.com/s/dc4dut3yw1vhagm4y9ks

我想增加红点的大小,这意味着他们也必须填补他们的方形邻居。如下所示:

www - > rrr

wrw - > rrr

www - > rrr

1 个答案:

答案 0 :(得分:1)

我很确定这可以进一步优化...基本上它会加载图像两次,读取im1和draw1,如果在draw2中找到红点,则范围为[x + -1,y + -1]改变。最终由draw2制作的im2被保存。

#!/usr/bin/env python

import Image

im1 = Image.open("hil0.png")
im2 = Image.open("hil0.png")
w, h = im1.size

draw1 = im1.load()
draw2 = im2.load()

for x in range(w):
    for y in range(h):
        if draw1[x,y] == (255,0,0):
            for dx in [-1,0,1]:
                for dy in [-1,0,1]:
                    nx = x+dx
                    ny = y+dy
                    # print nx, ny
                    if nx>=0 and ny>=0 and nx<w and ny<h:
                        draw2[ nx, ny ] = (255,0,0)



# im2.show()
im2.save("hil1.png")