我正试图用Python解决一个问题,我需要将TIFF图像转换为JPEG。 我已经尝试使用 Pillow 以及 OpenCV 来做这件事,但是当我尝试转换一个保存有透明度的 TIFF 图像时,一直得到错误。 如果我保存TIFF并移除透明度,它就会成功保存JPEG。 透明度必须保持在TIFF上。 有人知道这个问题的解决方案吗? 如果我能找到一种方法,甚至通过一个Python脚本保存没有透明度的TIFF,保存为JPEG,然后删除没有透明度的TIFF,这也会工作。 如果有任何帮助,我将非常感激。 下面是我试过的失败的代码的例子。
import os
from PIL import Image
os.chdir('S:/DAM/Test/Approved/')
# for root, dirs, files in os.walk('S:/DAM/Test/Approved'):
for root, dirs, files in os.walk('.'):
for name in files:
if name.endswith('.tif'):
filename = os.path.join(root, name)
print('These are the files: ', filename)
# img = Image.open(filename).convert('RGB')
img = Image.open(filename)
print('image is open', filename)
img = img.convert('RGB')
print('image should be converted: ', filename)
imageResize = img.resize((2500, 2500))
print('image should be resized: ', filename)
imageResize.save(filename[:-4]+'.jpg', 'JPEG')
print('image should be saved as a jpeg: ', filename)
当Python试图用Pillow打开带有透明度的TIFF时,我得到的错误是这样的。
Exception has occurred: UnidentifiedImageError
cannot identify image file '.\\Beauty Images\\XXX.tif'
File "U:\Python files\image_conversion2.py", line 22, in <module>
img = Image.open(filename)
当我用OpenCV运行这段代码时,在同一张图片上也失败了。
img = cv2.imread('S:/DAM/Test/Approved/Beauty Images/XXX.tif')
cv2.imwrite('S:/DAM/Test/Approved/Beauty Images/XXX.jpg', img)
这是我在使用这段代码时得到的错误。
OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:715: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'
File "U:\Python files\img_convert_new.py", line 19, in <module>
cv2.imwrite('S:/DAM/Test/Approved/Beauty Images/XXX.tif', img)
下面是如何用Python Wand读取CMYKA TIFF,删除alpha通道,保存为JPG,并将图像转换为OpenCV格式。
输入图片。
from wand.image import Image
from wand.display import display
import numpy as np
import cv2
with Image(filename='guinea_pig.tiff') as img:
display(img)
with img.clone() as img_copy:
# remove alpha channel and save as JPG
img_copy.alpha_channel='off'
img_copy.format = 'jpeg'
img_copy.save(filename='guinea_pig.jpg')
display(img_copy)
# convert to opencv/numpy array format and reverse channels from RGB to BGR for opencv
img_copy.transform_colorspace('srgb')
img_opencv = np.array(img_copy)
img_opencv = cv2.cvtColor(img_opencv, cv2.COLOR_RGB2BGR)
# display result with opencv
cv2.imshow("img_opencv", img_opencv)
cv2.waitKey(0)
结果是JPG
感谢@cgohlke 找到了解决方案! 解决方法如下,使用 imagecodecs。 fullpath变量是源路径的根+'' +文件。
for root, subdirs, files in os.walk(src):
for file in files:
fullpath = (root + '/' + file)
from imagecodecs import imread, imwrite
from PIL import Image
imwrite(fullpath[:-4] + '.jpg', imread(fullpath)[:,:,:3].copy()) # <-- using the imagecodecs library function of imread, make a copy in memory of the TIFF File.
# The :3 on the end of the numpy array is stripping the alpha channel from the TIFF file if it has one so it can be easily converted to a JPEG file.
# Once the copy is made the imwrite function is creating a JPEG file from the TIFF file.
# The [:-4] is stripping off the .tif extension from the file and the + '.jpg' is adding the .jpg extension to the newly created JPEG file.
img = Image.open(fullpath[:-4] + '.jpg') # <-- Using the Image.open function from the Pillow library, we are getting the newly created JPEG file and opening it.
img = img.convert('RGB') # <-- Using the convert function we are making sure to convert the JPEG file to RGB color mode.
imageResize = img.resize((2500, 2500)) # <-- Using the resize function we are resizing the JPEG to 2500 x 2500
imageResize.save(fullpath[:-4] + '.jpg') # <-- Using the save function, we are saving the newly sized JPEG file over the original JPEG file initially created.