Не удалось преобразовать изображения в PDF. Ошибка объекта 'str' не имеет атрибута 'encoderinfo'Python

Программы на Python
Anonymous
Не удалось преобразовать изображения в PDF. Ошибка объекта 'str' не имеет атрибута 'encoderinfo'

Сообщение Anonymous »

Я пытаюсь создать конвертер изображений в PDF на Python. Он хорошо работает при выборе только одного изображения, но когда я выбираю несколько, выдает следующую ошибку: у 'str' obejct нет атрибута 'encoderinfo'.
Вот мой полный код:< /p>
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from PIL import Image
import os
# function to convert images to PDF
def images_to_pdf(images, pdf_name):
try:
# create a new pdf file
pdf = Image.open(images[0])
pdf.save(pdf_name, "PDF", resolution=100.0,
save_all=True, append_images=images[1:])
messagebox.showinfo("Success",
"Images have been successfully converted to PDF.")
except Exception as e:
messagebox.showerror("Error",
"Failed to convert images to PDF.\nError: " + str(e))
# function to select images
def select_images():
images = filedialog.askopenfilenames(title="Select Images",
filetypes=(("Image files", "*.jpg;*.jpeg;*.png"),
("All files", "*.*")), initialdir = "C:/")
return images
# function to select pdf name and path
def select_pdf():
pdf = filedialog.asksaveasfilename(title="Save PDF As",
defaultextension=".pdf", initialdir = "C:/",
filetypes=(("PDF files", "*.pdf"),("All files", "*.*")))
return pdf
# create GUI
root = tk.Tk()
root.title("Convert Images to PDF")
select_images_btn = tk.Button(root,
text="Select Images", command=select_images)
select_pdf_btn = tk.Button(root, text="Select PDF",
command=select_pdf)
convert_btn = tk.Button(root, text="Convert",
command=lambda: images_to_pdf(select_images(), select_pdf()))
select_images_btn.pack()
select_pdf_btn.pack()
convert_btn.pack()
root.mainloop()

Как я могу выбрать несколько изображений и поместить их в один целый PDF-файл?
Изменить: также извините за отсутствие сообщения трассировки . Вот оно:
Traceback (most recent call last):
File "c:\Users\username\OneDrive\Desktop\ConvertImageToPdf\ImageToPdf.py", line 13, in images_to_pdf
pdf.save(pdf_name, "PDF", resolution=100.0,
File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\site-packages\PIL\Image.py", line 2459, in save
save_handler(self, fp, filename)
File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\site-packages\PIL\PdfImagePlugin.py", line 43, in _save_all
_save(im, fp, filename, save_all=True)
File "C:\Users\username\AppData\Local\Programs\Python\Python312\Lib\site-packages\PIL\PdfImagePlugin.py", line 221, in _save
append_im.encoderinfo = im.encoderinfo.copy()
^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'encoderinfo'


Подробнее здесь: https://stackoverflow.com/questions/786 ... encoderinf

Вернуться в «Python»