congdc's picture
init
ca63cf1
from PIL import Image
def repeat_partent(image: Image, new_width: str, new_height: str):
'''
This function takes an image and repeats the partent in the image to fill the target_tile_size.
'''
width, height = image.size
new_width, new_height = int(new_width), int(new_height)
# calculate the width of every partent in the image
o = new_width//width
list_width = [width]*o
r = new_width%width
if r > 0:
list_width.append(r)
# calculate the height of every partent in the image
o = new_height//height
list_height = [height]*o
r = new_height%height
if r > 0:
list_height.append(r)
# create a new image with the new width and height
new_image = Image.new('RGB', (new_width, new_height))
w_start, h_start = 0, 0
for w in list_width:
for h in list_height:
if h < height or w < width:
new_image.paste(image.crop((0, 0, w, h)), (w_start, h_start))
else:
new_image.paste(image, (w_start, h_start))
h_start += h
w_start += w
h_start = 0
return new_image
# test the function
if __name__ == '__main__':
image = Image.open("input.png")
result = repeat_partent(image=image, new_width=2000, new_height=1000)
result.save("output.png")