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
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
Screen capture
from PIL import ImageGrab
ImageGrab.grab(bbox=(0, 50, 800, 650))
Recommended Posts