0

recently started coding with python using pycharm / python IDLE on mac osx.

When it comes to showing / blitting images onto a defined pygame display, png and jpg formats don't appear properly (see images).

Only bmp formats appear properly.

Any suggestions to make jpg and png appear correctly in pygame?

Thank you

source jpg image

how it appears on pygame display

import pygame, sys
    from pygame.locals import *

    pygame.init()

    width = 640
    height = 480

    screen = pygame.display.set_mode((width,height))
    pygame.display.set_caption('hello piggy')

    white = (255,255,255)
    image = pygame.image.load("flpig.jpg")

    x=160
    y=120

    running = True

    while running:
        screen.fill(white)
        screen.blit(image, (x, y))

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

        pygame.display.update()
Barry S
  • 1
  • 1
  • You are changing the x and y co-ordinates 60 times a second in the while loop.. It starts off as "right" and updates that, and then goes through the others, down, left, up. this changes the x and y co-ordinates. Then you blit the image using those co-ordinates. Try changing `fpsClock.tick(FPS)` to `fpsClock.tick(3)` to see if you can see what these rapid updates are doing. – marienbad Apr 19 '16 at 11:41
  • Also, as answered below, you should use a running variable, and set it to True for your loop. – marienbad Apr 19 '16 at 11:42
  • Thanks Marienbad. I have simplified the code to a bare minimum, only to display the image. It still shows the jpg and png images in a distorted way, as shown in attached image in my question. I wonder what could fix this, really keen on using png in my pygames... – Barry S Apr 19 '16 at 23:06
  • What happens if you do this: pygame.image.load('flpig.jpg').convert() ? – marienbad Apr 20 '16 at 01:53
  • Same, same. Image is still not displaying correctly... – Barry S Apr 21 '16 at 01:14
  • You can use pillow to load your image. Try this: [link](https://stackoverflow.com/questions/56976371/how-to-display-images-loaded-with-pillow-with-pygame-in-python-3-7) – Gonzalo Plaza Feb 07 '22 at 08:29

1 Answers1

0
import pygame
from pygame.locals import*
img = pygame.image.load("flpig.jpg")
white = (255, 255, 255)
w = 640
h = 480
screen = pygame.display.set_mode((w, h))
screen.fill((white))
running = 1

while running:
 screen.fill((white))
 screen.blit(img,(0,0))
 pygame.display.flip()
Moo
  • 166
  • 12