I am trying to crop an image in python in circular shape. And I also want to paste that image on the top of another image, and then then then save the image in a desired format.
This is the image that I want to crop in circular way
This is how the image should be looked like after cropping
This is the image on which I want to paste the circular shaped image
Here is the code, as far I tried
from PIL import Image, ImageDraw, ImageFilter im1 = Image.open('rocket.jpg') im2 = Image.open('lena.jpg') width, height = im1.size print(height, width) mask_im = Image.new("L", im2.size, 0) draw = ImageDraw.Draw(mask_im) draw.ellipse((150, 40, 250, 100), fill=255) mask_im.save('mask_circle.jpg', quality=95) back_im = im1.copy() back_im.paste(im2, (0, 0), mask_im) back_im.save('rocket_pillow_paste_mask_circle.jpg', quality=95) mask_im_blur = mask_im.filter(ImageFilter.GaussianBlur(10)) mask_im_blur.save('mask_circle_blur.jpg', quality=95) back_im = im1.copy() back_im.paste(im2, (0, 0), mask_im_blur) back_im.save('rocket_pillow_paste_mask_circle_blur.jpg', quality=95)
Advertisement
Answer
from PIL import Image, ImageDraw, ImageFilter im1 = Image.open('rocket.jpg') im2 = Image.open('lena.jpg') #height, width, channels = im1.shape width, height = im1.size print(height, width) # ![rocket_pillow_paste_out](data/dst/rocket_pillow_paste_out.jpg) mask_im = Image.new("L", im2.size, 0) draw = ImageDraw.Draw(mask_im) draw.ellipse((140, 50, 260, 170), fill=255) mask_im.save('mask_circle.jpg', quality=95) back_im = im1.copy() back_im.paste(im2, (0, 0), mask_im) back_im.save('rocket_pillow_paste_mask_circle.jpg', quality=95) # ![rocket_pillow_paste_mask_circle](data/dst/rocket_pillow_paste_mask_circle.jpg) mask_im_blur = mask_im.filter(ImageFilter.GaussianBlur(10)) mask_im_blur.save('mask_circle_blur.jpg', quality=95) back_im = im1.copy() back_im.paste(im2, (0, 0), mask_im_blur) back_im.save('rocket_pillow_paste_mask_circle_blur.jpg', quality=95) # ![rocket_pillow_paste_mask_circle_blur](data/dst/rocket_pillow_paste_mask_circle_blur.jpg)