This repository has been archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcopyMons.py
143 lines (106 loc) · 5.06 KB
/
copyMons.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
import cv2
import os
import numpy as np
import imutils
import glob, os
import os.path
import logging
import json
from shutil import copyfile
from PIL import Image
from walkerArgs import parseArgs
from db.dbWrapper import DbWrapper
log = logging.getLogger(__name__)
args = parseArgs()
class MonRaidImages(object):
@staticmethod
def copyMons(pogoasset):
monList = []
log.info('Processing Pokemon Matching....')
with open('raidmons.json') as f:
data = json.load(f)
monImgPath = os.getcwd() + '/mon_img/'
filePath = os.path.dirname(monImgPath)
if not os.path.exists(filePath):
log.info('mon_img directory created')
os.makedirs(filePath)
assetPath = pogoasset
if not os.path.exists(assetPath):
log.error('PogoAssets not found')
exit(0)
for file in glob.glob(monImgPath + "*mon*.png"):
os.remove(file)
for mons in data:
for mon in mons['DexID']:
lvl = mons['Level']
if str(mon).find("_") > -1:
mon_split = str(mon).split("_")
mon = mon_split[0]
frmadd = mon_split[1]
else:
frmadd = "00"
mon = '{:03d}'.format(int(mon))
monList.append(mon)
monFile = monImgPath + '_mon_' + str(mon) + '_' + str(lvl) + '.png'
if not os.path.isfile(monFile):
monFileAsset = assetPath + '/pokemon_icons/pokemon_icon_' + str(mon) + '_' + frmadd + '.png'
if not os.path.isfile(monFileAsset):
log.error('File ' + str(monFileAsset) + ' not found')
exit(0)
copyfile(monFileAsset, monFile)
image = Image.open(monFile)
image.convert("RGBA")
canvas = Image.new('RGBA', image.size, (255,255,255,255)) # Empty canvas colour (r,g,b,a)
canvas.paste(image, mask=image) # Paste the image onto the canvas, using it's alpha channel as mask
canvas.save(monFile, format="PNG")
monAsset = cv2.imread(monFile,3)
height, width, channels = monAsset.shape
monAsset = cv2.inRange(monAsset,np.array([240,240,240]),np.array([255,255,255]))
cv2.imwrite(monFile, monAsset)
crop = cv2.imread(monFile,3)
crop = crop[0:int(height), 0:int((width/10)*10)]
kernel = np.ones((2,2),np.uint8)
crop = cv2.erode(crop,kernel,iterations = 1)
kernel = np.ones((3,3),np.uint8)
crop = cv2.morphologyEx(crop, cv2.MORPH_CLOSE, kernel)
#gray = cv2.cvtColor(crop,cv2.COLOR_BGR2GRAY)
#_,thresh = cv2.threshold(gray,1,255,cv2.THRESH_BINARY_INV)
#contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
#cnt = contours[0]
#x,y,w,h = cv2.boundingRect(cnt)
#crop = crop[y-1:y+h+1,x-1:x+w+1]
cv2.imwrite(monFile,crop)
_monList = myList = '|'.join(map(str, monList))
dbWrapper = DbWrapper(str(args.db_method), str(args.dbip), args.dbport, args.dbusername, args.dbpassword, args.dbname, args.timezone)
dbWrapper.clearHashGyms(_monList)
@staticmethod
def copyWeather(pogoasset):
from shutil import copyfile
log.info('Processing Weather Pics')
weatherImgPath = os.getcwd() + '/weather/'
filePath = os.path.dirname(weatherImgPath)
if not os.path.exists(filePath):
log.info('weather directory created')
os.makedirs(filePath)
assetPath = pogoasset
for file in glob.glob(os.path.join(assetPath, 'static_assets/png/weatherIcon_small_*.png')):
MonRaidImages.read_transparent_png(file, os.path.join('weather', os.path.basename(file)), 0)
@staticmethod
def read_transparent_png(assetFile, saveFile, bgcolor = 255):
image_4channel = cv2.imread(assetFile, cv2.IMREAD_UNCHANGED)
alpha_channel = image_4channel[:,:,3]
rgb_channels = image_4channel[:,:,:3]
white_background_image = np.ones_like(rgb_channels, dtype=np.uint8) * bgcolor
alpha_factor = alpha_channel[:,:,np.newaxis].astype(np.float32) / 255.0
alpha_factor = np.concatenate((alpha_factor,alpha_factor,alpha_factor), axis=2)
base = rgb_channels.astype(np.float32) * alpha_factor
white = white_background_image.astype(np.float32) * (1 - alpha_factor)
final_image = base + white
cv2.imwrite(saveFile,final_image.astype(np.uint8))
return assetFile
@staticmethod
def runAll(pogoasset):
MonRaidImages.copyMons(pogoasset)
MonRaidImages.copyWeather(pogoasset)
if __name__ == '__main__':
MonRaidImages.runAll('../../PogoAssets/')