def saveToRpkg(self):
"""
Speichern des Raums
Parameter: -
Rückgabewerte: -
"""
print type(self.bg)
room = (self.name, self.col, pygame.image.tostring(self.bg, "RGB"), self.bg.get_size(), self.equippables, self.doors)
#room = (self.name, self.col, "placeholder", "placeholder", self.equippables, self.doors)
try:
Pickle.dump(room, open(self.name + ".rpkg", "wb"), 2)
except Exception as e:
print e
python类image()的实例源码
def random_bytes(self, min_bytes=1):
generated_bytes = []
self.cam.start()
while len(generated_bytes) < min_bytes:
raw = self._get_image_raw(ensure_fresh=True) # each subsequent image must be different
chunk_size = self.chunk_size
extracted_bytes = self._extract_randomness_sha(raw, chunk_size)
generated_bytes.extend(extracted_bytes)
self.cam.stop()
return generated_bytes
def load_sound(path):
path = os.path.join(main_dir, "data", path)
if not pygame.mixer:
return None
try:
data = pygame.mixer.Sound(path)
except pygame.error:
print("Sorry, couldn't load image " +(path) + " " + pygame.get_error())
return data
def load_image(path, transparent):
path = os.path.join(main_dir, "data", path)
if not pygame.image:
return None
try:
data = pygame.image.load(path)
except pygame.error:
print("Couldn't load image file " + path+ " " + pygame.get_error())
if transparent:
corner = data.get_at((0,0))
data.set_colorkey(corner, RLEACCEL)
return data.convert()
def testLoadPNG(self):
""" see if we can load a png with color values in the proper channels.
"""
# Create a PNG file with known colors
reddish_pixel = (210, 0, 0, 255)
greenish_pixel = (0, 220, 0, 255)
bluish_pixel = (0, 0, 230, 255)
greyish_pixel = (110, 120, 130, 140)
pixel_array = [reddish_pixel + greenish_pixel,
bluish_pixel + greyish_pixel]
f_descriptor, f_path = tempfile.mkstemp(suffix='.png')
f = os.fdopen(f_descriptor, 'wb')
w = png.Writer(2, 2, alpha=True)
w.write(f, pixel_array)
f.close()
# Read the PNG file and verify that pygame interprets it correctly
surf = pygame.image.load(f_path)
pixel_x0_y0 = surf.get_at((0, 0))
pixel_x1_y0 = surf.get_at((1, 0))
pixel_x0_y1 = surf.get_at((0, 1))
pixel_x1_y1 = surf.get_at((1, 1))
self.assertEquals(pixel_x0_y0, reddish_pixel)
self.assertEquals(pixel_x1_y0, greenish_pixel)
self.assertEquals(pixel_x0_y1, bluish_pixel)
self.assertEquals(pixel_x1_y1, greyish_pixel)
# Read the PNG file obj. and verify that pygame interprets it correctly
f = open(f_path, 'rb')
surf = pygame.image.load(f)
f.close()
pixel_x0_y0 = surf.get_at((0, 0))
pixel_x1_y0 = surf.get_at((1, 0))
pixel_x0_y1 = surf.get_at((0, 1))
pixel_x1_y1 = surf.get_at((1, 1))
self.assertEquals(pixel_x0_y0, reddish_pixel)
self.assertEquals(pixel_x1_y0, greenish_pixel)
self.assertEquals(pixel_x0_y1, bluish_pixel)
self.assertEquals(pixel_x1_y1, greyish_pixel)
os.remove(f_path)
def testSaveJPG(self):
""" JPG equivalent to issue #211 - color channel swapping
Make sure the SDL surface color masks represent the rgb memory format
required by the JPG library. The masks are machine endian dependent
"""
from pygame import Color, Rect
# The source image is a 2 by 2 square of four colors. Since JPEG is
# lossy, there can be color bleed. Make each color square 16 by 16,
# to avoid the significantly color value distorts found at color
# boundaries due to the compression value set by Pygame.
square_len = 16
sz = 2 * square_len, 2 * square_len
# +---------------------------------+
# | red | green |
# |----------------+----------------|
# | blue | (255, 128, 64) |
# +---------------------------------+
#
# as (rect, color) pairs.
def as_rect(square_x, square_y):
return Rect(square_x * square_len, square_y * square_len,
square_len, square_len)
squares = [(as_rect(0, 0), Color("red")),
(as_rect(1, 0), Color("green")),
(as_rect(0, 1), Color("blue")),
(as_rect(1, 1), Color(255, 128, 64))]
# A surface format which is not directly usable with libjpeg.
surf = pygame.Surface(sz, 0, 32)
for rect, color in squares:
surf.fill(color, rect)
# Assume pygame.image.Load works correctly as it is handled by the
# third party SDL_image library.
f_path = tempfile.mktemp(suffix='.jpg')
pygame.image.save(surf, f_path)
jpg_surf = pygame.image.load(f_path)
# Allow for small differences in the restored colors.
def approx(c):
mask = 0xFC
return pygame.Color(c.r & mask, c.g & mask, c.b & mask)
offset = square_len // 2
for rect, color in squares:
posn = rect.move((offset, offset)).topleft
self.assertEqual(approx(jpg_surf.get_at(posn)), approx(color))