-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.py
More file actions
244 lines (201 loc) · 8.64 KB
/
Copy pathimage.py
File metadata and controls
244 lines (201 loc) · 8.64 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
'''
Program name: image.py
Born: August 13, 2018
Updated: August 26, 2018 16:30 (added a prompt to wm() for text color so it can stand out against any background)
Author: Robert D. Schoening, https://www.robertdschoening.com
Code and documentation: https://github.com/exbuf/Shrink
'''
# Functions-----------------------------------------------------------------------------------------
def printErrMsg(error): # Display input error messages and Help screen
print(" ")
print(" * * * * * * * * * * * * ERROR: "+error+ " * * * * * * * * * * * * * * * ")
print(" ")
print(" 1. TO SHRINK IMAGES:")
print(" =============================================================================================")
print(" EXAMPLE....... python image.py 600 400 s_")
print(" arg[1] arg[2] arg[3]")
print(" where... ")
print(" - arg[1] is the maximum desired horizontal size in pixels, in this example 600.")
print(" - arg[2] is the maximum desired vertical size in pixels, in this example 400.")
print(" - arg[3] is the text you want prepended to your shunken filename, in this example s_")
print(" Do not use these illegal Windows filename characters in arg[3]: \ / : * ? < > | ")
print(" ")
print(" ")
print(" 2. TO CONVERT IMAGES TO BLACK AND WHITE (bw):")
print(" ============================================================================================")
print(" EXAMPLE.......> python image.py bw ( ... Converted files will be prepended with 'bw_')")
print(" ")
print(" ")
print(" 3. TO ADD WATERMARK (wm) TEXT IN UPPER LEFT HAND CORNER:")
print(" ============================================================================================")
print(" EXAMPLE.......> python image.py wm ( ... then enter your text when prompted. ")
print(" Converted files will be prepended with 'wm_')")
print(" ")
print(" ")
print(" Please read the program documentation: https://github.com/exbuf/Shrink/blob/master/README.md")
print(" ")
print(" To get this Help screen anytime, type: 'python image.py help' OR 'python image.py'")
print(" ")
def black_white(fileList):
count = 0
notCount = 0
print("")
# process all files
for fileName in fileList:
start_time = time.time()
try:
im = Image.open(fileName)
print(fileName, im.format, im.size, im.mode)
im = im.convert('1')
outFile = 'bw_'+fileName
im.save(outFile)
print(outFile, im.format, im.size, im.mode)
count=count+1
print("--- %s seconds ---" % round((time.time() - start_time),3))
print("")
except IOError:
notCount=notCount+1
# summarize results of processing
print(str(count)+" files processed and "+str(notCount)+" files ignored.")
if(count==0):
print("0 files processed can be caused by illegal characters \ / : * ? < > | in the prefix argument.")
print("To get the Help screen, type this-----------> python image.py help")
print(" ")
print(" ")
print(" ")
print(" ")
#print("--- %s seconds ---" % (time.time() - start_time))
def shrink(fileList, prefix, size):
count = 0
notCount = 0
print("")
# process all files
for fileName in fileList:
try:
# start timer
start_time = time.time()
im = Image.open(fileName)
print(fileName, im.format, im.size, im.mode)
im.thumbnail(size) # thumbnail function maintains aspect ratio
outFile = prefix+fileName
im.save(outFile) # save shrunken file
print(outFile, im.format, im.size, im.mode)
count=count+1
print("--- %s seconds ---" % round((time.time() - start_time),3))
print("")
except IOError:
notCount=notCount+1
# summarize results of processing
print(str(count)+" files processed and "+str(notCount)+" files ignored.")
if(count==0):
print("")
print("0 files processed can be caused by illegal characters \ / : * ? < > | in the prefix argument.")
print("To get the Help screen, type this-----------> python image.py help")
print(" ")
print(" ")
print(" ")
print(" ")
def checkInputs():
if ( len(sys.argv) != 4):
printErrMsg("Wrong number of arguments ("+str(len(sys.argv)-1)+")")
exit()
try:
value = int(sys.argv[1])
except ValueError:
printErrMsg("arg[1] is not an integer")
exit()
try:
value = int(sys.argv[2])
except ValueError:
printErrMsg("arg[2] is not an integer")
exit()
if(int(sys.argv[1]) < 1 or int(sys.argv[2]) < 1):
printErrMsg("arg[1] and arg[2] must be integers 1 or greater")
exit()
# This works for .jpg and .png but not on black and white. So disallow images beginning with 'wm_'.
def wm(fileList):
pos=(10, 0)
wm_text = input("Enter your watermark text: ")
print("You entered: "+wm_text)
wm_font = input("Enter the font size: ")
print("You entered: "+wm_font)
wm_color = input("Enter the font color: ")
print("You entered: "+wm_color)
count = 0
notCount = 0
prefix = "wm_"
print(" ")
for fileName in fileList:
start_time = time.time()
if('bw_' in fileName):
print("Skipping "+fileName+" because 'wm' does\'t work on B/W images yet. Run 'wm' on color image first, then run 'bw'.")
continue
ext = os.path.splitext(fileName)[-1].lower()
if(ext == '.JPG' or ext == '.jpg' or ext == '.PNG' or ext == '.png'):
print(fileName)
photo = Image.open(fileName)
# make image editable
drawing = ImageDraw.Draw(photo)
black = (3, 8, 12)
font = ImageFont.truetype("arial.ttf", int(wm_font))
drawing.text(pos, wm_text, fill=wm_color, font=font)
outFile = 'wm_'+fileName
# photo.show()
photo.save(outFile)
count=count+1
print(outFile, photo.format, photo.size, photo.mode)
print("--- %s seconds ---" % round((time.time() - start_time),3))
print(" ")
else:
notCount=notCount+1
# summarize results of processing
print(str(count)+" files processed and "+str(notCount)+" files ignored.")
if(count==0):
print("0 files processed can be caused by illegal characters \ / : * ? < > | in the prefix argument.")
print("To get the Help screen, type this-----------> python image.py help")
print(" ")
print(" ")
print(" ")
print(" ")
exit()
# End Functions--------------------------------------------------------------------------------
# Main program---------------------------------------------------------------------------------
# imports
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import os, sys, time
from sys import exit # fixes bug in pyinstaller (for making standalone executable)
# start timer
#start_time = time.time()
# fileList is common to all functions()
fileList = os.listdir(os.getcwd())
# check for zero arguments (empty arguments causes checkIntegers() to get 'out of range' error)
if(len(sys.argv) == 1):
printErrMsg("Wrong number of arguments (0).")
exit()
# check for one argument--'help', so show help screen
if(str(sys.argv[1]) == 'HELP' or str(sys.argv[1]) == 'help'):
printErrMsg("None. This is the Help screen.")
exit()
# check for one argument--'bw', so call black_white() function
if(str(sys.argv[1]) == "bw" or str(sys.argv[1]) == "BW"):
black_white(fileList)
exit()
# check for one argument--'wm', so call wm() function
if(str(sys.argv[1]) == "wm" or str(sys.argv[1]) == "WM"):
wm(fileList)
exit()
# neither of the two legal one-argument inputs were found, so default to the shrink() function which requires 3 arguments
# first check for argument input errors:
checkInputs()
# no argument input errors found, so prepare the shrink() function arguments
size_x = int(sys.argv[1])
size_y = int(sys.argv[2])
prefix = str(sys.argv[3])
size = (size_x, size_y)
# call the shrink() function
shrink(fileList, prefix, size)
# exit program
#print("--- %s seconds ---" % round((time.time() - start_time),3))
exit()