Python PIL - 为什么我不能在这里使用 ImageOps.colorize。?
Python PIL - why can't I use ImageOps.colorize here.?
我正在尝试使用 PIL 和 python 制作 3D 图像。但是我在包含“(red_img = ImageOps.colorize(right_img_developed,(0, 0, 0), (255, 0, 0)))
和 cyan_img = ImageOps.colorize(left_img_developed,(0, 0, 0), (0, 255, 255))
” 的行中得到了 AssertionError 说“assert image.mode == "L"
”,我尝试用谷歌搜索错误但我无法得到任何答案。我还查看了文档,但找不到任何有用的帮助。提前致谢。:那是我的代码:
import io, re, requests
from PIL import Image, ImageOps, ImageEnhance
imgpth ='path/image.jpg'
right_img = Image.open(imgpth)
right_img_resized = right_img.resize((400, 400))
right_img_developed = right_img_resized.transform((400, 300), Image.QUAD, data =(0, 0, 100, 400, 300, 400, 400, 0), resample=Image.BILINEAR)
left_img_url = re.sub('FRB', 'FLB', imgpth)
left_img = Image.open(left_img_url)
left_img_resized = left_img.resize((400, 400))
left_img_developed = left_img_resized.transform((400, 300), Image.QUAD, data =(0, 0, 100, 400, 300, 400, 400, 0), resample=Image.BILINEAR)
red_img = ImageOps.colorize(right_img_developed,(0, 0, 0), (255, 0, 0))
cyan_img = ImageOps.colorize(left_img_developed,(0, 0, 0), (0, 255, 255))
blend = Image.blend(red_img, cyan_img, 0.5)
red_img.show()
cyan_img.show()
blend.show()
函数ImageOps.colorize
expects a grayscale image as input and applies a color scale to it. That is what mode "L"代表(L = Luminosity = grayscale).
如果您正在加载的图像以 RGB(A) 格式存储,那么您可以像这样将其转换为灰度:
img = Image.open("image.jpg").convert("L")
我正在尝试使用 PIL 和 python 制作 3D 图像。但是我在包含“(red_img = ImageOps.colorize(right_img_developed,(0, 0, 0), (255, 0, 0)))
和 cyan_img = ImageOps.colorize(left_img_developed,(0, 0, 0), (0, 255, 255))
” 的行中得到了 AssertionError 说“assert image.mode == "L"
”,我尝试用谷歌搜索错误但我无法得到任何答案。我还查看了文档,但找不到任何有用的帮助。提前致谢。:那是我的代码:
import io, re, requests
from PIL import Image, ImageOps, ImageEnhance
imgpth ='path/image.jpg'
right_img = Image.open(imgpth)
right_img_resized = right_img.resize((400, 400))
right_img_developed = right_img_resized.transform((400, 300), Image.QUAD, data =(0, 0, 100, 400, 300, 400, 400, 0), resample=Image.BILINEAR)
left_img_url = re.sub('FRB', 'FLB', imgpth)
left_img = Image.open(left_img_url)
left_img_resized = left_img.resize((400, 400))
left_img_developed = left_img_resized.transform((400, 300), Image.QUAD, data =(0, 0, 100, 400, 300, 400, 400, 0), resample=Image.BILINEAR)
red_img = ImageOps.colorize(right_img_developed,(0, 0, 0), (255, 0, 0))
cyan_img = ImageOps.colorize(left_img_developed,(0, 0, 0), (0, 255, 255))
blend = Image.blend(red_img, cyan_img, 0.5)
red_img.show()
cyan_img.show()
blend.show()
函数ImageOps.colorize
expects a grayscale image as input and applies a color scale to it. That is what mode "L"代表(L = Luminosity = grayscale).
如果您正在加载的图像以 RGB(A) 格式存储,那么您可以像这样将其转换为灰度:
img = Image.open("image.jpg").convert("L")