8

我正在尝试使用 Python Imaging Library 更改照片中的 RGB 值。我一直在使用函数 Image.point ,它做我想要的,除了我希望能够在 R、G 和 B 值上实现不同的函数。有谁知道我该怎么做?

谢谢!

4

1 回答 1

7

numpy除了 PIL 之外,您最好使用它来对图像的各个波段进行数学运算。

作为一个人为的例子,它并不意味着以任何方式看起来都很好:

import Image
import numpy as np

im = Image.open('snapshot.jpg')

# In this case, it's a 3-band (red, green, blue) image
# so we'll unpack the bands into 3 separate 2D arrays.
r, g, b = np.array(im).T

# Let's make an alpha (transparency) band based on where blue is < 100
a = np.zeros_like(b)
a[b < 100] = 255

# Random math... This isn't meant to look good...
# Keep in mind that these are unsigned 8-bit integers, and will overflow.
# You may want to convert to floats for some calculations.
r = (b + g) * 5

# Put things back together and save the result...
im = Image.fromarray(np.dstack([item.T for item in (r,g,b,a)]))

im.save('output.png')

输入 在此处输入图像描述


输出 在此处输入图像描述

于 2012-05-31T01:34:16.307 回答