How to load an image in python?

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.

1 Like

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)

1 Like

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():
def draw():

def fight(unit1, unit2):
    round = 0
    if round == 0: -= unit1.attack
        if <= 0:
            return True
        if > 0:
            round = 1
    if round == 1: -= unit2.attack
        if <= 0:
            return False
        if > 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:
# 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[0]
    Knight.img = images[1]

    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[0].fight(troop[2])

    for t in troop: print

class Warrior(object):
    img = None

    def __init__(self, name, x, y): = 50
        self.attack = 5
        self.alive = True = name
        self.x = x
        self.y = y

    def __str__(self, STR = 'Name: %s, Health: %d, Attack: %d, Alive: %s'):
        return STR % (,, self.attack, self.alive)

    def show(self):
        image(self.img, self.x, self.y)
        return self

    def fight(self, other): -= other.attack -= self.attack

        self.alive = > 0
        other.alive = > 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.