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 **
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/)
The image used for the mask is this image. Black mask image
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.
Paste the mask image hollowed out from the panel on the horse image.
I will briefly explain the program.
** The entire program is listed at the end. ** **
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))
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)
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)
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 ")
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 ")
A detailed explanation of the arguments is introduced. note.nkmk.me
Recommended Posts