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

Re: [pygame] pygame.image.save()



Good point about pygame.image.load not caring about file extensions,
and only file contents...

I just updated the unittest to check the magic hex numbers of the file
to test if the file is actually getting saved how we want it to be.

Below is the magic testing function I used - in case you want to use it.

cheers,



def test_magic(f, magic_hex):
    """ tests a given file to see if the magic hex matches.
    """
    data = f.read(len(magic_hex))

    if len(data) != len(magic_hex):
        return 0

    for i in range(len(magic_hex)):
        if data[i] != data[i]:
            return 0

    return 1


magic_hex = {}
magic_hex['jpg'] = [0xff, 0xd8, 0xff, 0xe0]
magic_hex['png'] = [0x89 ,0x50 ,0x4e ,0x47]
magic_hex['tga'] = [0x0, 0x0, 0xa]
magic_hex['bmp'] = [0x42, 0x4d]

if test_magic(open(temp_filename, "rb"), magic_hex["png"]):
    print "this is a png file!"




On Tue, Apr 15, 2008 at 10:56 AM, Lenard Lindstrom <len-l@xxxxxxxxx> wrote:
> Ian Mallett wrote:
>
> > Well, it worked for me--saving it to a valid .png file.
> >
>  Are you sure? I just tried it with a .png picture. It was certainly not
> saved as a PNG image. Changing the file extension to .jpg let me view it
> with ImageNavigator. The reason this bug may have slipped through is that
> Pygame apparently ignores the file extension when determining file type. It
> will quite happily load a JPEG named "something.png", or even "something".
>
>  --
>  Lenard Lindstrom
>  <len-l@xxxxxxxxx>
>
>