-
Notifications
You must be signed in to change notification settings - Fork 3
/
filenamecleaner.py
127 lines (104 loc) · 4.03 KB
/
filenamecleaner.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
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
# no copyright - 5/9/2018 [email protected]
# remove special characters, spaces, EC and GX designation from imagefile.
import os
import shutil
deletedfiles = []
global masterlist
basepath = './images/raw_form/245x342_TCG_images_mixed/'
processedpath = './images/processed/245x342_TCG_images_mixed/'
# the word alolan from the name
def removealolan(filename):
#print 'checking {} for variations of alolan'.format(filename)
if filename.find('alolan ') > -1:
filename = filename.replace('alolan ', '')
if filename.find('ALOLAN ') > -1:
filename = filename.replace('ALOLAN ', '')
if filename.find('Alolan ') > -1:
filename = filename.replace('Alolan ', '')
return filename
# break versoin of cards will only confuse the training since they are usually horizontally oriented and all gold
def checkforbreak(filename):
result = False
if filename.find('BREAK') > -1:
result = True
os.remove(basepath + filename)
deletedfiles.append('{} was deleted as it was a break version of the pokemon and not suited for training'
.format(filename))
if filename.find('LEGEND') > -1:
result = True
os.remove(basepath + filename)
deletedfiles.append('{} was deleted as it was a LEGEND version of the pokemon and not suited for training'
.format(filename))
return result
# remove and GX of EX initials
def removeGXEX(filename):
if filename.find('GX') > -1:
filename = filename.replace('GX', '')
if filename.find('EX') > -1:
filename = filename.replace('EX', '')
if filename.find('[G]') > -1:
filename = filename.replace('[G]', '')
if filename.find(' ex') > -1:
filename = filename.replace(' ex', '')
if filename.find('FB') > -1:
filename = filename.replace('FB', '')
if filename.find('Ash') > -1:
filename = filename.replace('Ash', '')
if filename.find("Ash's ") > -1:
filename = filename.replace("Ash's ", '')
if filename.find("Dark ") > -1:
filename = filename.replace("Dark ", '')
if filename.find("Black ") > -1:
filename = filename.replace("Black ", '')
if filename.find("Team Aqua's ") > -1:
filename = filename.replace("Team Aqua's ", '')
if filename.find("Ultra ") > -1:
filename = filename.replace('Ultra ', '')
return filename
def clean(filename):
newarray = []
# remove GX or EX
filename = removeGXEX(filename)
# remove alolan
filename = removealolan(filename)
# split at the underscore and ignore anything AFTER the underscore
if filename.find('_') > -1:
parts = filename.split('_')
underscore = parts[0]
else:
underscore = filename
#split at the first SPACE and ignore anyting AFTER the space
if underscore.find(' ') > -1:
parts = underscore.split(' ')
chararray = list(parts[0])
else:
chararray = list(underscore)
# check for special characters first
for letter in chararray:
#print 'letter {} = ASCII {}'.format(letter, ord(letter))
if 65 <= ord(letter) <= 90: # A-Z - acceptable
newarray.append(letter)
else:
if 97 <= ord(letter) <= 122: # a-z - acceptable
newarray.append(letter)
newname = ''.join(newarray)
return newname
toprocess = os.listdir(basepath)
masterlist = []
# testlimit = 1000
# counter = 1
for f2p in toprocess:
# if counter == testlimit:
# break
if checkforbreak(f2p) == False:
cleanname = clean(f2p)
# add this name to the master list
masterlist.append(cleanname)
# count the number of instances this name is in the list
occ = masterlist.count(cleanname)
# rename the file appropriately
cleanname = cleanname + '_' + str(occ) + '_p.png'
print '{} becomes {}'.format(f2p, cleanname)
shutil.copy(basepath + f2p, processedpath)
os.rename(processedpath + f2p, processedpath + cleanname)
# counter = counter + 1