forked from marianabianca/pancake_studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
182 lines (135 loc) · 4.36 KB
/
functions.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
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
from PIL import Image, ExifTags, ImageFont, ImageDraw, ImageFilter
import numpy as np
import random
import os.path, time
from blend_modes import soft_light as sfl
from blend_modes import overlay as ovl
# Applies grain effect to image
def grain_img(img, percentage):
img_arr = np.array(img)
grain(img_arr, pixel_sample(img.size, percentage))
return Image.fromarray(img_arr)
# Applies grains on specified pixels image matrix
def grain(arr, pixels, min_br=10, max_br=40):
for x, y in pixels:
arr[x, y] = bright_pixel(arr[x, y], min_br, max_br)
# Determines the pixels that will be brighter
def pixel_sample(size, percentage):
for i in range(size[0]):
for j in range(size[1]):
if random.random() < percentage:
yield (i, j)
# Makes a pixels brighter in a random way
def bright_pixel(pixel, min_br, max_br):
br = random.randint(min_br, max_br)
return tuple(min(255, ch + br) for ch in pixel)
# Resizes image
def resize(img, max_height, max_width):
img_aux = img
img_arr = np.array(img_aux)
height = len(img_arr)
width = len(img_arr[0])
ratio = 1
if height > max_height or width > max_width:
if max_width > max_height:
ratio = max_height / height
else:
ratio = max_width / width
new_height = height * ratio
new_width = width * ratio
size = new_height, new_width
img_aux.thumbnail(size)
return img_aux
# Crops image
def crop(img, height, width):
img_arr = np.array(img)
img_width = len(img_arr[0])
img_height = len(img_arr)
center_width = img_width / 2
center_height = img_height / 2
if width > img_width:
width = img_width
if height > img_height:
height = img_height
init_width = int(center_width - (width/2))
end_width = int(init_width + width)
init_height = int(center_height - (height/2))
end_height = int(init_height + height)
img_arr = img_arr[init_height:end_height, init_width:end_width]
img = Image.fromarray(img_arr)
print(height, len(img_arr), width, len(img_arr[0]))
return img
# Overlays RGBA images
def soft_light(background, overlay, opacity):
# Converts images to arrays of float type so
# the blend modes module can work
background_arr = np.array(background).astype(float)
overlay_arr = np.array(overlay).astype(float)
# Blend the images using soft light mode
final_img_arr = sfl(background_arr, overlay_arr, opacity)
# Transforms array of floats to array of unsigned integer
# between 0 and 255
final_img_arr = np.uint8(final_img_arr)
# Transforms array to image
final_img = Image.fromarray(final_img_arr)
return final_img
def get_exif(im):
try:
im_exif = im._getexif().items()
except:
return False
exif = {}
for (tag, value) in im_exif:
if tag in ExifTags.TAGS:
exif[ExifTags.TAGS[tag]] = value
return exif
def get_img_date(path):
im = Image.open(path)
exif = get_exif(im)
if exif:
date = exif['DateTime'].split(' ')[0]
else:
date = time.strftime(
'%Y/%m/%d',
time.gmtime(os.path.getmtime(path))
)
year = str(date[2:4])
month = str(date[5:7])
day = str(date[8:10])
return {'day': day, 'month': month, 'year': year}
def write_on_img(im, text, font, size, position, color):
copy_im = im.copy()
draw = ImageDraw.Draw(copy_im)
font = ImageFont.truetype(font, size)
draw.text(position, text, color, font=font)
return copy_im
def blank_img(size, color):
im = Image.new('RGBA', size, color)
return im
def transparent_img(size):
color = (0, 0, 0, 0)
im = blank_img(size, color)
return im
def brazilian_date_format(date):
text = date['day'] + ' ' + date['month'] + " '" + date['year']
return text
def write_br_date(im, date):
font_color = (255, 122, 56)
h, w = im.size[0], im.size[1]
size = int(h/25)
x, y = int(h - h/25 - size*5), int(w - w/25 - size)
position = (x, y)
im_date = write_on_img(im, date, 'digital-7.ttf', size, position, font_color)
im_date = im_date.filter(ImageFilter.GaussianBlur(3))
im_date = write_on_img(im_date, date, 'digital-7.ttf', size, position, font_color)
return im_date
def date_overlay(path):
im = Image.open(path)
size = im.size
t_im = transparent_img(size)
date = get_img_date(path)
str_date = brazilian_date_format(date)
date_overlay = write_br_date(t_im, str_date)
im = im.convert('RGBA')
final = soft_light(im, date_overlay, 1)
return final