改变像素颜色Python [英] Changing pixel color Python

查看:122
本文介绍了改变像素颜色Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的福禄克机器人那里得到一个图像并确定我图像中每个像素的颜色。然后,如果像素大部分是红色,则将其更改为完全绿色。如果像素大部分为绿色,请将其更改为完全蓝色。如果像素主要是蓝色,则将其更改为完全红色。这是我能够做到的,但我无法让它工作以获得我必须改变的图像。没有语法错误,它只是语义我遇到了麻烦。我正在使用python。

I am suppose to get an image from my fluke robot and determine the color of each pixel in my image. Then if the pixel is mostly red, change it to completely green. If the pixel is mostly green, change it to completely blue. If the pixel is mostly blue, change it to completely red. This is what I am able to do, but I can't get it to work to get the image I have to change. There is no syntax error, it is just semantic I am having trouble with. I am using python.

我的尝试代码:

import getpixel
getpixel.enable(im)  
r, g, b = im.getpixel(0,0)  
print 'Red: %s, Green:%s, Blue:%s' % (r,g,b)

此外,我将照片保存如下:

Also I have the picture saved like the following:

pic1 = makePicture("pic1.jpg"):
    for pixel in getpixel("pic1.jpg"):
        if pixel Red: %s:
           return Green:%s
        if pixel Green:%s: 
           return Blue:%s


推荐答案

我假设你正在尝试使用 Image 模块。这是一个例子:

I assume you're trying to use the Image module. Here's an example:

import Image
picture = Image.open("/path/to/my/picture.jpg")
r,g,b = picture.getpixel( (0,0) )
print("Red: {0}, Green: {1}, Blue: {2}".format(r,g,b))

在此图片我得到了输出:

>>> import Image
>>> picture = Image.open("/home/gizmo/Downloads/image_launch_a5.jpg")
>>> r,g,b = picture.getpixel( (0,0) )
>>> print("Red: {0}, Green: {1}, Blue: {2}".format(r,g,b))
Red: 138, Green: 161, Blue: 175

编辑:
要做你想做的事我会尝试这样的事情

To do what you want I would try something like this

import Image
picture = Image.open("/path/to/my/picture.jpg")

# Get the size of the image
width, height = picture.size()

# Process every pixel
for x in width:
   for y in height:
       current_color = picture.getpixel( (x,y) )
       ####################################################################
       # Do your logic here and create a new (R,G,B) tuple called new_color
       ####################################################################
       picture.putpixel( (x,y), new_color)

这篇关于改变像素颜色Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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