An image processing library for Python. https://pillow.readthedocs.io/en/stable/
Import the package.
from PIL import Image
import matplotlib.pyplot as plt
%matplotlib inline
lena = Image.open("./lena.png ")
type(lena)
output
PIL.PngImagePlugin.PngImageFile
fig, ax = plt.subplots()
ax.imshow(lena)
plt.title("Lena Color")
plt.show()
lena_gray = lena.convert("L")
type(lena_gray)
output
PIL.Image.Image
fig, ax = plt.subplots()
ax.imshow(lena_gray)
plt.title("Lena Gray")
plt.show()
It's a weird color, but it's a Matplotlib spec.
You need to change the color map from the default values.
Specify cmap =" gray "
to display a grayscale image.
For actually displaying a grayscale image set up the color mapping using the parameters
https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.imshow.html
fig, ax = plt.subplots()
ax.imshow(lena_gray, cmap="gray")
plt.title("Lena Gray")
plt.show()
lena_gray.save("./lena_gray.png ")
lena_resize = lena.resize((150,150))
fig, ax = plt.subplots()
ax.imshow(lena_resize)
plt.title("Lena Resize")
plt.show()
If you look at the scale of the image, you can see that it has been resized.
This time, rotate the image 75 degrees.
lena_rotate = lena.rotate(75)
fig, ax = plt.subplots()
ax.imshow(lena_rotate)
plt.title("Lena rotate 75")
plt.show()
I have run out. It doesn't seem to have changed from the size of the original image. Add ʻexpand = True` to Image.rotate so that it will not be cut off.
Optional expansion flag. If true, expands the output image to make it large enough to hold the entire rotated image. If false or omitted, make the output image the same size as the input image.
https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.rotate
lena_rotate_expand = lena.rotate(75, expand=True)
fig, ax = plt.subplots()
ax.imshow(lena_rotate_expand)
plt.title("Lena rotate 75 expand")
plt.show()
Recommended Posts