【问题标题】:Channel Replacement Using PIL使用 PIL 进行通道替换
【发布时间】:2020-02-16 23:24:15
【问题描述】:

我目前正在开发一个工具,其中 1 个图像的通道需要用 3 个其他图像替换。

例如:

I have an image "X" and the channels would be X.r, X.g and X.b respectively.
I have 3 other images ["A","B","C"]
Those 3 images needs to replace the channels in X.

So the result would be X.A, X.B and X.C.

最好的方法是什么?

【问题讨论】:

  • 最好的方法是阅读pillow的文档
  • 如果要替换X的所有频道,为什么首先需要X?输出中不会留下任何东西。
  • 到底是什么问题?你有没有尝试过,做过任何研究? Stack Overflow 不是免费的代码编写服务。请参阅:tourHow to Askhelp centermeta.stackoverflow.com/questions/261592/…
  • @AMC 我确实做了一些研究,但没有任何结果。因此问题。我不是在这里寻找免费代码。欢迎新的stackoverflow成员的方式:)

标签: python python-imaging-library


【解决方案1】:

你有split()merge()等方法

from PIL import Image

img = Image.open('images/image.jpg')

r,g,b = img.split()

#r = img.getchannel(0)
#g = img.getchannel(1)
#b = img.getchannel(2)

img = Image.merge('RGB', (r,g,b))

img.show()

您还可以转换为 numpy 数组并使用数组

from PIL import Image
import numpy as np

img = Image.open('images/image.jpg')

arr = np.array(img)

r = arr[:,:,0]
g = arr[:,:,1]
b = arr[:,:,2]

arr[:,:,0] = r
arr[:,:,1] = g
arr[:,:,2] = b

img = Image.fromarray(arr)

img.show()

例子

from PIL import Image

img1 = Image.open('winter.jpg')
img2 = Image.open('spring.jpg')

r1,g1,b1 = img1.split()
r2,g2,b2 = img2.split()

new_img = Image.merge('RGB', (r1,g2,b2))

new_img.show()

new_img.save('output.jpg')

冬天.jpg

spring.jpg

输出.jpg

【讨论】:

  • 感谢您的帮助。这是我所追求的边缘。我想将第二个、第三个和第四个图像合并为第一个图像的 RGB 通道。它确实以正确的方式发送给我。我最终将 RGB 更改为灰度模式,否则它不会让我将图像复制到 RGB 通道中。所以非常感谢你:)
猜你喜欢
  • 1970-01-01
  • 2018-10-19
  • 1970-01-01
  • 2017-02-23
  • 2011-01-02
  • 1970-01-01
  • 2013-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多