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 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| from PIL import Image, ImageColor
im = Image.open("exemple.jpg")
x, y = im.size
mask = (75, 79, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,
97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 147, 148, 149,
152, 218, 219, 220, 221, 222, 223, 224)
lng = []
imdata = []
for value in list(im.getdata()):
if isinstance(value, tuple):
value = (value[0]*299 + value[1]*587 + value[2]*114)/1000
lng.append(value)
if len(lng) == x:
imdata.append(lng)
lng = []
istrue = [(index, all([l[index] in mask for l in imdata])) for index in range(x)]
# Pas terrible : a revoir.
flag = False
Start = None
End = None
for idx, item in istrue:
if not item:
if End:
break
flag = True
if all((item, flag)):
if Start:
End = idx
else:
Start = idx
tocut = Start + (End-Start)/2
crop1 = im.crop((0, 0, tocut, y))
crop2 = im.crop((tocut, 0, x, y))
im1 = Image.new("RGB", (tocut, y), "white")
im2 = Image.new("RGB", (x-tocut, y), "white")
im1.paste(crop1, (0, 0, tocut, y))
im2.paste(crop2, (0, 0, x-tocut, y))
im1.save("test1.jpg", "JPEG")
im2.save("test2.jpg", "JPEG") |