Search code examples
pythonpython-3.xpygamepygame-surface

How to scale every image in a list? pygame


How do I scale every picture within a list in pygame?

I can't really figure it out. Whenever I try, an error says the item I am transform.scaling is a str or a bool and that I need a surface to work with. I understand that what I'm doing is wrong, but didn't hurt to try and I didn't know how to approach this. I am an amateur coder who just needs a little bit of help.

The relevant and most recent code:

rain_imgs = []
rain_list = ["1.png", "mv.png", "b1.png", "b3.png", "b4.png", "b5.png"]
for img in rain_list:
    rain_imgs.append(pg.image.load(path.join(img_folder, img)).convert())
    pg.transform.scale(img in rain_list, (60, 60))

The error:

TypeError: argument 1 must be pygame.Surface, not bool

Solution

  • I see that you're trying to use

    pg.transform.scale(img in rain_list, (60, 60))
    

    to refer to the item you've just put into the list. unfortunately, the 'in' keyword when used solo just tells you whether the item exists in the designated array, which is where the error is coming from. What you might want to do would look something like:

    for img in rain_list:
      img_item = pg.image.load(path.join(img_folder, img)).convert()
      pg.transform.scale(img_item, (60, 60))
      rain_imgs.append(img_item)
    

    Which, instead of having to yank the item back out of the list to alter it, creats a local object, alter its features, then stores it, without the boolean error cropping up.

    ====Edit===

    Since it's now appending the original images, that tells me

    pg.transform.scale(img_item, (60, 60))
    

    Might be returning a new object, rather than running an alteration on the supplied one. I would try something like:

    for img in rain_list:
      img_item = pg.image.load(path.join(img_folder, img)).convert()
      rain_imgs.append(pg.transform.scale(img_item, (60, 60)))
    

    see if that works.