In this example we will show how to display images using the library Pillow with Python.
To open an image file, you need to use the open() function of the Image module. The usage is as follows: open(infile [, mode]) where
- infile is the path to open the image file.
- mode is the mode in which the file is opened, which is the same as the mode of the general file.
Source Code
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# showImages.py
from tkinter import *
from PIL import Image, ImageTk
win = Tk()
win.title(string = "Show Images Using Pillow")
# the path you store your iamges
path = "F:/logos/"
imgFile1 = Image.open(path + "cmu-logo.png")
imgFile2 = Image.open(path + "mit-logo.png")
imgFile3 = Image.open(path + "stanford-logo.png")
imgFile4 = Image.open(path + "berkeley-logo.png")
img1 = ImageTk.PhotoImage(imgFile1)
img2 = ImageTk.PhotoImage(imgFile2)
img3 = ImageTk.PhotoImage(imgFile3)
img4 = ImageTk.PhotoImage(imgFile4)
canvas = Canvas(win, width=360, height=360)
canvas.create_image(40, 40, image=img1, anchor=NW)
canvas.create_image(220, 40, image=img2, anchor=NW)
canvas.create_image(40, 220, image=img3, anchor=NW)
canvas.create_image(220, 220, image=img4, anchor=NW)
canvas.pack(fill=BOTH)
win.mainloop()
4. Data
showImages: these are the images that we’re going to show.