forked from openRPGteam/openrpg-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.py
61 lines (51 loc) · 2.55 KB
/
render.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
from PIL import Image
import os, random
WORLD_MAP = "test_resources/terrain.jpg"
SPRITES_DIR = "test_resources/sprites/"
def get_background(char_pos, size=300):
"""return pure background image around the character in char_pos position."""
full_back = Image.open(WORLD_MAP).convert("RGBA")
cropbox = (char_pos[0] - size//2, char_pos[1] - size//2, char_pos[0] + size//2, char_pos[1] + size//2)
full_back = full_back.crop(cropbox)
return full_back
def random_pos(image):
"""image must be at least 11x11 px"""
return (random.randint(0, image.size[0] - 50), random.randint(0, image.size[1] - 50))
def fill_with_shit(background, quantity=random.randint(2,11)):
"""add terrain elements to the background"""
elements = [SPRITES_DIR + filename for filename in os.listdir(SPRITES_DIR) if filename.endswith('.png')]
for _ in range(quantity):
tmpimg = Image.open(elements[random.randint(0, len(elements) - 1)])
blank = Image.new('RGBA', background.size, color=(0,0,0,0))
blank.paste(tmpimg, random_pos(background))
background = Image.alpha_composite(background, blank)
tmpimg.close(); blank.close()
return background
def add_dynamic_sprites(terrain_img, spr):
"""sprite list must be a tuple of filename and (x, y) position"""
transparent = Image.new("RGBA", terrain_img.size, (0,0,0,0))
tmpimg = Image.open(spr[0]).convert('RGBA')
transparent.paste(tmpimg, spr[1])
terrain_img = Image.alpha_composite(terrain_img, transparent)
tmpimg.close(); transparent.close()
return terrain_img
if __name__ == '__main__':
import sys
if len(sys.argv) < 4:
print('''Usage: render.py [position x] [position y] [output filename] -s [size] -q [quantity] -d [sprite_list (filename,x,y NO indents)]
Arguments with switches are optional''')
else:
# extract size form argv
size = int(sys.argv[sys.argv.index('-s') + 1]) if '-s' in sys.argv else 300
# extract quantity from argv
quantity = int(sys.argv[sys.argv.index('-q') + 1]) if '-q' in sys.argv else random.randint(2,11)
bck = get_background((int(sys.argv[1]), int(sys.argv[2])), size)
bck = fill_with_shit(bck, quantity)
if '-d' in sys.argv:
spr_list_begin = sys.argv.index('-d') + 1
subargv = sys.argv[spr_list_begin:]
for dyn_sprite in subargv:
el = dyn_sprite.split(',')
bck.add_dynamic_sprites((el[0], (int(el[1]), int(el[2]))))
bck.save(sys.argv[3], "JPEG", optimize=True, quality=80)
bck.close()