problems with PIL

Viewed 45

Today I tried to switch pygame's image.load() to Pil's Image.open() but (as usual) I got problems. At the beginning of the code I have this fragment with pygame's image.load():

class Gamesprite():
    def __init__(self,img,x,y,width,height,speed):
        sprite.Sprite.__init__(self)
        self.image = transform(image.load(img), (width, height))

The fragment of code below is one of 4 fragments (left, right, up down) where I experimentally added image = Image.open('image.png') but code got crashed.

class Player(Gamesprite):
    def control(self):
        keys=key.get_pressed()
        if keys[K_LEFT]:
            hero_img = Image.open('hero_left.png')
            self.rect.x -= self.speed
            for wall in list_wall:
                if sprite.collide_rect(self,wall):
                    self.rect.x += self.speed*1.1

After starting code I get:

File "e:\VSCode projects\lab2.1.py", line 60, in <module>
    player = Player(hero_img,     tiles,    tiles,    int(tiles*0.87),tiles, tiles//10)
  File "e:\VSCode projects\lab2.1.py", line 15, in __init__
    self.image = transform(image.load(img), (width, height))
TypeError: not a file object

If you know how to solve this problem, save image in variable "hero_img", and make character switch his image while pressing button, please help! :(

1 Answers
Related