The final piece in the puzzle of my PPG platform is the Python
Imaging Library (PIL) from Secret Labs AB
(PythonWare). This makes it easy to check that the uploaded
images are the right dimensions, for example:
im = Image.open(StringIO.StringIO(data))
width, height = im.size
if width > game.maxWidth or height > game.maxHeight:
log('Image is too large: the maximum is %d × %d.' \
% (game.maxWidth, game.maxHeight), STOP)
ok = 0
I don’t even need to know whether the image is a
PNG,
JPEG, or
GIF.
To make it easier to create pictures for the Picky
Picky Game, I have added code to automatically convert
Microsoft Windows Bitmap (.bmp
) files into
PNGs.
This is straightforward because I already was using PIL to check the
dimensions of the images. Converting to PNG (if the format is
one PIL knows) is pretty simple:
permittedTypes = ['image/png', 'image/jpeg', 'image/pjpeg', 'image/gif']
...
if not imt in permittedTypes:
buf = StringIO.StringIO()
im.save(buf, 'PNG')
data = buf.getvalue()
logger.log(slog.WARNING, 'Converted your image from %s to image/png' % imt,
'This may have lead to a slight loss of image quality.')
imt = 'image/png'
buf.close()
The above goes in the sequence of checks on uploaded images
(after the check for width × height, but before the check
for number of bytes). I think I spent longer creating
a BMP image to test it on than I did writing the new code!
The advantage of BMP support is that, if you have Microsoft
Windows, then you definitely have Microsoft Paint installed.
So long as you know about Start menu → Programs →
Accessories → Paint, and the Image → Attributes menu item,
you can create panels for Picky Picky Game.