-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadImages.py
68 lines (46 loc) · 1.76 KB
/
loadImages.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
import pygame
import constants
#----------------------------------------------------------------------
def loadImages(folder, filetype, size=(32, 32)):
"""load images from a given folder of a given format,
returns a surface of each or takes a single file and returns 1 surface"""
import glob
#list of all the images to be returned
imageList = []
if '.' not in filetype:
path = folder+r'*.{0}'.format(filetype)
else:
path = folder + filetype
##to check abs path
#print 'path', path
#import os.path
#print os.path.abspath(path)
for filepath in glob.glob(path):
#print 'filepath:', filepath
#scale the image at filepath
image = scaleImage(filepath, size)
#append the image to the list of images
imageList.append(image)
#when its done, return the list or single image surce
try:
if len(imageList) > 1:
return imageList
else:
return imageList[0]
except IndexError:
print 'bad image'
#----------------------------------------------------------------------
def scaleImage(path, (size)):
"""scales an image to size tuple"""
#load the image to a surface and set the colorkey
imageSurface = pygame.image.load(path).convert()
imageSurface.set_colorkey(constants.TRANS)
#print path
#scale it and get the rect of the surface
if type(size) == tuple:
scaledSurface = pygame.transform.scale(imageSurface, size)
else: #or dont scale
scaledSurface = imageSurface
scaledSurface.set_colorkey(constants.TRANS)
scaledRect = scaledSurface.get_rect()
return scaledSurface