Mirror image¶

In [1]:
import skimage as ski
from PIL import Image, ImageOps
import matplotlib.pyplot as plt
import numpy as np

In this notebook we read in a JPEG file of a photo of a python facing to the left, then make a mirror image showing the python facing to the right.
We will explain two different approaches, one using the PIL library, and the other using skimage.

In [4]:
pics_dir = '../data/images/'

PIL version¶

In [5]:
left_pic = Image.open(pics_dir + 'python_facing_left.jpg')
left_pic
Out[5]:
No description has been provided for this image
In [6]:
right_pic = ImageOps.mirror(left_pic)
right_pic.save(pics_dir + 'python_facing_right.jpg')
right_pic
Out[6]:
No description has been provided for this image

skimage version¶

In [8]:
left_pic = ski.io.imread(pics_dir + 'python_facing_left.jpg')
fig, ax = plt.subplots()
ax.axis('off')
ax.imshow(left_pic)
Out[8]:
<matplotlib.image.AxesImage at 0x1cbffe6d690>
No description has been provided for this image
In [9]:
h, w, c = left_pic.shape
A = [[-1, 0 , w], [0, 1, 0], [0, 0, 1]]
ref = ski.transform.AffineTransform(matrix = A)
right_pic = ski.transform.warp(left_pic, inverse_map = ref.inverse)
right_pic = (right_pic * 255).astype(np.uint8)
ski.io.imsave(pics_dir + 'python_facing_right.jpg', right_pic, plugin='imageio')
fig, ax = plt.subplots()
ax.axis('off')
ax.imshow(right_pic)
Out[9]:
<matplotlib.image.AxesImage at 0x1cb84199e50>
No description has been provided for this image
In [ ]: