I tried playing with the image with Pillow

What is Pillow?

An image processing library for Python. https://pillow.readthedocs.io/en/stable/

This environment

Preparation

Import the package.

from PIL import Image
import matplotlib.pyplot as plt
%matplotlib inline

Display the image for the time being

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_color.png

Try grayscale

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()

lena_gray_1.png

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_2.png

save

lena_gray.save("./lena_gray.png ")

Resize

lena_resize = lena.resize((150,150))
fig, ax = plt.subplots()
ax.imshow(lena_resize)
plt.title("Lena Resize")
plt.show()

lena_resize.png

If you look at the scale of the image, you can see that it has been resized.

Rotate

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()

lena_rotate_1.png

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()

lena_rotate_2.png

Recommended Posts

I tried playing with the image with Pillow
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
I tried "binarizing" the image with Python + OpenCV
I tried playing with the calculator on tkinter
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
I tried simple image recognition with Jupyter
I tried to process the image in "sketch style" with OpenCV
I tried to process the image in "pencil style" with OpenCV
I tried image recognition of CIFAR-10 with Keras-Learning-
I tried image recognition of CIFAR-10 with Keras-Image recognition-
I tried to save the data with discord
I tried playing with PartiQL and MongoDB connected
I tried to correct the keystone of the image
I tried playing mahjong with Python (single mahjong edition)
I tried using the image filter of OpenCV
I tried simple image processing with Google Colaboratory.
I tried fp-growth with python
I tried scraping with Python
I tried AutoGluon's Image Classification
Image processing with PIL (Pillow)
I tried to detect the iris from the camera image
I tried Learning-to-Rank with Elasticsearch!
I tried to touch the CSV file with Python
I tried clustering with PyCaret
I tried to solve the soma cube with python
I tried the changefinder library!
I tried to solve the problem with Python Vol.1
[Introduction to AWS] I tried porting the conversation app and playing with text2speech @ AWS ♪
I tried gRPC with Python
I tried scraping with python
[Introduction to AWS] I tried playing with voice-text conversion ♪
I tried hitting the API with echonest's python client
I tried to compress the image using machine learning
I tried to simulate how the infection spreads with Python
I tried to analyze the whole novel "Weathering with You" ☔️
I tried using the Python library from Ruby with PyCall
I tried to find the average of the sequence with TensorFlow
I tried to notify the train delay information with LINE Notify
I tried replacing the Windows 10 HDD with a smaller SSD
I tried using the DS18B20 temperature sensor with Raspberry Pi
I tried saving the DRF API request history with django-request
I tried to divide the file into folders with Python
I displayed the chat of YouTube Live and tried playing
I tried the TensorFlow tutorial 1st
I tried the Naro novel API 2
I tried trimming efficiently with OpenCV
I tried summarizing sentences with summpy
seam carving (image cropping) I tried
I tried machine learning with liblinear
I tried web scraping with python.
I tried knocking 100 image processes (Q1 ~ Q10)
I tried moving food with SinGAN
I tried the TensorFlow tutorial 2nd
I tried implementing DeepPose with PyTorch
I liked the tweet with python. ..
I tried face detection with MTCNN
I tried the Naruro novel API
Try blurring the image with opencv2
I tried running prolog with python 3.8.2.