-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmoteResizer.py
72 lines (60 loc) · 2.7 KB
/
EmoteResizer.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Takes an image and makes it compatible for a Discord emote.
This script takes a cropped image and turns it into
the appropriate size for use in a Discord emote, maximizing
the possible size in the 128pxx128px allowed by Discord
"""
__version__ = "1.4"
__author__ = "Marc Hoeltge"
import sys
import os
from wand.image import Image
from wand.display import display
try:
with Image(filename=sys.argv[1]) as img:
IMAGE_SIDE_LENGTH_PX = 128
#autocrop the image to get rid of transparent space outlining it
img.trim()
#determine if width is the longest side of the image
width_longest_side = True
if img.width < img.height:
width_longest_side = False
if width_longest_side:
#find the ratio that the short side will have to be scaled
scale_ratio = float(IMAGE_SIDE_LENGTH_PX) / img.width
new_height = int(round(scale_ratio * img.height))
#crop the image into the smallest size possible
img.resize(IMAGE_SIDE_LENGTH_PX, new_height)
#create a new blank image and make a composite with the cropped image
with Image(width=IMAGE_SIDE_LENGTH_PX, height=IMAGE_SIDE_LENGTH_PX) as new_img:
top = (IMAGE_SIDE_LENGTH_PX - new_height) / 2
new_img.composite(img, left=0, top=top)
if len(sys.argv) == 3:
new_img.format = "png"
if sys.argv[2][::-1][:3] == "gnp":
new_img.save(filename=sys.argv[2])
else:
new_img.save(filename=sys.argv[2] + ".png")
else:
new_img.format = "png"
new_img.save(filename="output.png")
#repeat the above code but in terms of the height being the longest side
else:
scale_ratio = float(IMAGE_SIDE_LENGTH_PX) / img.height
new_width = int(round(scale_ratio * img.width))
img.resize(new_width, IMAGE_SIDE_LENGTH_PX)
with Image(width=IMAGE_SIDE_LENGTH_PX, height=IMAGE_SIDE_LENGTH_PX) as new_img:
left = (IMAGE_SIDE_LENGTH_PX - new_width) / 2
new_img.composite(img, left=left, top=0)
if len(sys.argv) == 3:
new_img.format = "png"
if sys.argv[2][::-1][:3] == "gnp":
new_img.save(filename=sys.argv[2])
else:
new_img.save(filename=sys.argv[2] + ".png")
else:
new_img.format = "png"
new_img.save(filename="output.png")
except IOError:
print ("Not a valid file")