-
Notifications
You must be signed in to change notification settings - Fork 0
/
MakePerfect.py
151 lines (129 loc) · 5.16 KB
/
MakePerfect.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
import datetime
import requests
import io
import random
import os
import config as conf
from discord_webhook import DiscordWebhook
from PIL import Image, ImageFilter, ImageDraw, ImageFont, ImageColor
import sys
sys.path.append('..')
#
W, H = (512, 512)
def calculate_brightness(image):
greyscale_image = image.convert('L')
histogram = greyscale_image.histogram()
pixels = sum(histogram)
brightness = scale = len(histogram)
for index in range(0, scale):
ratio = histogram[index] / pixels
brightness += ratio * (-scale + index)
return 1 if brightness == 255 else brightness / scale
def random_color(bottomBound, upperBound):
random_number = random.randint(bottomBound, upperBound)
hex_number = str(hex(random_number))
hex_number = '#' + hex_number[2:]
valid_hex = False
while (not valid_hex):
if len(hex_number) < 4 or len(hex_number) < 7:
hex_number = hex_number + str(0)
else:
valid_hex = True
return hex_number
def generateIcon(drawModeRandom=True, drawMode=2, drawTextMode=2, lw="LW", sendToDiscord=True, debug=False):
dir = os.path.dirname(__file__)
if (drawModeRandom):
drawMode = random.randint(1, 2)
drawTextMode = random.randint(1, 2)
while 1:
if len(os.listdir(dir + "/props/bg/")) == 0:
drawMode == 2
else:
rand_imagebg = dir + "/props/bg/" + \
random.choice(os.listdir(dir + "/props/bg/"))
print(rand_imagebg)
if drawMode == 2:
rand_imagebg = io.BytesIO(requests.get(
"https://source.unsplash.com/random").content)
img1 = Image.open(rand_imagebg).convert('RGBA')
w, h = img1.size
if w >= 512 and h >= 512:
break
rand_x = random.randint(0, w - 512)
rand_y = random.randint(0, h - 512)
#cropped = img1.crop((0, 0, 512, 512))
cropped = img1.crop((rand_x, rand_y, rand_x + 512, rand_y + 512))
filters = [ImageFilter.GaussianBlur(radius=5), ImageFilter.BLUR,
ImageFilter.CONTOUR, ImageFilter.DETAIL, ImageFilter.EDGE_ENHANCE,
ImageFilter.EDGE_ENHANCE_MORE,
ImageFilter.SHARPEN, ImageFilter.SMOOTH, ImageFilter.SMOOTH_MORE,
ImageFilter.UnsharpMask(radius=2, percent=150, threshold=3),
# Disable ImageFilter.EMBOSS by default
ImageFilter.RankFilter(3, 0),
ImageFilter.MedianFilter(size=3)]
#img1 = cropped.filter(ImageFilter.GaussianBlur(radius=5))
random_filter = random.choice(filters)
img1 = cropped.filter(random_filter)
img1 = img1.filter(ImageFilter.GaussianBlur(radius=5))
if drawTextMode == 1:
rand_imagetext = dir + "/props/text/" + \
random.choice(os.listdir(dir + "/props/text/"))
img2 = Image.open(rand_imagetext).convert('RGBA')
final1 = Image.alpha_composite(img1, img2)
if drawTextMode == 2:
randFont = dir + "/fonts/" + random.choice(os.listdir(dir + "/fonts/"))
print(randFont)
myFont = ImageFont.truetype(randFont, 250)
draw = ImageDraw.Draw(img1)
w, h = draw.textsize(lw, font=myFont)
i = 0
while(w + 30 > W):
i = i + 1
myFont = ImageFont.truetype(randFont, 250 - i)
draw = ImageDraw.Draw(img1)
w, h = draw.textsize(lw, font=myFont)
while(w < W - 70):
i = i + 1
myFont = ImageFont.truetype(randFont, 250 + i)
draw = ImageDraw.Draw(img1)
w, h = draw.textsize(lw, font=myFont)
#print(str(W) + " " + str(w) + " " + str(H) + " " + str(h))
bright_bg = calculate_brightness(img1)
print(bright_bg)
if bright_bg > 0.8:
t_color = random_color(10777215, 16777215) # "#aaa" 8388600
t_back_color = "#777"
else:
t_color = random_color(0, 4388600) # "white"
t_back_color = "#555"
draw.text(((W - w) / 2 + 5, (H - h) / 2 + 5),
lw, fill=t_back_color, font=myFont)
draw.text(((W - w) / 2, (H - h) / 2), lw, fill=t_color, font=myFont)
final1 = img1
print(t_color)
final1.save(dir + '/out/out.png')
if not sendToDiscord:
final1.show()
if(drawTextMode == 2):
info = str(datetime.datetime.now()) + " \nUsed filter: " + str(random_filter) + "\nUsed font: " + str(randFont) \
+ "\nGlobal bright level: " + \
str(bright_bg) + "\nUsed text color: " + str(t_color)
else:
info = str(datetime.datetime.now()) + " \nUsed filter: " + \
str(random_filter) + "\nUsed text-prop: " + rand_imagetext
if sendToDiscord:
if debug == False:
info = ""
webhook = DiscordWebhook(url=conf.webhook_url,
content=info)
b = io.BytesIO()
final1.save(b, "PNG")
b.seek(0)
webhook.add_file(file=b.read(), filename='logo.png')
response = webhook.execute()
print(response)
# with open('image.jpg', 'rb') as f:
#icon = f.read()
# await bot.edit_server(ctx.message.server, icon=icon)
print(info)
# outimg.save('out/out.png')