2

I loaded 150 .tif images to a glob module and am currently unable to load them. I'm very new to programming so I imagine it's a dumb mistake I'm missing but I cant seem to figure it out.

The is the code:

import pygame, glob

types= ('*.tif')

artfile_names= []

for files in types:
    artfile_names.extend(glob.glob(files))

print(artfile_names)

for artworks in artfile_names:
    pygame.image.load(str(artfile_names))

Thank you for any help!


Delly
  • 123
  • 1
  • 10

1 Answers1

1

The mistake is that your types variable is just a string (wrapping it in parentheses has no effect), so you iterate over the letters in the string and call artfile_names.extend(glob.glob(files)) for each letter.

The comma makes the tuple (except for the empty tuple):

types = '*.tif',  # This gives you a tuple with the length 1.

types = '*.tif', '*.png'  # This is a tuple with two elements.

In the second part of your code, you need to iterate over the artfile_names, call pygame.image.load(artwork) to load the image from your hard disk and append the resulting surface to a list:

images = []

for artwork in artfile_names:
    images.append(pygame.image.load(artwork).convert())

Call the .convert() method (or .convert_alpha() for images with transparency) to improve the blit performance.

skrx
  • 19,980
  • 5
  • 34
  • 48
  • BTW, that code only loads the images, but it doesn't display anything. Do you read or watch a pygame tutorial? You need a while loop in which you use the `.blit` method of your pygame display surface to blit the images. – skrx Nov 12 '17 at 01:42
  • Ah, I see. Thanks for the clarification :) – Delly Nov 12 '17 at 01:45
  • No problem. [Program Arcade Games](http://programarcadegames.com/index.php?chapter=introduction_to_graphics&lang=en#section_5) is a good book about Python and pygame if you need a tutorial. – skrx Nov 12 '17 at 01:48