-
-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathimages_to_pdf.py
More file actions
94 lines (78 loc) · 2.75 KB
/
images_to_pdf.py
File metadata and controls
94 lines (78 loc) · 2.75 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from tkinter import Tk, Button, Label, filedialog, messagebox
from PIL import Image
import os
import webbrowser
class ImageToPDFApp:
"""A simple Interface to convert images into a PDF file."""
def __init__(self, root):
self.root = root
self.root.title("Image to PDF Converter")
self.root.geometry("420x260")
self.root.config(bg="#f4f4f4")
self.files = []
self.label = Label(
root,
text="Upload your images to convert to PDF",
bg="#f4f4f4",
font=("Arial", 12)
)
self.label.pack(pady=15)
self.upload_btn = Button(
root,
text=" Upload Images",
command=self.upload_files,
bg="#4285F4",
fg="white",
width=25
)
self.upload_btn.pack(pady=10)
self.convert_btn = Button(
root,
text="Convert to PDF",
command=self.convert_to_pdf,
bg="#34A853",
fg="white",
width=25
)
self.convert_btn.pack(pady=10)
def upload_files(self):
"""Open a file dialog to select image files."""
self.files = filedialog.askopenfilenames(
title="Select Images",
filetypes=[("Image Files", "*.jpg *.jpeg *.png")]
)
if self.files:
messagebox.showinfo("Files Selected", f"{len(self.files)} image(s) selected.")
else:
messagebox.showwarning("No File", "Please select at least one image.")
def convert_to_pdf(self):
"""Convert selected images into a single PDF and open it."""
if not self.files:
messagebox.showerror("Error", "No images selected.")
return
output_name = filedialog.asksaveasfilename(
defaultextension=".pdf",
filetypes=[("PDF Files", "*.pdf")],
title="Save PDF As"
)
if output_name:
images = [Image.open(f).convert("RGB") for f in self.files]
images[0].save(output_name, save_all=True, append_images=images[1:])
messagebox.showinfo(
"Success",
f" PDF saved successfully!\n\n Location:\n{output_name}"
)
# Automatically open the PDF after saving
try:
webbrowser.open_new(rf"file://{os.path.abspath(output_name)}")
except Exception as e:
messagebox.showwarning(
"Open File",
f"PDF saved but couldn't open automatically.\nError: {e}"
)
else:
messagebox.showwarning("Cancelled", "Save operation cancelled.")
if __name__ == "__main__":
root = Tk()
app = ImageToPDFApp(root)
root.mainloop()