File size: 1,074 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
28
29
30
31
32
#!/usr/bin/env python3

# reads in a square image and outputs a rectangular image

import sys 
from PIL import Image

def flip_bottom_half_and_attach(img):
    "takes one 256x256 and returns on 512x128 image with the bottom half reversed and attached on the right"
    #w, h = 256, 256
    h, w = img.size
    #assert sub_img.size[0] == h and sub_img.size[1] == w
    new_img = Image.new(img.mode, (w*2, h//2))
    new_img.paste(img.crop((0, 0, w, h//2)), (0, 0))
    new_img.paste(img.crop((0, h//2, w, h)).transpose(Image.FLIP_LEFT_RIGHT), (w, 0))
    return new_img 

def square_to_rect(img):
    # just an alias for flip_bottom_half_and_attach
    return flip_bottom_half_and_attach(img)


if __name__ == '__main__':
    img_filename = sys.argv[1]
    square_img = Image.open(img_filename)
    print("Input image dimenions: ", square_img.size)
    rect_img = square_to_rect(square_img)
    print("Output image dimenions: ", rect_img.size)
    out_filename = img_filename.replace('.png', '_rect.png')
    print("Saving to ", out_filename)
    rect_img.save(out_filename)