forked from 2000prath/Dynamic-Avatar-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavatar.py
91 lines (81 loc) · 3.13 KB
/
avatar.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
__author__ = '2000prath@gmail.com'
from PIL import Image,ImageEnhance,ImageOps
import numpy as np
import random
import time
class AvatarGenerator:
"""
obj = AvatarGenerator()\n
img = obj.CreateAvatar(5,300,'lightgrey')\n
str_bs64 = obj.toBase64(img)\n
img.show() #use to open image\n
img.save('image.png') #to save image\n
"""
def toBase64(self,img):
import base64
from io import BytesIO
buffered = BytesIO()
img.save(buffered, format="PNG")
img_base64 = base64.b64encode(buffered.getvalue())
return img_base64
def getMatrix(self,x,y):
#don't use more than 10 seconds to generate matrix
wait_till = time.time() + 10
ones = 0
zeros = 1
array=None
while(ones<zeros and time.time() < wait_till):
array = np.random.randint(2, size=(x, y))
for i in range(len(array)):
for j in range(len(array[i])):
if array[i][j]:
ones+=1
else:
zeros+=1
for i in range(len(array)):
if array[i][1]==1:
ones+=1
else:
zeros+=1
if array[i][0]==1:
ones+=1
else:
zeros+=1
array[i][len(array)-2] = array[i][1]
array[i][len(array)-1] = array[i][0]
return array
def CreateAvatar(self,xy_axis=5,pixels=300,background_color='lightgrey',border=True,border_width=25):
"""
@xy_axis : Row and Columns, it should be same Eg. 5 for(5*5 matrix)\n
@pixels : Use less pixels to minimize final image siz Eg. 300 e\n
@background_color : Specify the color of background Eg. white, grey, lighgrey\n
@border : Boolean - True: add border, False: no border\n
@border_width : size
"""
#to get the image x and y should be same
column_row = xy_axis
pixel_x_y = pixels
#create blank image
img = Image.new('RGB', (pixel_x_y, pixel_x_y), background_color)
#get matrix to fill blocks
array = self.getMatrix(column_row,column_row)
#generate colors RGB
red = random.randint(0,255)
green = random.randint(0,255)
blue = random.randint(0,255)
#generate dark color if RGB is light color
while(red+green+blue > 400):
red = random.randint(0,255)
green = random.randint(0,255)
blue = random.randint(0,255)
for i in range(len(array)):
for j in range(len(array[i])):
if array[i][j] == 1:
right = int(j*(pixel_x_y/column_row))
top = int(i*(pixel_x_y/column_row))
left = int((j*(pixel_x_y/column_row))+(pixel_x_y/column_row))
bottom = int((i*(pixel_x_y/column_row))+(pixel_x_y/column_row))
img.paste( (red,green,blue), [right ,top,left,bottom])
if border:
img = ImageOps.expand(img,border=border_width,fill=background_color)
return img