2016-01-24 20:48:39 +00:00
|
|
|
from PIL import Image
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2016-02-21 07:13:50 +00:00
|
|
|
close_enough_threshold = 90
|
|
|
|
filename = sys.argv[1]
|
|
|
|
try:
|
|
|
|
close_enough_threshold = int(sys.argv[2])
|
|
|
|
except:
|
|
|
|
pass
|
2016-01-24 20:48:39 +00:00
|
|
|
|
|
|
|
def close_enough(a, b):
|
|
|
|
for (a_channel, b_channel) in zip(a, b):
|
2016-02-21 07:13:50 +00:00
|
|
|
if abs(a_channel - b_channel) > close_enough_threshold:
|
2016-01-24 20:48:39 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def deletterbox(filename):
|
|
|
|
image = Image.open(filename)
|
|
|
|
trim_top(image)
|
|
|
|
for x in range(4):
|
|
|
|
image = trim_top(image)
|
|
|
|
image = image.rotate(90)
|
2016-02-21 07:13:50 +00:00
|
|
|
#(base, ext) = os.path.splitext(filename)
|
2016-01-24 20:48:39 +00:00
|
|
|
#filename = base + 'X' + ext
|
2016-02-21 07:13:50 +00:00
|
|
|
image.save(filename, quality=100)
|
2016-01-24 20:48:39 +00:00
|
|
|
|
|
|
|
def trim_top(image):
|
|
|
|
letterbox_color = image.getpixel((0, 0))
|
|
|
|
for y in range(image.size[1]):
|
|
|
|
solid = True
|
|
|
|
for x in range(image.size[0]):
|
|
|
|
pixel = image.getpixel((x, y))
|
2016-02-21 07:13:50 +00:00
|
|
|
#print(pixel)
|
2016-01-24 20:48:39 +00:00
|
|
|
if not close_enough(letterbox_color, pixel):
|
|
|
|
solid = False
|
2016-02-21 07:13:50 +00:00
|
|
|
#print(y,pixel)
|
2016-01-24 20:48:39 +00:00
|
|
|
break
|
|
|
|
if not solid:
|
|
|
|
break
|
|
|
|
bounds = (0, y, image.size[0], image.size[1])
|
2016-02-21 07:13:50 +00:00
|
|
|
print(bounds)
|
2016-01-24 20:48:39 +00:00
|
|
|
image = image.crop(bounds)
|
|
|
|
return image
|
|
|
|
|
2016-02-21 07:13:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
deletterbox(filename)
|
2016-01-24 20:48:39 +00:00
|
|
|
|