from PIL import Image
im = Image.open('dead_parrot.jpg') # Can be many different formats.
pix = im.load()
print im.size # Get the width and hight of the image for iterating over
print pix[x,y] # Get the RGBA Value of the a pixel of an image
pix[x,y] = value # Set the RGBA Value of the image (tuple)
im.save('alive_parrot.png') # Save the modified pixels as .png
使用Pillow(与Python 3.X和Python 2.7+一起使用),可以执行以下操作:
现在你有了所有的像素值。如果是RGB,则
im.mode
可以读取另一个模式。然后您可以通过以下方法获得像素(x, y)
:或者,您可以使用Numpy并重塑阵列:
一个完整、简单易用的解决方案是
最好使用Python Image Library来完成这项工作,恐怕这是一个单独的下载。
最简单的方法是通过load() method on the Image object返回一个像素访问对象,您可以像数组一样对其进行操作:
或者,看看ImageDraw,它为创建图像提供了更丰富的API。
PyPNG-轻量级PNG解码器/编码器
虽然这个问题暗示了JPG,但我希望我的回答对一些人有用。
下面是如何使用PyPNG module读取和写入PNG像素:
PyPNG是一个不到4000行的纯Python模块,包括测试和注释。
PIL是一个更全面的图像库,但它也明显更重。
相关问题 更多 >
编程相关推荐