Let’s add a new feature to ImageSlider, the free app made in python to make presentations out of png files (or to browse images). We will add the feature to create a pdf file out of the images.
You may want to know:
- how to convert all images into a pdf file
- the previous version of this code: Imagebrowser 3.0
- the first second of this code: imagebrowser 2.0
- the first version: Imagebroswer 1.0 (with labels instead of canvas)
- the alternative version with svg files: SvgSlider
The code
import tkinter as tk import glob import os from tkinter import filedialog cd = os.getcwd() print(cd) from fpdf import FPDF from PIL import Image import os def makePDF(): "Takes filename and number of pages abd creates pdf" lst_files = [ f for f in os.listdir() if f.endswith(".png") or f.endswith(".PNG")] w = 800 h = 600 for f in lst_files: cover = Image.open(f) if cover.size[0] > w: w = cover.size[0] if cover.size[1] > h: h = cover.size[1] nomefile = filedialog.asksaveasfilename() pdf = FPDF(unit="pt", format=[w, h]) for page in lst_files: pdf.add_page() pdf.image(page, 0, 0) pdf.output(nomefile + ".pdf", "F") os.startfile(nomefile + ".pdf") def insertfiles(): for filename in glob.glob("*.png"): lst.insert(tk.END, filename) def showimg(event): n = lst.curselection() filename = lst.get(n) img = tk.PhotoImage(file=filename) w, h = img.width(), img.height() print(filename) canvas.image = img canvas.config(width=w, height=h) canvas.create_image(0, 0, image=img, anchor=tk.NW) root = tk.Tk() root.geometry("800x600+300+50") lst = tk.Listbox(root, width=20) lst.pack(side="left", fill=tk.BOTH, expand=0) lst.bind("<<ListboxSelect>>", showimg) insertfiles() canvas = tk.Canvas(root) canvas.pack() menu = tk.Menu() menu.add_command(label="PDF", command=makePDF) root.configure(menu=menu) root.mainloop()
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame