-
Notifications
You must be signed in to change notification settings - Fork 0
/
PictureGenerator.py
112 lines (101 loc) · 4.32 KB
/
PictureGenerator.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
# -*- coding: utf-8 -*-
"""
@author: R.P.O.D.
Class for generating pictures
===============================================================================
Picture Generator Class
===============================================================================
"""
import urllib.request
import os
import io
import shutil
from PIL import Image
class PictureGenerator:
def __init__(self, deck):
self.deck = deck
def createPicture(self):
grid = Image.new('RGB', (4800, 4760))
x = 0
y = 0
for card in self.deck.cards:
try:
iurl = urllib.request.urlopen("http://mtgimage.com/card/" + card.name + ".hq.jpg")
except urllib.request.HTTPError:
print ("Could not load " + card.name)
mode = 'y'
while (mode != 'y' or 'n'):
mode = input("Do you want to continue without the card? [y/n]\n")
if (mode == 'y'):
break
if (mode == 'n'):
if not os.path.exists("tmp\\" + card.name):
os.makedirs("tmp\\" + card.name)
input("Please insert the cardimage from http://mtgimage.com/card/" +
card.name + ".jpg into the folder: tmp\\" + card.name + " manually\n\nPress Enter to Continue\n")
while(len(os.listdir("tmp\\" + card.name)) != 1):
input("Please insert just one imagefile.\n\nPress Enter to continue.\n")
tim = Image.open("tmp\\"+ card.name + "\\" + os.listdir("tmp\\" + card.name)[0])
tim = tim.resize((480, 680), Image.ANTIALIAS)
for i in range(int(card.amount)):
grid.paste(tim, (x, y))
x = x + 480
if (x == 4800):
x = 0
y = y + 680
break
else:
print("Please insert y or n.")
shutil.rmtree('tmp', ignore_errors=True)
continue
rawfile = io.BytesIO(iurl.read())
cim = Image.open(rawfile)
cim = cim.resize((480, 680), Image.ANTIALIAS)
for i in range(int(card.amount)):
grid.paste(cim, (x, y))
x = x + 480
if (x == 4800):
x = 0
y = y + 680
try:
burl = urllib.request.urlopen("http://s22.postimg.org/9n0ainnwh/XLHQ_Back.jpg")
except urllib.request.HTTPError:
print ("Backimage could not be loaded'")
rawfileb = io.BytesIO(burl.read())
bim = Image.open(rawfileb)
bim = bim.resize((480, 680), Image.ANTIALIAS)
grid.paste(bim, (4320, 4080))
grid.save("Output\\" + self.deck.name + ".jpg")
def createRawPicture(self, deck, c):
grid = Image.new('RGB', (4800, 4760))
x = 0
y = 0
for card in deck:
cim = Image.open("Input(raw)\\" + card)
cim = cim.resize((480, 680), Image.ANTIALIAS)
grid.paste(cim, (x, y))
x = x + 480
if (x == 4800):
x = 0
y = y + 680
bim = Image.open("Input(raw)\\back\\" + os.listdir("Input(raw)\\back")[0])
bim = bim.resize((480, 680), Image.ANTIALIAS)
grid.paste(bim, (4320, 4080))
grid.save("Output\\RawDeck\\Deck " + str(c) + ".jpg")
#Just for manual download purposes
def pictureDownload(self):
for card in self.deck.cards:
url = "http://mtgimage.com/card/" + card.name + ".jpg"
if not os.path.exists("Output\\" + self.deck.name):
os.makedirs("Output\\" + self.deck.name)
for i in range(int(card.amount)):
try:
urllib.request.urlretrieve(url, "Output\\" + self.deck.name + "\\" + card.name + str(i) + ".jpg" )
except urllib.request.HTTPError:
print ("Please load " + card.name + " manually.")
continue
def main():
pg = PictureGenerator("")
pg.createSingle()
if __name__ == '__main__': # call if module is called as main
main()