-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgprocessor.py
101 lines (77 loc) · 2.88 KB
/
imgprocessor.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
# sudo pip install tinypng
# sudo apt-get install python-imaging
import os
import getopt
import sys
import math
from PIL import Image, ImageDraw, ImageFont, ImageEnhance, ImageStat
from tinypng import shrink_file
# Required width for the images
BASE_WIDTH = 607
WATERMARK = "www.javahelps.com"
api_key = "" # Fill your tinypg developer key here
FONT = 'font.ttf'
def getOpacity(img):
# Calculate the opacity of watermark based on the image.
rgbMedian = ImageStat.Stat(img).median
totalAverage = sum(rgbMedian) / len(rgbMedian)
opacity = math.ceil(totalAverage * 25.00 / 255.00) / 100.00
return opacity
def add_watermark(img, text, angle=23):
watermark = Image.new('RGBA', img.size, (0, 0, 0, 0))
size = 2
n_font = ImageFont.truetype(FONT, size)
n_width, n_height = n_font.getsize(text)
while n_width + n_height < watermark.size[0]:
size += 2
n_font = ImageFont.truetype(FONT, size)
n_width, n_height = n_font.getsize(text)
draw = ImageDraw.Draw(watermark, 'RGBA')
draw.text(((watermark.size[0] - n_width) / 2,
(watermark.size[1] - n_height) / 2),
text, font=n_font)
watermarkOpacity = getOpacity(img)
watermark = watermark.rotate(angle, Image.BICUBIC)
alpha = watermark.split()[3]
alpha = ImageEnhance.Brightness(alpha).enhance(watermarkOpacity)
watermark.putalpha(alpha)
return Image.composite(watermark, img, watermark)
def resize(img):
# Width percent
widthPercent = (BASE_WIDTH / float(img.size[0]))
if widthPercent >= 1.0:
return img
# Expected height
height = int((float(img.size[1]) * float(widthPercent)))
# Resize it.
resized = img.resize((BASE_WIDTH, height), Image.ANTIALIAS)
return resized
# Let's parse the arguments.
opts, args = getopt.getopt(sys.argv[1:], 'd:')
# Set some default values to the needed variables.
directory = ''
# If an argument was passed in, assign it to the correct variable.
for opt, arg in opts:
if opt == '-d':
directory = arg
# We have to make sure that all of the arguments were passed.
if directory == '':
print('Invalid command line argument. -d [directory] is required')
# If an argument is missing exit the application.
exit()
# Iterate through every image given in the directory argument and resize it.
for image in os.listdir(directory):
print('Processing image ' + image)
imgPath = os.path.join(directory, image)
# Open the image file.
img = Image.open(imgPath)
# Add watermark
watermark = add_watermark(img, WATERMARK)
# Resize the imae
img = resize(watermark)
# Save it back to disk.
img.save(os.path.join(directory, image), quality=95)
# Compress image using tinypng.com
shrink_info = shrink_file(imgPath, api_key, imgPath)
print("Savings: {:.2%}".format(1 - shrink_info["output"]["ratio"]))
print('Batch processing complete.')