[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Numeric Question



Hey Pete, I have another Numeric question for you, if you've got the time.

Let's say I have a bunch of data in FFFFRGBA format, where FFFF is an arbitrary floating point number, and RGBA are unsigned bytes. The following routine will convert that to an RGBA picture, but it takes a looooong, time.

What's the magical incantation to get Numeric to assign the data directly to the pixels of the surface?

--Kamilche

---------------------------------------------------

import pygame
import Numeric
import Image
import struct
import math
import os
import sys

pathname, scriptname = os.path.split(sys.argv[0])
pathname = os.path.abspath(pathname)
os.chdir(pathname)


def Convert(filename):
if filename[-3:] != 'zbf':
return
print "Converting %s" % filename
fd = open(filename, 'rb')
header = fd.read(34)
data = fd.read()
fd.close()
numpixels = len(data)/8
w = h = math.sqrt(numpixels)
if int(w) != w:
print int(w), w
raise Exception("The picture must be totally square, and rendered 3x the desired size without antialiasing!")
w = int(w)
h = int(h)
pic = pygame.Surface((w, h), pygame.SRCALPHA, 32).convert_alpha()
ctr = 0
for y in range(h):
for x in range(w):
d, r, g, b, a = struct.unpack("<fBBBB", data[ctr:ctr+8])
if d < 100000:
a = 255
else:
a = 0
pic.set_at((x, y), [r, g, b, a])
ctr += 8
smallpic = pygame.transform.rotozoom(pic, 0, .33)
neww, newh = smallpic.get_size()


s = pygame.image.tostring(smallpic, "RGBA")
im = Image.fromstring("RGBA", (neww, newh), s)
newfilename = filename[:-3] + 'png'
im.save(newfilename)
print "New file saved to %s, shrunk to %dx%d" % (newfilename, neww, newh)