File size: 821 Bytes
6dfde8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python3

# reads in a rectangular image, cuts it in half length-wise, and attaches the right half to the bottom, reversed

import sys 
from PIL import Image


def rect_to_square(img):
    # get width and height of img
    w, h = img.size
    new_img = Image.new(img.mode, (w//2, h*2))
    new_img.paste(img.crop((0, 0, w, h)), (0, 0))
    new_img.paste(img.crop((0, 0, w, h)).transpose(Image.FLIP_LEFT_RIGHT), (0, h))
    return new_img


if __name__ == '__main__':
    img_filename = sys.argv[1]
    in_img = Image.open(img_filename)
    print("Input image dimenions: ", in_img.size)
    out_img = rect_to_square(in_img)
    print("Output image dimenions: ", out_img.size)
    out_filename = img_filename.replace('.png', '_square.png')
    print("Saving to ", out_filename)
    out_img.save(out_filename)