Wednesday, December 19, 2018

codingdirectional: Open a picture with pillow module

Welcome to the brand new chapter of the python module’s series where we will start to take a look at each ready-made python’s module which can be used in varies python projects development. In these few chapters, we will look at the pillow module which can be used to manipulate the picture and displays the picture from our windows os’s computer hard drive. In the first chapter regarding the pillow module we will create a tkinter’s user interface with a button which will open the file selector dialog when the user clicks on it, the user can then select an image file (with either the png or jpg format) from his computer hard drive and this program will display that image to the user.

Starting from now on, I will switch between PyCharm and Visual Studio Code IDE when writing a python program, visual studio code is a fantastic IDE for python’s program development besides PyCharm, if you do not have it installed on your pc yet, then just go ahead and download it through this link.  The user interface of this IDE is as good as the one from PyCharm so it will be great if you can keep one copy of it for your own use.

The next thing you need to do is to install pillow using pip install pillow command on the windows command prompt.

Finally here is the python source code to open up the image with the pillow module, take note that we do not import pillow into the program but instead import the image module from PIL which seems a little bit confusing to me at the beginning.

from PIL import Image as pimage
from tkinter import *
from tkinter import filedialog

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 open an 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
    
    if(fullfilename != ''):
        im = pimage.open(fullfilename)
        im.show()

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()
Pillow module in action

That is it! One more thing I forget to mention above is that we can use visual studio code for other programming languages project as well besides the python project!



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...