Python:PIL替换单个RGBA颜色 [英] Python: PIL replace a single RGBA color

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

问题描述

我已经看过这个问题: SO问题和似乎已经实现了一种非常类似的技术来替换单个颜色,包括alpha值:

  c = Image.open 
c = c.convert(RGBA)
w,h = c.size
cnt = 0
对于c.getdata()中的px:
c.putpixel ((int(cnt%w),int(cnt / w)),(255,0,0,px [3])cnt + = 1

但是,这很慢。我发现这个食谱在网络上,但没有成功使用它迄今:食谱



我想要做的是采取由单一颜色,白色组成的各种PNG图像。每个像素是具有各种α值的100%白色,包括α= 0。我想要做的是基本上用新的设定颜色着色图像,例如#ff0000 <00-ff>。所以我的开始和结果的图像将看起来像这样左边是我的起始图像和右边是我的结束图像(注意:背景已改变为浅灰色所以你可以看到它,因为它实际上是透明的,你会' t



解决方案



p>如果你有numpy,它提供了一个更快,更快的方式来操作PIL图像。



例如:

  import Image 
import numpy as np

im = Image.open('test.png')
im = im。 convert('RGBA')

data = np.array(im)#data是一个高度x宽度x 4 numpy数组
red,green,blue,alpha = data.T #临时解开乐队的可读性

#将白色替换为红色...(只留下alpha值...)
white_areas =(red == 255)& (blue == 255)& (green == 255)
data [...,:-1] [white_areas.T] =(255,0,0)#转置回需要

im2 = Image.fromarray (数据)
im2.show()

编辑:这是一个缓慢的星期一,我想添加几个例子:



只是为了表明它只保留alpha值,这里是一个版本的示例图像的结果与径向渐变应用于Alpha频道:



原创:



结果:


I have already taken a look at this question: SO question and seem to have implemented a very similar technique for replacing a single color including the alpha values:

c = Image.open(f)
c = c.convert("RGBA")
w, h = c.size
cnt = 0
for px in c.getdata():
    c.putpixel((int(cnt % w), int(cnt / w)), (255, 0, 0, px[3]))                                                                                                      cnt += 1

However, this is very slow. I found this recipe out on the interwebs, but have not had success using it thus far: recipe

What I am trying to do is take various PNG images that consist of a single color, white. Each pixel is 100% white with various alpha values, including alpha = 0. What I want to do is basically colorize the image with a new set color, for instance #ff0000<00-ff>. SO my starting and resulting images would look like this where the left side is my starting image and the right is my ending image (NOTE: background has been changed to a light gray so you can see it since it is actually transparent and you wouldn't be able to see the dots on the left.)

Any better way to do this?

解决方案

If you have numpy, it provides a much, much faster way to operate on PIL images.

E.g.:

import Image
import numpy as np

im = Image.open('test.png')
im = im.convert('RGBA')

data = np.array(im)   # "data" is a height x width x 4 numpy array
red, green, blue, alpha = data.T # Temporarily unpack the bands for readability

# Replace white with red... (leaves alpha values alone...)
white_areas = (red == 255) & (blue == 255) & (green == 255)
data[..., :-1][white_areas.T] = (255, 0, 0) # Transpose back needed

im2 = Image.fromarray(data)
im2.show()

Edit: It's a slow Monday, so I figured I'd add a couple of examples:

Just to show that it's leaving the alpha values alone, here's the results for a version of your example image with a radial gradient applied to the alpha channel:

Original:

Result:

这篇关于Python:PIL替换单个RGBA颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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