【发布时间】:2021-12-04 20:26:42
【问题描述】:
我正在尝试完成一个挑战,我使用一个方程从其他图像构建一个新图像 (d)。然后我必须得到图像(d)中的标志。给定的图像是 a.png、b.png c.png 和 y.png,它们可以在这里找到:https://drive.google.com/drive/folders/1bZOm_0apr5ZmaRNf9R5UVIEmtMuYSphn?usp=sharing
等式:d = y - 21a - 3b + 41c
我当前的代码
from PIL import Image
imagey = Image.open('y.png')
imagea = Image.open('a.png')
imageb = Image.open('b.png')
imagec = Image.open('c.png')
size = width, height = imagey.size
new = Image.new('RGB', size)
imgy = imagey.load()
imga = imagea.load()
imgb = imageb.load()
imgc = imagec.load()
data = new.load()
for x in range(width):
for y in range(height):
they = imgy[x, y]
thea = imga[x, y]
theb = imgb[x, y]
thec = imgc[x, y]
new_color = ((int(they[0])) & ~(int((21 * thea[0])) ^ int((3 * theb[0])) ^ int(~(41 * thec[0]))),
(int(they[1])) & ~(int((21 * thea[1])) ^ int((3 * theb[1])) ^ int(~(41 * thec[1]))),
(int(they[2])) & ~(int((21 * thea[2])) ^ int((3 * theb[2])) ^ int(~(41 * thec[2]))))
data[x, y] = new_color
new.save('final.png')
new.show()
【问题讨论】:
-
我不明白。你的公式是简单的乘法和加法。这与按位运算有什么关系?
-
如果您将
Pillow image转换为numpy array,或者您将使用OpenCV或imageio来加载图像(并直接获取numpy array),那么您可以直接使用imagey - 21*imagea - 3*imageb + 41*imagec
标签: python python-3.x python-imaging-library bitwise-operators