如何编写代码以使用 tifffile 库读取 TIFF 文件并将其转换为 JPEG?
How do I write code to use the tifffile library to read TIFF files and convert them to JPEGs?
我正在尝试编写一个 Python 程序,该程序将打开一个带有透明胶片的 TIFF 文件并创建一个 JPEG,同时还保留该 TIFF 文件。我试过 Pillow,但它不会处理上面有透明胶片的 TIFF,我试过 imagecodecs,但有时它出于某种原因无法打开某些 TIFF 文件。一个建议是使用 tifffile 但我不知道用来写这个的语法。任何人都可以帮助我吗?这是我当前使用 imagecodecs 的代码:
import shutil
import os
import io
import stat
import time
import datetime
from dateutil import parser
from PIL import Image
from imagecodecs import imread, imwrite
imwrite(filepath[:-4] + '.jpg', imread(filepath)[:,:,: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(filepath[:-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(filepath[:-4] + '.jpg') # <-- Using the save function, we are saving the newly sized JPEG file over the original JPEG file initially created.
这不起作用的原因是因为在 IDE 中打开了错误断点,所以无论如何它都不起作用,因为断点无论如何都会停止代码。关闭断点后,代码将按预期运行!
我正在尝试编写一个 Python 程序,该程序将打开一个带有透明胶片的 TIFF 文件并创建一个 JPEG,同时还保留该 TIFF 文件。我试过 Pillow,但它不会处理上面有透明胶片的 TIFF,我试过 imagecodecs,但有时它出于某种原因无法打开某些 TIFF 文件。一个建议是使用 tifffile 但我不知道用来写这个的语法。任何人都可以帮助我吗?这是我当前使用 imagecodecs 的代码:
import shutil
import os
import io
import stat
import time
import datetime
from dateutil import parser
from PIL import Image
from imagecodecs import imread, imwrite
imwrite(filepath[:-4] + '.jpg', imread(filepath)[:,:,: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(filepath[:-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(filepath[:-4] + '.jpg') # <-- Using the save function, we are saving the newly sized JPEG file over the original JPEG file initially created.
这不起作用的原因是因为在 IDE 中打开了错误断点,所以无论如何它都不起作用,因为断点无论如何都会停止代码。关闭断点后,代码将按预期运行!