问题描述
我正在尝试使用 Pillow 在 Python 中将 24 位灰度 Tiff 图像转换为 JPEG。这种尝试适用于一些 24 位 Tiff 图像,但不是全部。它为下图提供了 unkNown raw mode
:
from PIL import Image
im = Image.open("example.tif")
if im.mode != "L": # rescale 16 bit tiffs to 8 bits
im.mode = "I"
im = im.point(lambda i: i * (1.0 / 256))
im = im.convert("RGB")
im.save("example.jpg","JPEG",quality=100)
解决方法
事实证明,这个示例图像的模式已经是 RGB
,即使图像看起来是灰度的。如果您不先尝试手动重新缩放,则枕头转换工作正常:
from PIL import Image
im = Image.open("example.tif")
if im.mode not in ("L","RGB"): # rescale 16 bit tiffs to 8 bits
im.mode = "L"
im = im.point(lambda i: i * (1.0 / 256))
im = im.convert("RGB")
im.save("example.jpg","JPEG",quality=100)