Rewrite shrink_and_paste_on_blank function

pull/26/head
Jonas Klesen 2023-04-17 00:48:36 +02:00
parent c4c1b91349
commit b20bc1f7d8
1 changed files with 12 additions and 21 deletions

View File

@ -1,7 +1,4 @@
import requests
from PIL import Image
import numpy as np
def shrink_and_paste_on_blank(current_image, mask_width, mask_height):
"""
@ -10,23 +7,17 @@ def shrink_and_paste_on_blank(current_image, mask_width, mask_height):
so that the image the function returns is the same size as the input.
:param current_image: input image to transform
:param mask_width: width in pixels to shrink from each side
:param mask_height: height in pixels to shrink from each side
"""
height = current_image.height
width = current_image.width
# calculate new dimensions
width, height = current_image.size
new_width = width - 2 * mask_width
new_height = height - 2 * mask_height
# shrink down by mask_width
prev_image = current_image.resize(
(width-2*mask_width, height-2*mask_height))
prev_image = prev_image.convert("RGBA")
prev_image = np.array(prev_image)
# resize and paste onto blank image
prev_image = current_image.resize((new_width, new_height))
blank_image = Image.new("RGBA", (width, height), (0, 0, 0, 1))
blank_image.paste(prev_image, (mask_width, mask_height))
# create blank non-transparent image
blank_image = np.array(current_image.convert("RGBA"))*0
blank_image[:, :, 3] = 1
# paste shrinked onto blank
blank_image[mask_height:height-mask_height,mask_width:width-mask_width :] = prev_image
prev_image = Image.fromarray(blank_image)
return prev_image
return blank_image