# How to load an image in python?

**URL:** <https://discourse.processing.org/t/how-to-load-an-image-in-python/17710>\
**Category:** Processing.py\
**Created:** [February 11, 2020, 2:03am UTC](https://discourse.processing.org/t/how-to-load-an-image-in-python/17710 "2020-02-11T02:03:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [February 11, 2020, 2:03am UTC](https://discourse.processing.org/t/how-to-load-an-image-in-python/17710/1 "2020-02-11T02:03:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [February 11, 2020, 2:07am UTC](https://discourse.processing.org/t/how-to-load-an-image-in-python/17710/2 "2020-02-11T02:07:38Z")

</div>

For a static (non-animated) sketch, you can load and render an image in two lines:  
[https://py.processing.org/reference/image.html](https://py.processing.org/reference/image.html)

Technically speaking, you could do it in one line:  
`image(loadImage("laDefense.jpg"), 0, 0)`

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [February 11, 2020, 2:15am UTC](https://discourse.processing.org/t/how-to-load-an-image-in-python/17710/3 "2020-02-11T02:15:21Z")

</div>

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

war

```auto
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

```

warrior

```auto
class Warrior:
    health = 50
    attack = 5
    isAlive = True
    
    def show(self):
        image(loadImage("warrior.png"), 0, 0)

```

knight

```auto
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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 11, 2020, 5:43am UTC](https://discourse.processing.org/t/how-to-load-an-image-in-python/17710/4 "2020-02-11T05:43:47Z")

</div>

> [@ctrembla](#):
>
> I’m having trouble getting the image to load.

- 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:

```auto
# 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[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 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

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [February 11, 2020, 11:37pm UTC](https://discourse.processing.org/t/how-to-load-an-image-in-python/17710/5 "2020-02-11T23:37:40Z")

</div>

> [@GoToLoop](#):
>
> Best place for loading assets is within **setup()** btW. Not in classes or in **draw()**.

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.
