[Python] Mask the image into a circle using Pillow

Introduction

Here is the code that uses Python's ** Pillow ** to mask the area other than the center of the image in a circle.

** Image example after mask processing ** maksed

Image to use

The area other than the horse's face is masked and painted black in a circle. [Horse image] (https://pixabay.com/ja/photos/%E9%A6%AC-%E5%8B%95%E7%89%A9-%E4%B9%97%E3%82%8B-%E3%83%AC%E3%82%A4%E3%82%BF%E3%83%BC%E3%83%9B%E3%83%95-4847088/) Horse

The image used for the mask is this image. Black mask image Horse

Mask processing flow

  1. Generate a panel in which the masked part (other than the horse's face) is white and the part to which the masking process is not applied (horse's face part) is black. The frame of the image is white and cannot be seen, but it has the same image size as the horse image. panel

  2. Paste the mask image hollowed out from the panel on the horse image.

code

I will briefly explain the program.

** The entire program is listed at the end. ** **

Loading mask image

The mask image is also loaded according to the size of the horse image.

Load image


from PIL import Image,ImageDraw

#Load horse image
img = Image.open("horse.jpg ")

#Load the mask image to match the horse image
mask_width = img.size[0]
mask_height = img.size[1]
black_mask=Image.open("black.jpg ").resize((mask_width,mask_height))

Panel creation

Create a panel (circle_mask) of the same size as the black mask image and draw a circle on it. When creating the panel, "L": 8bit grayscale image is used.

The mask image can be used when it is the same size as the pasted image and the mode is the following three types. 1: 1bit image (binary image) L: 8-bit grayscale image RGBA: Image with alpha channel

When the mask image is 8-bit grayscale (mode ='L'), 0 (black) is 100% of the base image, 255 (white) is 100% of the pasted image, and the median value is 2 images according to the value. Is blended.

Panel creation



#Draw a circle on the panel
circle_mask = Image.new("L", black_mask.size, 255)
draw = ImageDraw.Draw(circle_mask)

Draw a circle

Note that the circle is drawn using the ellipse method, but the coordinates of the bounding box (bottom left and bottom right) are passed.

Draw a circle


#Coordinates of the bounding box of the circle
start_x = 600
start_y = 200
width = 900
height = 900 #If you want to make a perfect circle, the same value of width
end_x = start_x + width
end_y = start_y + height

#drawing
draw.ellipse(((start_x, start_y),(end_x, end_y)), fill=0)

Hollow out the image and mask it

Mask processing


#Circular panel(circle_mask)As a mask, black mask image(black_mask)To the horse image
img.paste(black_mask, (0, 0), circle_mask)

#Save
img.save("horse_masked.jpg ")
maksed

Whole code

from PIL import Image,ImageDraw

#Load horse image
img = Image.open("horse.jpg ")

#Load the mask image to match the horse image
mask_width = img.size[0]
mask_height = img.size[1]
black_mask=Image.open("black.jpg ").resize((mask_width,mask_height))

#Draw a circle on the panel
circle_mask = Image.new("L", black_mask.size, 255)
draw = ImageDraw.Draw(circle_mask)

#Coordinates of the bounding box of the circle
start_x = 600
start_y = 200
width = 900
height = 900 #If you want to make a perfect circle, the same value of width
end_x = start_x + width
end_y = start_y + height

#drawing
draw.ellipse(((start_x, start_y),(end_x, end_y)), fill=0)

#Circular panel(circle_mask)As a mask, black mask image(black_mask)To the horse image
img.paste(black_mask, (0, 0), circle_mask)

#Save
img.save("horse_masked.jpg ")

Reference URL

A detailed explanation of the arguments is introduced. note.nkmk.me

Recommended Posts

[Python] Mask the image into a circle using Pillow
Try a similar search for Image Search using the Python SDK [Search]
Create a GIF file using Pillow in Python
Cut a part of the string using a Python slice
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 5 ~
A little bit from Python using the Jenkins API
The image is a slug
[python] Change the image file name to a serial number
Paste the image into an excel file using Python's openpyxl
Generate a Docker image using Fabric
Turn your Android Smart Phone into a Web Server using python.
Control the motor with a motor driver using python on Raspberry Pi 3!
Image capture of firefox using python
[Python] Using OpenCV with Python (Image Filtering)
I made a Line-bot using Python!
Do a search by image from the camera roll using Pythonista3
Create a python GUI using tkinter
[Python] Using OpenCV with Python (Image transformation)
Drawing a silverstone curve using python
Try to edit a new image using the trained StyleGAN2 model
Create a simple scheduled batch using Docker's Python Image and parse-crontab
[Python] Make the function a lambda function
[Ev3dev] Create a program that captures the LCD (screen) using python
I just erased the object using image repair (inpaint) (OpenCV: Python)
Extract the targz file using python
Try using the Python Cmd module
[Python] A simple function to find the center coordinates of a circle
I ran GhostScript with python, split the PDF into pages, and converted it to a JPEG image.
A Python script that automatically collects typical images using bing image search
[EV3 x Python] Stream the camera image to your PC using mjpg-streamer.
[Mac] Build a Python 3.x environment at the fastest speed using Docker
A python program that resizes a video and turns it into an image
Create a record with attachments in KINTONE using the Python requests module
A useful note when using Python for the first time in a while
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Behind the flyer: Using Docker with Python
Create a dummy image with Python + PIL.
Write the test in a python docstring
Python: Application of image recognition using CNN
Cut out A4 print in the image
Tweet using the Twitter API in Python
Create a graph using the Sympy module
Search the maze with the python A * algorithm
[Python] Create a Batch environment using AWS-CDK
Run the Python interpreter in a script
[GIMP] [Python-Fu] Divide the image into swords
Scraping a website using JavaScript in Python
[Python] Scraping a table using Beautiful Soup
I tried playing with the image with Pillow
Easy image processing in Python with Pillow
Draw a tree in Python 3 using graphviz
The story of blackjack A processing (python)
Add a layer using the Keras backend
A program that plays rock-paper-scissors using Python
[Python] A progress bar on the terminal
Introduce graphviz into a portable python environment