Welcome back, in this article we will continue to use pillow to create a thumbnail from a jpeg picture and then save that thumbnail in the png format. Below code will first open a picture and then reduces it’s size to 64×64 pixel if the picture is a square with equal sides, or else it will scale down that picture based on the 1.33 aspect ratio.
from PIL import Image as pimage from tkinter import * from tkinter import filedialog import os win = Tk() # Create instance win.title("Display Picture") # Add a title win.resizable(0, 0) # Disable resizing the GUI win.configure(background='black') # change background color # Create a label aLabel = Label(win, text="Click on below button to manipulate that image!", anchor="center", padx=13, pady=10, relief=RAISED,) aLabel.grid(column=0, row=0, sticky=W+E) aLabel.configure(foreground="white") aLabel.configure(background="black") aLabel.configure(wraplength=160) # Open an image file def openImage(): fullfilename = filedialog.askopenfilename(initialdir="/", title="Select a file", filetypes=[("Image files", "*.jpg; *.png")]) # select png or jpg image file from the hard drive size = (64, 64) # thumbnail if(fullfilename != ''): f, e = os.path.splitext(fullfilename) f = f + '.png' # the new output file format im = pimage.open(fullfilename) im.thumbnail(size) im.save(f, "png") action_pic = Button(win, text="Open Image", command=openImage, padx=2) action_pic.grid(column=0, row=1, sticky=E+W) action_pic.configure(background='black') action_pic.configure(foreground='white') win.mainloop()
After you have clicked on the open image button to select a picture, this program will turn it into a thumbnail then saves that thumbnail in the png format. With that, we have concluded the pillow module demo and in the next chapter, we will start to look into where are the other online sources that you can visit to learn python.
from Planet Python
via read more
No comments:
Post a Comment