Easy image processing in Python with Pillow

pillow: Image processing library. Lighter and easier than scikit-image and opencv.

Installation

$ pip install -U Pillow

Image acquisition with wget

$ cd <workspace>
$ wget --no-check-certificate -O gorilla.jpg https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Male_silverback_Gorilla.JPG/236px-Male_silverback_Gorilla.JPG
$ atom test_pillow.py #See below

gorilla.jpg

A script that loads an image, draws it, and saves it

test_pillow.py


from __future__ import print_function
from PIL import Image, ImageDraw, ImageFont

#Read from file
im = Image.open('gorilla.JPG')

#Image information
print(im.format, im.size, im.mode)

#Code conversion Gray
# im = im.convert("L")
#Code conversion Alpha Channel added
# im = im.convert('RGBA')
#Code conversion RGB2BGR
# r, g, b = im.split() 
# im = Image.merge("RGB", (b, g, r))

#Area cut
# box = (50, 50, 200, 200) # left, upper, right, lower
# region = im.crop(box)
# region
# region.show()
#Area paste
# im = im.paste(region, box)

#Image conversion
# im = im.transpose(Image.FLIP_LEFT_RIGHT)
# im = im.transpose(Image.ROTATE_90)
# im = im.rotate(45)

#filtering
# from PIL import ImageFilter
# im = im.filter(ImageFilter.BLUR)

#Drawing
# font_Look up in book and choose your favorite font@mac
fnt = ImageFont.truetype('/Library/Fonts/Arial.ttf', size=20)
d = ImageDraw.Draw(im)
d.line((0, 0) + im.size, fill=(128,0,0), width=2)
d.line((0, im.size[1], im.size[0], 0), fill=(128,0,0), width=2)
# draw rectangle
import numpy as np
thickness = 5
left, top, right, bottom = 30,20, 220, 220 #Really deep_Use learning. Designated this time.
text_origin = (10,10)
label_size = d.textsize("gorilla", fnt)
for i in range(thickness):
    d.rectangle([left + i, top + i, right - i, bottom - i], outline=(0,0,128))
d.rectangle([text_origin, tuple(np.array(text_origin) + label_size)], fill=(0,0,128))
d.text(text_origin, "gorilla", font=fnt, fill=(255,255,255))
del d

#Save image
im.save('gorilla_convert.jpg')

#Image display
im.show()

Run

$ python test_pillow.py

gorilla_convert.jpg

Screen capture

from PIL import ImageGrab

ImageGrab.grab(bbox=(0, 50, 800, 650))

Recommended Posts

Easy image processing in Python with Pillow
Image processing with Python
Image processing with Python (Part 2)
Image processing with PIL (Pillow)
Image processing with Python (Part 1)
Tweet with image in Python
Image processing with Python (Part 3)
Image processing by python (Pillow)
[Python] Image processing with scikit-image
[Python] Easy parallel processing with Joblib
Image processing with Python 100 knocks # 3 Binarization
Image processing with Python 100 knocks # 2 Grayscale
python image processing
Basics of binarized image processing with Python
Image processing with Python 100 knock # 10 median filter
Image processing with Python 100 knocks # 8 Max pooling
Image processing with Python & OpenCV [Tone Curve]
Image processing with Python 100 knock # 12 motion filter
Drawing with Matrix-Reinventor of Python Image Processing-
Image processing with Python 100 knocks # 7 Average pooling
Light image processing with Python x OpenCV
Image processing with Python 100 knocks # 9 Gaussian filter
Image processing with MyHDL
File processing in Python
First Python image processing
Text processing in Python
Image format in Python
Queue processing in Python
Image Processing with PIL
Image processing from scratch with python (5) Fourier transform
Image processing from scratch with python (4) Contour extraction
Image Processing with Python Environment Setup for Windows
Create an image with characters in python (Japanese)
Parallel processing with no deep meaning in Python
100 Language Processing with Python Knock 2015
UTF8 text processing in python
Scraping with selenium in Python
"Apple processing" with OpenCV3 + Python3
[Co-occurrence analysis] Easy co-occurrence analysis with Python! [Python]
Working with LibreOffice in Python
Scraping with chromedriver in python
Notes on HDR and RAW image processing with Python
Image editing with python OpenCV
Debugging with pdb in Python
[Python] Get the numbers in the graph image with OCR
Acoustic signal processing with Python (2)
Acoustic signal processing with Python
Easy folder synchronization with Python
Sorting image files with Python (2)
Scraping with Selenium in Python
Asynchronous processing (threading) in python
Easy image classification with TensorFlow
Scraping with Tor in Python
Sorting image files with Python
Combined with permutations in Python
Easy Python compilation with NUITKA-Utilities
Easy HTTP server with Python
Easy password box in Python
Using Python mode in Processing
Easy with just Python! Output Graphviz figures in draw.io format!
[Let's play with Python] Image processing to monochrome and dots