-
Notifications
You must be signed in to change notification settings - Fork 0
/
img_utils.py
40 lines (33 loc) · 1.14 KB
/
img_utils.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
import os
def is_img(file):
"""checks whether given file exists, and is an image file
of supported format
Args:
file (str): path to file which will be examined
Returns:
True or False whether file exists and has extention
equal to supported image file extentions
"""
return os.path.isfile(file) and \
os.path.splitext(file)[1] in ['.jpg','.png', '.jpeg']
def sum_imgs(overlay, background, output_filename):
""" Pastes overlay onto background image and saves resulting
image file at output_filename
Args:
overlay (PIL.Image) : Image which will be pasted
on background
background (PIL.Image) : Image to be used as background
output_filename (str) : output file name with path
Returns:
saves output image as file at output_filename path
returns None
"""
# resize overlay to fit on background
overlay.thumbnail(background.size)
# create new Image of size background.size
transparent = Image.new('RGBA', background.size, (1, 1, 1, 1))
# paste both images in order
transparent.paste(background, (0, 0))
transparent.paste(overlay, (0, 0), mask=overlay)
# save resulting image
transparent.save(output_filename)