Ben Chuanlong Du's Blog

It is never too late to learn.

Shift Images Using Pillow in Python

In [4]:
import numpy as np
from PIL import Image, ImageOps, ImageChops
In [8]:
img = Image.open("../../home/media/poker/4h.png")
img
Out[8]:

ImageChops.offset

Shift to the left with 10 pixels.

In [9]:
ImageChops.offset(img, -10, 0)
Out[9]:

Image.crop and ImageOps.pad

In [16]:
img.crop((10, 0, 37, 54))
Out[16]:
In [22]:
img_crop = img.crop((10, 0, 37, 54))
ImageOps.pad(img_crop, img.size, color="white", centering=(0, 0))
Out[22]:
In [10]:
img_crop = img.crop((10, 0, 37, 54))
ImageOps.pad(img_crop, img.size, color="black", centering=(0, 0))
Out[10]:

Image.crop and ImageOps.fit

In [9]:
img_crop = img.crop((10, 0, 37, 54))
ImageOps.pad(img_crop, img.size, method=0, centering=(0, 0))
Out[9]:
In [ ]:
 

Comments