-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimagetopdf_converter.py
More file actions
46 lines (39 loc) · 1.67 KB
/
imagetopdf_converter.py
File metadata and controls
46 lines (39 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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()