ImageMagic
We will be using simple image processing with PIL library
use command below to install it:
python3 -m pip install pillow
opening an image file using PIL:
use command below to install it:
python3 -m pip install pillow
opening an image file using PIL:
from PIL import Image im = Image.open("images/egg.png") im.show() #delegates show image command to system's default image viewer
Save image:
from PIL import Image im = Image.open("images/egg.png") im.save("images/newegg.png")
Crop Image
from PIL import Image im = Image.open("images/egg.png") cropbox = 50, 50, 200, 200 im = im.crop(cropbox) im.save("images/cropegg.png")
Creating a thumbnail:
The filter argument can be one of NEAREST (use nearest neighbour), BILINEAR (linear interpolation in a 2x2 environment), BICUBIC (cubic spline interpolation in a 4x4 environment), or ANTIALIAS (a high-quality downsampling filter). If omitted, or if the image has mode “1” or “P”, it is set to NEAREST.
The filter argument can be one of NEAREST (use nearest neighbour), BILINEAR (linear interpolation in a 2x2 environment), BICUBIC (cubic spline interpolation in a 4x4 environment), or ANTIALIAS (a high-quality downsampling filter). If omitted, or if the image has mode “1” or “P”, it is set to NEAREST.
from PIL import Image im = Image.open("images/egg.png") size = (30, 30) im.thumbnail(size, Image.ANTIALIAS ) im.save("images/neweggthumb.png")
Rotate Image
from PIL import Image im = Image.open("images/egg.png") im = im.rotate(90) im.save("images/rotatedegg.png")
Blur Image
from PIL import Image from PIL import ImageFilter im = Image.open("images/egg.png") im = im.filter(ImageFilter.BLUR) im.save("images/eggblur.png")
The current version of the library provides the following set of predefined image enhancement filters:
BLUR, CONTOUR, DETAIL, EDGE_ENHANCE,EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH,SMOOTH_MORE, and SHARPEN.
BLUR, CONTOUR, DETAIL, EDGE_ENHANCE,EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH,SMOOTH_MORE, and SHARPEN.