# The start of a game in pygame, blitting images to the screen

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

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

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.

• Thanks very much for the reply, however using convert adds some very strange deformities to my loaded images imgur.com/a/htautZi – Thomas Ayling Jul 3 '18 at 15:55
• @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? – Graipher Jul 3 '18 at 15:57
• No, i still get the same problem imgur.com/a/80IPaj7 (New image) – Thomas Ayling Jul 3 '18 at 15:59
• @ThomasAyling: That does indeed look weird (comparing it to the original in the github). Maybe the picture contains transparency which is wrongly interpreted? – Graipher Jul 3 '18 at 16:01
• 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 – Thomas Ayling Jul 3 '18 at 16:03