-
Notifications
You must be signed in to change notification settings - Fork 0
/
cutter.py
executable file
·63 lines (50 loc) · 1.28 KB
/
cutter.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
# Simple bottom cutter for G3 TIFFs created by PAPS + Ghostscript
#
# by Magnetic-Fox, 02.08.2024
#
# (C)2024 Bartłomiej "Magnetic-Fox" Węgrzyn!
import PIL.Image
import sys
# default
leave=94
def checkLine(image, pixels, lineNumber):
for i in range(image.width):
if pixels[i,lineNumber] != 255:
return False
return True
def bottomEnd(image, pixels):
for i in range(image.height):
if not checkLine(image,pixels,image.height-i-1):
return image.height-i-1
return -1
def cuttingPossibleFormula(botEnd, imgHeight, leave=leave):
return (botEnd!=-1) and (botEnd+leave<imgHeight)
def cuttingPossible(image, pixels, leave=leave):
botEnd=bottomEnd(image,pixels)
return cuttingPossibleFormula(botEnd,image.height,leave)
def cutBottom(image, pixels, leave=leave):
botEnd=bottomEnd(image,pixels)
if cuttingPossibleFormula(botEnd,image.height,leave):
return image.crop((0,0,image.width,botEnd+leave))
else:
return None
def setLeave(newLeave):
global leave
leave=newLeave
return
def getLeave():
global leave
return leave
def loadAndCrop(filename):
img=PIL.Image.open(filename)
pxl=img.load()
img=cutBottom(img,pxl)
img.save(filename)
return
# Main code goes here
if __name__ == "__main__":
if len(sys.argv)==2:
loadAndCrop(sys.argv[1])
else:
exit(1)