Can anyone explain how to load an image in python. I looked at a few videos & they use like 5 lines of code or more just to get a single image. so if I do that for all my images that be tons of lines for just images.
For a static (non-animated) sketch, you can load and render an image in two lines:
Technically speaking, you could do it in one line:
image(loadImage("laDefense.jpg"), 0, 0)
tyvm you saved the day. I was losing hope for python. I’m having trouble getting the image to load. I’m trying to see who is fighting but it says no attribute ‘show’. Here the code
from Warrior import * from Knight import * chuck = Warrior() bruce = Warrior() carl = Knight() dave = Warrior() def setup(): size(600,600) def draw(): background(0) fight(chuck,bruce) def fight(unit1, unit2): unit1.show() round = 0 if round == 0: unit2.health -= unit1.attack if unit2.health <= 0: return True if unit2.health > 0: round = 1 if round == 1: unit1.health -= unit2.attack if unit1.health <= 0: return False if unit1.health > 0: round = 0
class Warrior: health = 50 attack = 5 isAlive = True def show(self): image(loadImage("warrior.png"), 0, 0)
from Warrior import * class Knight(Warrior): attack = 7 def show(self): image(loadImage("knight.png"), 0, 0)
i’m trying to get the show to happen in the fight
- As a general rule, assets should be loaded once before draw() starts.
- Best place for loading assets is within setup() btW. Not in classes or in draw().
- Here’s my take on your posted code:
# https://Discourse.Processing.org/t/how-to-load-an-image-in-python/17710/4 # 2020/Feb/11 FILES = 'warrior.png', 'knight.png' troop =  def setup(): size(600, 600) global images images = tuple(loadImage(img) for img in FILES) Warrior.img = images Knight.img = images cy = height >> 1 troop.append(Warrior('Chuck', 0, cy)) troop.append(Warrior('Bruce', 100, cy)) troop.append(Knight('Carl', 200, cy)) troop.append(Warrior('Dave', 300, cy)) for i in range(8): troop.fight(troop) for t in troop: print t.show() class Warrior(object): img = None def __init__(self, name, x, y): self.health = 50 self.attack = 5 self.alive = True self.name = name self.x = x self.y = y def __str__(self, STR = 'Name: %s, Health: %d, Attack: %d, Alive: %s'): return STR % (self.name, self.health, self.attack, self.alive) def show(self): image(self.img, self.x, self.y) return self def fight(self, other): self.health -= other.attack other.health -= self.attack self.alive = self.health > 0 other.alive = other.health > 0 return self class Knight(Warrior): def __init__(self, name, x, y): super(Knight, self).__init__(name, x, y) self.attack = 7
Yes! And this is because, if you load your images in setup, then your computer opens the files and reads them into memory once (slow). When it displays them, it just checks memory (fast).
If you put loadImage in show, then your computer opens the file and reloads it into memory every single time you call show – which may be every time you call draw – maybe 60+ times a second! Very, very, very slow.