-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeavatars.py
122 lines (86 loc) · 4.1 KB
/
mergeavatars.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
#!/usr/bin/env python
"""
A simple utility to generate private chat avatars from two given images.
Usage: python mergeavatars.py firstavatar secondavatar folder
firstavatar, secondavatar: images of the avatars to combine. Must be in a graphical format
supported by imagemagick
folder: the resulting folder where the result would be put.
"""
__author__ = "Andrew Nenakhov"
__copyright__ = "Copyright 2020, redsolution OÜ"
__credits__ = ["Andrew Nenakhov"]
__license__ = "GNU AGPLv3"
__version__ = "0.1"
__maintainer__ = "Andrew Nenakhov"
__email__ = "[email protected]"
__status__ = "Development"
import argparse
import os
import sys
import random
import string
from hashlib import sha1
from wand.image import Image
# from wand.drawing import Drawing
from wand.color import Color
def hex_to_rgb(value):
""" converts hex color value to nice tuple of R, G and B integer components """
value = value.lstrip('#')
lv = len(value)
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
def mergeAvatars(avatarLeft, avatarRight, folder):
""" Just as expected, this function gets two images and merges them into a private chat avatar """
# with Image( filename = "results/" + avatarRight ) as compositeImage:
with Image(width=128, height=128, background=Color("#F0F0F0")) as compositeImage:
try:
with Image(filename=avatarLeft) as leftImage:
# cropping image if it is not square
leftImage.transform(resize='128x128^')
sizeX, sizeY = leftImage.size
deltaX = int((sizeX - 128) / 2)
deltaY = int((sizeY - 128) / 2)
with leftImage[(deltaX+16):(deltaX+80), (deltaY):(deltaY+128)] as cropped:
compositeImage.composite(cropped, left=0, top=0)
except:
# raise Exception("first avatar can't be opened")
sys.exit("First avatar file " + avatarLeft + " can't be opened or is not an image.")
try:
with Image(filename=avatarRight) as rightImage:
# cropping image if it is not square
rightImage.transform(resize='128x128^')
sizeX, sizeY = rightImage.size
deltaX = int((sizeX - 128) / 2)
deltaY = int((sizeY - 128) / 2)
with rightImage[(deltaX+48):(deltaX+112), (deltaY):(deltaY+128)] as cropped2:
compositeImage.composite(cropped2, left=64, top=0)
except:
# raise Exception("second avatar can't be opened")
sys.exit("Second avatar file " + avatarRight + " can't be opened or is not an image.")
# save temporary file to specified folder
temporaryFile = folder + "/" + ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(10)) + ".png"
compositeImage.save(filename=temporaryFile)
# get sha1 hash of temporary file
imageHash = sha1(open(temporaryFile, 'rb').read()).hexdigest() + ".png"
resultFile = folder + "/" + imageHash
# rename temporary file
os.rename(temporaryFile, resultFile)
# write filename to stdout
sys.stdout.write(imageHash + "\n")
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='A simple utility to generate avatars from two given images.')
parser.add_argument('FirstAvatar', metavar='first-avatar', type=str,
help='You must specify path to first avatar')
parser.add_argument('SecondAvatar', metavar='second-avatar', type=str,
help='You must specify path to second avatar')
parser.add_argument('Path', metavar='path', type=str,
help='You must specify path to store generated image')
args = parser.parse_args()
dst_folder = os.path.abspath(args.Path)
# check if destination folder exists, trying to create it
if not os.path.exists(dst_folder):
try:
os.mkdir(dst_folder)
except OSError as err:
sys.exit(err)
mergeAvatars(args.FirstAvatar, args.SecondAvatar, dst_folder)