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

[pygame] Smooth scaling using WMF drawings



Here's something for the weekend:

I am planning an application in which all window contents will scale automatically to 
the window size. I don't want to see the pixellation effects which you get with scaling 
bitmaps, so I had a look at what can be done with .WMF drawings. Here's the result, 
using PIL with Pygame.

If anyone can see a more efficient way of doing it, please let me know, as eventually I 
will be displaying up to 100 .WMF images in a window!  I did read that 
image.frombuffer() is more efficient than image.fromstring(), but in Pygame 1.6 it 
seems there is no "frombuffer" function. Is that right?

Here's the code:
"""

wmf_show.py  ----- brf Apr 2005   ---------

Just to find out how to use/display a WMF file in Pygame. It works. Of course,
it is a CONVERTED image that we are looking at. To scale smoothly, you need to 
re-convert at each resolution, which is what this demo does.

"""

import pygame, Image, WmfPlugin
from pygame.locals import *
import win32api


# ------------------------------------------------------------------------------------
def main():
    
    pygame.init()

    SM_CXSCREEN = 0
    SM_CYSCREEN = 1
    x = win32api.GetSystemMetrics(SM_CXSCREEN)
    y = win32api.GetSystemMetrics(SM_CYSCREEN)
    width = x -50
    height = y -80
    print RESIZABLE
    screen = pygame.display.set_mode((width,height),RESIZABLE,32)
    pygame.display.set_caption\
        ("WMF Show - Resize the window to control drawing size")
    
    scale = 250
    resized = 0

    # Get loopin'
    for counter in range(10**5):

        # Have we received an event to close the window?
        for e in pygame.event.get():
            if e.type in (QUIT,KEYDOWN):  return

            if e.type == VIDEORESIZE:

               scale = 250 * e.size[0] / width
               screen = pygame.display.set_mode(e.size,RESIZABLE,32)
               resized = 1
                
        # For some reason, (e.g. the wmf is rendered behind the scenes) I need to 
        # re-open file each time as even copying the im1 surface freezes the size.

        # I found this wmf image in 
        # http://www.worldofmusic.com.au/downloads_view.asp?id=24
        im1 = Image.open("logo.wmf")
        
        if resized:
           im1.size = im1.size[0] * scale / 100, im1.size[1] * scale / 100
        else:
           # Back & forth scaling in a way that uses bigger increments when 
           # the image is bigger.
           sc = abs(counter%200-(100)) + 10
           sc = max((sc * sc/10) * scale/200,10)
           im1.size = im1.size[0] * sc / 500, im1.size[1] * sc / 500
        
        buf = im1.tostring("raw","RGB",0,0)
 
        # rotating background colours, well, why not?
        image = pygame.image.fromstring(buf,im1.size,"RGB",0)
        fcolour = abs(counter % 511 - 255), abs((counter / 2) % 511 - 255), \
                 abs((counter / 3) % 511 - 255)
        screen.fill(fcolour)

        # Centering
        ctr = screen.get_rect().center
        rsize = image.get_rect().center

        offset = ctr[0] - rsize[0], ctr[1] - rsize[1]
        screen.blit(image,offset)
	pygame.display.flip()

# 
if __name__ == '__main__': main()

# I thank you.
-- 
Regards
Ben Friman
t 0870 041 0935
w www.learntotype.com