[英]How to convert multi-page Image bytes into PDF or TIFF bytes in memory with Python?
[英]How to convert multi image TIFF to PDF in python?
我想在 python 中將多圖像 TIFF 轉換為 PDF。
我寫了這樣的代碼。 這段代碼如何不起作用。 我應該怎么改?
images = []
img = Image.open('multipage.tif')
for i in range(4):
try:
img.seek(i)
images.append(img)
except EOFError:
# Not enough frames in img
break
images[0].save('multipage.pdf',save_all=True,append_images=images[1:])
我解決了這個問題。 您可以通過此功能輕松地將 tiff 轉換為 pdf。
def tiff_to_pdf(tiff_path: str) -> str:
from PIL import Image
pdf_path = tiff_path.replace('.tiff', '.pdf')
if not os.path.exists(tiff_path): raise Exception(f'{tiff_path} does not find.')
image = Image.open(tiff_path)
images = []
for i, page in enumerate(ImageSequence.Iterator(image)):
page = page.convert("RGB")
images.append(page)
if len(images) == 1:
images[0].save(pdf_path)
else:
images[0].save(pdf_path, save_all=True,append_images=images[1:])
return pdf_path
當您使用此功能時,您需要安裝Pillow
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.