forked from chickens2/pony-comic-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindEmote.py
138 lines (128 loc) · 3.68 KB
/
findEmote.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
# coding=UTF-8
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# vim: set fileencoding=UTF-8 :
import json
from pprint import pprint
import os
import random
import urllib
from PIL import Image,ImageFont,ImageDraw
import cacher
import ConfigParser
config = ConfigParser.ConfigParser()
config.readfp(open('config.cfg'))
#
defaultSeed=config.get('Options','default_seed') #RAND0m_XD or something or whatever
MIN_LENGTH=config.getint('Options','emotional_diversity') #minimum number of emotes for a tag to be valid
emotesByName={}
emotesByPony={}
BANNED_TAGS=config.get('Ignore','banned_tags').split()
emoteMetadata={}
for file in os.listdir('tagAssignments'):
#print 'tagassignments file:'+file
with open('tagAssignments/'+file) as data_file:
data = json.load(data_file)
emoteMetadata.update(data)
for fn in os.listdir('emotes'):
data=None
with open('emotes/'+fn) as data_file:
data = json.load(data_file)
for key in data.keys():
value=data[key]
if 'Emotes' not in value or '' not in value['Emotes']:
del data[key]
continue
if 'Image' in value['Emotes']['']:
if 'amazonaws' in value['Emotes']['']['Image']:
del data[key]
continue
else:
del data[key]
if 'Offset' in value['Emotes']['']:
offset=value['Emotes']['']['Offset']
if offset[0]>0 or offset[1]>0:
del data[key]
else:
offset[0]=offset[0]*-1
offset[1]=offset[1]*-1
emotesByName.update(data)
for fn in os.listdir('tags'):
data=None
with open('tags/'+fn) as data_file:
data = json.load(data_file)
for key, value in data.iteritems():
if key in emotesByName and len(value)==1:
emotes=set()
#print 'tag: '+value[0]
if value[0][1:] not in BANNED_TAGS:
if value[0] not in emotesByPony:
emotesByPony[value[0]]=emotes
else:
emotes=emotesByPony[value[0]]
emotes.add(key)
for key in emotesByPony.keys():
if len(emotesByPony[key])<MIN_LENGTH:
del emotesByPony[key]
#pprint(emotesByPony)
#
def getProceduralPony(seed):
random.seed(seed)
pony=random.choice(emotesByPony.keys())
random.seed()
return pony
#
def getProceduralEmote(seed1,seed2):
return getRandomEmote(seed2,getProceduralPony(seed1))
#
def getEmote(emoteName):
data=emotesByName[emoteName]
url=data['Emotes']['']['Image']
offset=[0,0]
if 'Offset' in data['Emotes']['']:
offset=data['Emotes']['']['Offset']
#offset[0]=offset[0]*-1
#offset[1]=offset[1]*-1
#if offset[0]<0 or offset[1]<1:
# return None
size=data['Emotes']['']['Size']
print 'emotename: '+emoteName
pprint(data)
print url
if 'http:' not in url:
url='http:'+url
imgloc=cacher.getUrlFile(url)
fullImage=Image.open(imgloc).convert('RGBA')
#urllib.urlretrieve(url,'temp.png')
#fullImage=Image.open("temp.png").convert('RGBA')
print offset
print size
fullImage=fullImage.crop((offset[0],offset[1],offset[0]+size[0],offset[1]+size[1]))
if emoteName in emoteMetadata:
data=emoteMetadata[emoteName]
if 'right' in data:
print 'findemote flipping image'
fullImage=fullImage.transpose(Image.FLIP_LEFT_RIGHT)
return fullImage
#
def getRandomEmote(seed,pony=None):
global defaultSeed
if len(seed)<1:
seed=defaultSeed
if pony is None:
pony=random.choice(emotesByPony.keys())
print pony
emote=None
emoteNames=list(emotesByPony[pony])
#while emote is None and len(emoteNames)>0:
print 'randomly choosing emote from list of '+str(len(emoteNames))+" with seed "+str(seed)
random.seed(seed)
emoteName=random.choice(emoteNames)
print 'selected emote: '+emoteName
random.seed()
emote=getEmote(emoteName)
#emoteNames.remove(emote)
return emote
# Other code
#image=getProceduralEmote("pondy")
#image.show()