Free App! ImgSlide 3.1 – Slide images and join them into a PDF

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:

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
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.