Re: [pygame] Load Graphics 30x faster than pygame.image.load
Yeah, with current DMA hard drives loading uncompressed images is going to be pretty quick. Since these drives can read 20-50MB/s. Note, that uncompressed .tga files often compress better with 7zip compression than what png files can be compressed to. However for computers where DMA isn't working, or just a slow hard drive, then compressed images will load faster. Cheers, On 1/20/07, David Gowers <[EMAIL PROTECTED]> wrote: On 1/20/07, Bob Ippolito <[EMAIL PROTECTED]> wrote: > On 1/19/07, Kamilche <[EMAIL PROTECTED]> wrote: > > I discovered a technique for loading graphics quickly using Pygame, > > about 30x faster than a straight pygame.image.load . > > > > The gist of it is convert the picture to a string with > > > > s = pygame.image.tostring(pic, 'RGBA') > > w, h = pic.get_size() > > > > and later, when loading, use > > pygame.image.frombuffer(s, [w, h], 'RGBA') > > > > Unfortunately, I can't use it in my current project, because I need to > > access the color palette of 256 color images. I can't use this technique > > to store and load these palettized images, because images come out solid > > black. > > > > Hope this technique comes in useful to someone else tho! (Someone that > > doesn't need 256 color images.) > > That doesn't seem to make any sense... you've already loaded the > picture, why convert it to a string just to convert it back to a > picture? It's using up about the same amount of RAM as a string or as > an image object. You misunderstand I think. It looks to me like he is saying 'convert the image to a string and save that', then when loading just load the string. Which is going to be faster, of course -- no decompression or headers. It just strikes me if this kind of thing is critical then he's probably loading the images at the wrong point.
Re: [pygame] Load Graphics 30x faster than pygame.image.load
On 1/20/07, Bob Ippolito <[EMAIL PROTECTED]> wrote: On 1/19/07, Kamilche <[EMAIL PROTECTED]> wrote: > I discovered a technique for loading graphics quickly using Pygame, > about 30x faster than a straight pygame.image.load. > > The gist of it is convert the picture to a string with > > s = pygame.image.tostring(pic, 'RGBA') > w, h = pic.get_size() > > and later, when loading, use > pygame.image.frombuffer(s, [w, h], 'RGBA') > > Unfortunately, I can't use it in my current project, because I need to > access the color palette of 256 color images. I can't use this technique > to store and load these palettized images, because images come out solid > black. > > Hope this technique comes in useful to someone else tho! (Someone that > doesn't need 256 color images.) That doesn't seem to make any sense... you've already loaded the picture, why convert it to a string just to convert it back to a picture? It's using up about the same amount of RAM as a string or as an image object. You misunderstand I think. It looks to me like he is saying 'convert the image to a string and save that', then when loading just load the string. Which is going to be faster, of course -- no decompression or headers. It just strikes me if this kind of thing is critical then he's probably loading the images at the wrong point.
Re: [pygame] Tentative patch for "metrics" font method
Hi Lenard... Thank you for testing all this. I really do not understand anymore why I am getting garbage (not square) characters... I will try some stand-alone tests and let you know. Regards, Guillaume
Re: [pygame] Tentative patch for "metrics" font method
Brian Fisher wrote: Hey Guillame, Thank you; apparently, the stuff i needed does not work as expected (on Windows at least). I will try out on Linux. Thanks a ton! That's a shame that the metrics stuff doesn't detect what you want, there might be a way you can work around it though... On 1/19/07, Guillaume Proux <[EMAIL PROTECTED]> wrote: ? ... Why does it display garbage then in Freevo?! I guess I need to make a couple of test cases. When it displays garbage it's a square box, yeah? Basically with a font there are 2 sets of things, one is the glyphs/outlines used to make the images, the other is the mapping table that goes from some char set (like unicode) to a glyph. Most fonts have mapping tables set up to have stuff they don't have glyphs for point to the "I don't have an image for that" glyph, which is usually a specific square box. I've actually found that some characters fail to render with pygame's font stuff for specific fonts (the render call throws an exception), which I assume is cause the mapping table doesn't have anything for that char. I'll try and dig up a case of that and send it later... -- So anyways, even though there doesn't seem to be a way to directly detect that there is no glyph or image designed for a given character through SDL_ttf, you might be able to indirectly figure it out by detecting that the font rendered the box... it would be really slow, but it oughta work ...Also, I think the metrics thing would be a cool addition anyways - since the chars over-render with empty space in order to get their spacing correct now, it would be great to have that metrics info in pygame. Also, if you were going to do some kind of square box char not found detection stuff, the metrics stuff could be a quick early rejection test (I'm sure that (1, 5, 0, 12, 5) must be the metrics for the square box for chars with no glyph) I finally loaded kochi-gothic.ttf into a pygame application and tried some text. It has ascii characters, which confirms what the Window's font browser showed me. And it has Hiragana characters. For unknown characters it shows a small, solid square. -- Lenard Lindstrom <[EMAIL PROTECTED]>
Re: [pygame] Load Graphics 30x faster than pygame.image.load
On 1/19/07, Kamilche <[EMAIL PROTECTED]> wrote: I discovered a technique for loading graphics quickly using Pygame, about 30x faster than a straight pygame.image.load. The gist of it is convert the picture to a string with s = pygame.image.tostring(pic, 'RGBA') w, h = pic.get_size() and later, when loading, use pygame.image.frombuffer(s, [w, h], 'RGBA') Unfortunately, I can't use it in my current project, because I need to access the color palette of 256 color images. I can't use this technique to store and load these palettized images, because images come out solid black. Hope this technique comes in useful to someone else tho! (Someone that doesn't need 256 color images.) That doesn't seem to make any sense... you've already loaded the picture, why convert it to a string just to convert it back to a picture? It's using up about the same amount of RAM as a string or as an image object. -bob
[pygame] Load Graphics 30x faster than pygame.image.load
I discovered a technique for loading graphics quickly using Pygame, about 30x faster than a straight pygame.image.load. The gist of it is convert the picture to a string with s = pygame.image.tostring(pic, 'RGBA') w, h = pic.get_size() and later, when loading, use pygame.image.frombuffer(s, [w, h], 'RGBA') Unfortunately, I can't use it in my current project, because I need to access the color palette of 256 color images. I can't use this technique to store and load these palettized images, because images come out solid black. Hope this technique comes in useful to someone else tho! (Someone that doesn't need 256 color images.) --Kamilche
Re: [pygame] Tentative patch for "metrics" font method
Hey Guillame, Thank you; apparently, the stuff i needed does not work as expected (on Windows at least). I will try out on Linux. Thanks a ton! That's a shame that the metrics stuff doesn't detect what you want, there might be a way you can work around it though... On 1/19/07, Guillaume Proux <[EMAIL PROTECTED]> wrote: ? ... Why does it display garbage then in Freevo?! I guess I need to make a couple of test cases. When it displays garbage it's a square box, yeah? Basically with a font there are 2 sets of things, one is the glyphs/outlines used to make the images, the other is the mapping table that goes from some char set (like unicode) to a glyph. Most fonts have mapping tables set up to have stuff they don't have glyphs for point to the "I don't have an image for that" glyph, which is usually a specific square box. I've actually found that some characters fail to render with pygame's font stuff for specific fonts (the render call throws an exception), which I assume is cause the mapping table doesn't have anything for that char. I'll try and dig up a case of that and send it later... -- So anyways, even though there doesn't seem to be a way to directly detect that there is no glyph or image designed for a given character through SDL_ttf, you might be able to indirectly figure it out by detecting that the font rendered the box... it would be really slow, but it oughta work ...Also, I think the metrics thing would be a cool addition anyways - since the chars over-render with empty space in order to get their spacing correct now, it would be great to have that metrics info in pygame. Also, if you were going to do some kind of square box char not found detection stuff, the metrics stuff could be a quick early rejection test (I'm sure that (1, 5, 0, 12, 5) must be the metrics for the square box for chars with no glyph)
Re: [pygame] Tentative patch for "metrics" font method
Hi Lenard, cheers for your help... Nice to see how easy it is to add new features to pygame! I downloaded kochi-gothic.ttf and found it has ascii characters. Maybe ? ... Why does it display garbage then in Freevo?! I guess I need to make a couple of test cases. about. I don't think Gurmukhi ( U+0A00-U+0A7F ) is on my machine. This is a quick test session I did: Thank you; apparently, the stuff i needed does not work as expected (on Windows at least). I will try out on Linux. Thanks a ton! cheers, Guillaume
Re: [pygame] Newbie needs help, what's the most efficient (or easiest) way to do this?
No. I guess I should, huh? Well, whatever. In any case, I've almost got this thing working. But I forgot to add a line that resets the program after the last word is typed... It gives some sort of "list out of index" something or other error. I'm working on it... What I need to do now is calculate the length of the list - I assume that it is self.text as self.text[self.current_string] pulls a word out of the list. I tried this: if len(self.text) == self.current_string: self.current_string = 0 self.pos = 0 without success. Time to get to my next class, though, so I'll troubleshoot this later... On 1/17/07, Brandon N <[EMAIL PROTECTED]> wrote: Great progress! One question, are you backing up your code in between "breaking" it? On Jan 17, 2007, at 11:43 AM, Charles Christie wrote: > Good news everyone! I found out why I could never get anywhere: > > I was too afraid of breaking stuff so I didn't try to mess with > anything. However, if you don't break it, how can you fix it? So, I > looked over that pygame thing again and decided to just start doing > things that came to mind. It took me five minutes to get it working, > much faster than if I had begged someone to tell me how to do it. > Python really is a simple language after all, I think I really > understand this now! This is what my code looks like now: > > *** > #Credit to scriptedfun (http://www.scriptedfun.com) for the help with > the typing portion > #Stil a very VERY barebones typing script. Still got one last part > of the main > #engine to go before getting to work on part two. > > import pygame > from pygame.locals import * > > SCREENRECT = Rect(0, 0, 640, 480) > > class Textsprite(pygame.sprite.Sprite): >def __init__(self, text): >pygame.sprite.Sprite.__init__(self) >self.text = text >self.pos = 0 >self.current_string = 0 >self.update() >def update(self): >self.image = pygame.font.Font(None, > 36).render(self.text[self.current_string], 1, (0, 0, 0)) >self.highlight = pygame.font.Font(None, > 36).render(self.text[self.current_string][:self.pos], 1, (0, 0, 255)) >self.image.blit(self.highlight, (0, 0)) >self.rect = self.image.get_rect() >self.rect.center = pygame.display.get_surface().get_rect > ().center >def keyin(self, key): >if key == self.text[self.current_string][self.pos]: >self.pos = self.pos + 1 >elif key == K_TAB: >self.pos = self.pos >else: >self.combo = 0 >if len(self.text[self.current_string]) == self.pos: >self.pos = 0 >self.current_string = self.current_string +1 > > def main(): >pygame.init() > >screen = pygame.display.set_mode(SCREENRECT.size) > ># make background >background = pygame.Surface(SCREENRECT.size).convert() >background.fill((255, 255, 255)) >screen.blit(background, (0, 0)) >pygame.display.update() > ># keep track of sprites >all = pygame.sprite.RenderUpdates() > ># keep track of time >clock = pygame.time.Clock() > >textsprite = Textsprite(["test string1", "another test!", "can you > type this?"]) >all.add(textsprite) > ># game loop >while 1: > ># get input >for event in pygame.event.get(): >if event.type == QUIT: >return >elif event.type == KEYDOWN: >if event.key == K_ESCAPE: >return >else: >textsprite.keyin(event.unicode) > ># clear sprites >all.clear(screen, background) > ># update sprites >all.update() > ># redraw sprites >dirty = all.draw(screen) >pygame.display.update(dirty) > ># maintain frame rate >clock.tick(30) > > if __name__ == '__main__': main() > *** > > Now, there's just one problem, and I'm pretty sure you can see it: > After the program goes through all the strings, it doesn't stop at the > last one and tries to add one again. Which won't work, and the program > will print an error about it going out of bounds. I'm working on that > now, it should take no less than five or ten minutes to figure out. > Thanks for all your help!
Re: [pygame] Tentative patch for "metrics" font method
Guillaume Proux wrote: Hi Lenard, Thank you for helping. I have NO knowledge of programming for Python/C interface. I tried just to copy paste from above methods I guess I should try to learn a bit before writing patches. I also need to set up a dev system. Only one sytax error. Not bad. As for writing Python extension code, studying existing modules is a good start. I admit I have little experience beyond patching existing code myself. As for a dev system, it was not that long ago that batch jobs were common :-). On my Linux box, I had problem with non appearing characters (garbage) when trying to display english characters using the Kochi font. I don't know how it would work if one would try this in Windows. This would be the only way to trigger the "character is not in the font" as per the SDL_TTF doc. I downloaded kochi-gothic.ttf and found it has ascii characters. Maybe this is the wrong ttf file. On Windows, anyway, the metrics method returns the tuple (1, 5, 0, 12, 5) for characters it should know nothing about. I don't think Gurmukhi ( U+0A00-U+0A7F ) is on my machine. This is a quick test session I did: >>> from pygame import font >>> font.init() >>> f=font.Font('kochi-gothic.ttf', 16) >>> f.metrics('a') (0, 7, 0, 9, 8) >>> f.metrics(u'\u2ee0') (1, 5, 0, 12, 5) >>> f.metrics(u'\u2eef') (1, 5, 0, 12, 5) >>> f.metrics(u'\U00012000') (1, 5, 0, 12, 5) >>> f.metrics('\x2e') (1, 3, 0, 2, 8) >>> f.metrics(u'\u0A01') (1, 5, 0, 12, 5) >>> f.metrics(u'\u0A00') (1, 5, 0, 12, 5) Regarding the exception, I don't have strong opinions, I tend to be conservative with raising exceptions and maybe one should raise an exception when the string passed as parameter is empty but... I am not convinced. But I will let official maintainers make the best choice. Yes, it is ultimately up to the Pygame maintainers to decide. Good luck with the patch. Lenard -- Lenard Lindstrom <[EMAIL PROTECTED]>
Re: [pygame] MacOS/PyGame/PyOpenGL mouse inversion
> I'm not sure how you got the older version, and why pygame is loading > it... I'm pretty sure I installed 1.8.0pre from the pythonmac.org > universal binary prebuilt: Yeah, that looks like the same place I installed from, too. This weekend, I'll do some exploring around my installation and see if I can turn up any clues - perhaps my last upgrade left things in a slightly inconsistent state. Thanks for the assistance. -Dave LeCompte