如果我用open("image.jpg")
打开一个图像,假设我知道像素的坐标,我如何获得该像素的RGB值?
那么,我该如何反其道而行之呢?从一个空白图形开始,“写”一个具有某个RGB值的像素?
如果我不需要下载任何额外的库,我会更喜欢。
发布于 2008-09-26 08:15:57
最好使用Python Image Library来做这件事,我担心这是一个单独的下载。
做你想做的事情最简单的方法是通过load() method on the Image object,它返回一个像素访问对象,你可以像操作数组一样操作它:
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
或者,看看ImageDraw,它提供了更丰富的应用程序接口来创建图像。
发布于 2015-11-10 21:02:32
photo = Image.open('IN.jpg') #your image
photo = photo.convert('RGB')
width = photo.size[0] #define W and H
height = photo.size[1]
for y in range(0, height): #each pixel has coordinates
row = ""
for x in range(0, width):
RGB = photo.getpixel((x,y))
R,G,B = RGB #now you can use the RGB value
发布于 2008-09-26 08:28:57
在wiki.wxpython.org上有一篇非常好的文章,题目是Working With Images。文章提到了使用wxWidgets (wxImage)、PIL或PythonMagick的可能性。就我个人而言,我使用过PIL和wxWidgets,它们都使得图像操作变得相当简单。
https://stackoverflow.com/questions/138250
复制相似问题