首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Python中读取给定像素的RGB值?

如何在Python中读取给定像素的RGB值?
EN

Stack Overflow用户
提问于 2008-09-26 08:10:51
回答 8查看 398K关注 0票数 170

如果我用open("image.jpg")打开一个图像,假设我知道像素的坐标,我如何获得该像素的RGB值?

那么,我该如何反其道而行之呢?从一个空白图形开始,“写”一个具有某个RGB值的像素?

如果我不需要下载任何额外的库,我会更喜欢。

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 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,它提供了更丰富的应用程序接口来创建图像。

票数 240
EN

Stack Overflow用户

发布于 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
票数 7
EN

Stack Overflow用户

发布于 2008-09-26 08:28:57

在wiki.wxpython.org上有一篇非常好的文章,题目是Working With Images。文章提到了使用wxWidgets (wxImage)、PIL或PythonMagick的可能性。就我个人而言,我使用过PIL和wxWidgets,它们都使得图像操作变得相当简单。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/138250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档