Trouble loading images from a list into pygame

Question:

I’m creating a card game in pygame and need to display different card graphics dependent on a random roll.
I create card_list earlier as a list of jpg files

card_list=[]
for filename in glob.glob('Cards/*.jpg'):
    im = Image.open(filename)
    card_list.append(im)


if cards_button.draw():
            rand  = random.randrange(0,3)       #Just using range of 0-3 to test
            screen.fill((128, 0, 128))
            user_card = card_list[rand]
            screen.blit(user_card, (100,0))`

But I get this error:

Traceback (most recent call last):
  File "C:UsersOwnerPycharmProjectsTarotGamemain.py", line 102, in <module>
    screen.blit(user_card, (100,0))
TypeError: argument 1 must be pygame.Surface, not JpegImageFile

What am I doing wrong here? Thank you.

I experimented with pygame.image.load() but still no dice.

Asked By: Mike Murphy

||

Answers:

Instead of using Image.open(filename), try using pygame’s pygame.image.load(filename) function. My guess is that pygame surfaces are incompatible with files loaded with PIL.

Answered By: Matt Eng
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.