-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagecropper.py
94 lines (85 loc) · 2.62 KB
/
imagecropper.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
from PIL import Image
import sys
import os
import getopt
def main():
cmd_args = sys.argv[1:]
folder = False
target = ""
dimensions = (1,3)
try:
opts, args = getopt.getopt(cmd_args, "hf:d:", ["help","folder=","dimensions="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt , arg in opts:
if opt in ("-h","--help"):
usage()
sys.exit(2)
elif opt in ("-f","--folder"):
folder = True
target = arg
elif opt in ("-d","--dimensions"):
[height,width] = arg.replace("(","").replace(")","").split(",")
dimensions=(int(height),int(width))
if folder == False:
target = "".join(args)
print("file: ",target)
splitImage(target,dimensions)
else:
print("folder: ",target)
folderHandler(target,dimensions)
def usage():
print("********USAGE NOTES********")
print("-f or --folder <directory>: specify the folder of files to process")
print("-d or --dimenstions <(rows,columns)>: specify the grid of squares to create (default is (1,3))")
def splitImage(file,dim):
print("Will split", file)
splitF = file.split("\\")
fileNameWType = splitF[len(splitF)-1]
[fileName,_type] = fileNameWType.split(".")
im = Image.open(file)
(width,height)=im.size
(nRows,nCols)=dim
boxWidth=width/nCols
boxHeight=height/nRows
"""
Initial implementation takes 3 boxes across
"""
nRowsComplete = 0
while nRowsComplete < nRows:
nColsComplete = 0
x1 = 0
y1 = 0+(nRowsComplete*boxHeight)
x2 = boxWidth
y2 = boxHeight+(nRowsComplete*boxHeight)
while nColsComplete < nCols:
nColsComplete+=1
newCrop = im.crop((x1,y1,x2,y2))
newCrop.save(fileName+"Crop"+str(nRowsComplete)+""+str(nColsComplete)+"."+_type)
x1+=boxWidth
x2+=boxWidth
nRowsComplete+=1
print(fileName,"cropped")
def folderHandler(folder,dim):
print("Will split all images in",folder)
os.chdir(folder)
files = os.listdir()
for file in files:
split_name = file.split(".")
file_type = split_name[len(split_name)-1]
if isImage(file_type):
splitImage(file,dim)
print("All images found in",folder,"have been cropped")
def isImage(file_type):
file_type=file_type.lower()
if (file_type == "jpg") | (file_type == "png") | (file_type == "gif" ):
return True
return False
if __name__ == "__main__":
main()