1
\$\begingroup\$

The small bit of code below blits and image to the screen within a class. I call this 4 times to display the image 4 times, each image has a different y coordinate and doesn't need different x but when they are blitted they make the code MUCH slower. I know it is the blitting as well due to the fact that if i just draw rects instead, the program moves smoothly.

tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png'), (270, 240))

    def display(self):
        if not self.occupied:
            screen.blit(tableTex, (self.x, self.y))
        else:
            pygame.draw.rect(screen, (127, 0, 0), self.rect, 0)

A link to the full code can be found here if needed. I just want to know if there is any reason this is so slow and if there are any other methods I could use that are faster?

Thanks

\$\endgroup\$
  • \$\begingroup\$ You should add the definition of tableTex to the code, since it is actually quite relevant. \$\endgroup\$ – Graipher Jul 3 '18 at 15:49
  • 1
    \$\begingroup\$ @Graipher My bad - Updated it now \$\endgroup\$ – Thomas Ayling Jul 3 '18 at 15:51
0
\$\begingroup\$

As noted in the beginners guide, you should convert your image right after loading it. So instead of

tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png'), (270, 240))

Do

tableTex = pygame.transform.scale(pygame.image.load('./Sources/Table.png').convert(), (270, 240))

As noted in the guide linked above, this can lead to a speed-up of about a factor 6. The reason is:

The 'format' that convert() refers to isn't the file format (ie PNG, JPEG, GIF), it's what's called the 'pixel format'. This refers to the particular way that a surface records individual colors in a specific pixel. If the surface format isn't the same as the display format, SDL [what pygame uses underneath] will have to convert it on-the-fly for every blit -- a fairly time-consuming process. Don't worry too much about the explanation; just note that convert() is necessary if you want to get any kind of speed out of your blits.

\$\endgroup\$
  • \$\begingroup\$ Thanks very much for the reply, however using convert adds some very strange deformities to my loaded images imgur.com/a/htautZi \$\endgroup\$ – Thomas Ayling Jul 3 '18 at 15:55
  • \$\begingroup\$ @ThomasAyling: I don't see them. But I just changed my answer to call convert on the loaded image, not the scaled one. Does that change anything? \$\endgroup\$ – Graipher Jul 3 '18 at 15:57
  • \$\begingroup\$ No, i still get the same problem imgur.com/a/80IPaj7 (New image) \$\endgroup\$ – Thomas Ayling Jul 3 '18 at 15:59
  • \$\begingroup\$ @ThomasAyling: That does indeed look weird (comparing it to the original in the github). Maybe the picture contains transparency which is wrongly interpreted? \$\endgroup\$ – Graipher Jul 3 '18 at 16:01
  • \$\begingroup\$ Possibly, it works properly without the convert in still so maybe there were some issues with compression, i'll try re-saving it from Photoshop \$\endgroup\$ – Thomas Ayling Jul 3 '18 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.