Browse Source

croppie

master
Merlijn Wajer 5 years ago
parent
commit
07da6cd4ab
1 changed files with 45 additions and 0 deletions
  1. +45
    -0
      crop.py

+ 45
- 0
crop.py View File

@@ -0,0 +1,45 @@
import sys
from os.path import join

import numpy
from PIL import Image

py3map = map
map = lambda *args, **kwargs: list(py3map(*args, **kwargs))

if __name__ == '__main__':
spec = sys.argv[1]
out = sys.argv[2]

print('jean luc control')

for line in open(spec, 'r').readlines():
l = line.split(',')
fname, coords = l[0], map(int, l[1:])

i = Image.open(fname)

w = max(coords[0::2]) - min(coords[0::2])
h = max(coords[1::2]) - min(coords[1::2])
mw = (max(coords[0::2]) + min(coords[0::2])) / 2
mh = (max(coords[1::2]) + min(coords[1::2])) / 2

data = list(zip(coords[::2], coords[1::2]))
def coord_order(coord):
x, y = coord
score = 0
score += 2 * int(x > mw)
score += int(y > mh)
return score

data = list(sorted(data, key=coord_order))

tl, bl, tr, br = data
data = [tl, bl, br, tr]
data = sum(map(list, data), [])

ii = i.transform((w, h), Image.QUAD, data=data, resample=Image.BILINEAR)
ii.save(join(out, fname))

del i
del ii

Loading…
Cancel
Save