# Python Imaging Library
import PIL
from PIL import Image
# read sample image
pil_im = Image.open('images/empire.jpg')
print(pil_im)
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=490x689 at 0x2C4FA2B7C18>
# show Image
pil_im
# clone image and convert to Grayscale
pil_im_gray = pil_im.convert('L')
print(pil_im_gray)
pil_im_gray
<PIL.Image.Image image mode=L size=490x689 at 0x2C4FA211AC8>
#save garyscale picture
pil_im_gray.save("images/empire_gray.jpg")
import os
# create function that returns all files from path
def getImages(path):
return [os.path.join(path, f) for f in os.listdir(path) if f.endswith(".jpg")]
# use the function over the images folder.
getImages("images")
# load the first image
Image.open(getImages("images")[0])
#create thmbnail of an image of AxB size
thumb_empire = Image.open('images/empire.jpg')
thumb_empire.thumbnail((128,128))
print(thumb_empire)
thumb_empire
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=91x128 at 0x2C4FA2A7278>
# Crop a section of the Image
# first create a box with the left, upper, right, lower
box = (100,100,400,400)
region = pil_im.crop(box)
region
# Rotate the Image and Paste on Original Image
region = region.transpose(Image.ROTATE_180)
pil_im.paste(region, box)
pil_im
# Resize Image
out = pil_im.resize((128,128))
out
# rotate counterclockwise
out = pil_im.rotate(45)
out