Forum Archive

Trouble using imported images from Photos

iOSBrett

Hi,

I am currently teaching some young kids to code using Pythonista, our current project is Pac Man. As the kids are young I am having to take some shortcuts. For instance instead of building the background with tiles, I just want to load it from an image.

So I have saved some images from the internet and am attempting to load them into my Scene as a SpriteNode. I can import them into Pythonista without any issue, however when I try to use them in the code I get strange errors. Sometime it is ‘could not load image’, but other times it gives errors on future lines, such as if you forget a semi colon.

My image is in the same folder as the code, and I have tried lots of different images. One time one image worked, so I deleted it and tried again (need to ensure it works for kids) and it failed.

I am on iOS11 with the latest version of Pythonista. Has anybody ne experienced this issue or have an ideas?

‘’’
from scene import *
import sound
import random
import math
from joystick import Joystick
from player import Player
from tile import Tile
from direction import Direction

A = Action

class MyScene (Scene):
def setup(self):

    self.background_color = '#000000'

    self.background = SpriteNode('IMG_0204.PNG')
    self.background.position = self.size.width / 2, self.size.height / 2
    self.add_child(self.background)

‘’’

JonB

you might try loading the image outside the scene, using ui.Image.named. if that fails, catch the error, and print out os.listdir('.') to ensure the image and curpath are where you think they are.

then, iirc, you can pass the ui.Image to SpriteNode.

iOSBrett

i added a try catch block, wasn’t sure how to catch the error though. But strangely even trying to list the current dir is not working. I’m not much of a python programmer, am a Swift coder, but I realise now that the strange errors are actually due to it throwing an exception in the init method. My errors happen in other methods due to the rest of the init method not being run.

I did:

‘’’
class MyScene (Scene):
def setup(self):

    self.background_color = '#000000'
    #print(os.listdir('.'))

    try:
        self.image = ui.Image('IMG_0204.PNG')
    except:
        print(os.listdir('.'))

‘’’

I am now thinking it may be something to do with directory length. This project is anpbout 5 sub folders deep.

cvp

@iOSBrett ui.Image.named('IMG_0204.PNG')

iOSBrett

Tried that too, same issue, thanks anyways.

cvp

@iOSBrett
except Exception as e: print(str(e))

iOSBrett

Thanks for that, exception is ‘could not load texture’

Had to remove ui.image.named as I couldn’t convert it to a texture for SpriteNode

Exception doesn’t really help us much, this is a weird one.

‘’’
try:
self.background = SpriteNode('IMG_0202.PNG')
except Exception as e:
print(str(e))
‘’’

cvp

@iOSBrett I have the same problem but only with big images, try with a litlle one, just to be sure. If ok, you will need to resize your photos

iOSBrett

It definitely doesn’t work with big images, but I have same problem even with 200x300 images.

JonB

did you try the stuff with listdir, and os.path.exists? (actually, should have checked for ui.Image.named(filename) is None -- Image fails silently when the file is not found, and just returns None.

The error you are getting is the same one when a file does not exist, so we need to confirm the file does exist before going any further.

So, check if Image.named returns an image, and do that outside of setup, as scene error handling is funky. Then print os.abspath('.') and print(os.listdir('.'))

how did you get the file into pythonista?

cvp

@JonB Hello, no, it is not a problem of "file not found" but file too big. I've tried with two files in the root, one of 5MB gives the problem, the other of 40KB does not have any problem

JonB

That error can be both. So it is important to rule out path issues first.

For large images, does named fail when outside of scene? If not, try creating the SpriteNode outside of the scene.

Have you tried using jpg instead of png? Jpg uses MUCH less memory.

cvp

@JonB My big image is a jpeg. It can be loaded in an ui.Image via named, even during the initial of the Scene but the error *OSError: could not load texture" occurs when set as SpriteNode

import ui
from scene import *

class MyScene (Scene):
    def setup(self):
        self.background_color = '#000000'
        ui_image = ui.Image.named('IMG_5903.JPG')   # 5 MB 3672x4896 px
        self.background = SpriteNode(ui_image) # corrected 😢
        self.background.position = self.size.width / 2, self.size.height / 2
        self.add_child(self.background)

run(MyScene())
mikael

@cvp, @iOSBrett, is there a chance for you to share a problem picture to experiment on? Preferably also a smaller one.

cvp

@mikael I'll try but don't forget I only do that to help, personally, I never use Scene nor SpriteNode

cvp

@mikael Is 5MB too big?

cvp

I have the same problem with photo which is an upload of a photo of 4 MB 1536x2048 px, but on Imgur: only 43 KB 640x853 px
Thus not too big, perhaps content problem

Edit: all examples of SpriteNode texture images I see on the net are "simple" images, not detailed images. I know I don't know anything about this matter, thus I only wanted to help but I obviously choosed an image not designed for textures. I'm sure I can't help, and I'm sorry to have be part in this topic, perhaps troubling the discussion.

mikael

@cvp, I had no trouble with your picture. Probably it got processed somehow by imgur.

Maybe if @iOSBrett would share his picture?

cvp

@mikael I have trouble with the same picture after download from Imgur...it takes 43 KB, is it the same for you?

cvp

Sorry for all, I'm not proud of me. My little script loads an ui.Image but I had let the image name in the SpriteNode line. I'll go back in my bed.

cvp
import ui
from scene import *

class MyScene (Scene):
    def setup(self):
        self.background_color = '#000000'
        ui_image = ui.Image.named('IMG_5903.JPG')   # 5 MB 3672x4896 px   **** Error
        #ui_image = ui.Image.named('IMG_3735.JPG')  # 4 MB 1536x2048 px **** ok
        #ui_image = ui.Image.named('IMG_6575.JPG')  # 43 KB 640x853 px  **** ok
        self.background = SpriteNode(ui_image)
        self.background.position = self.size.width / 2, self.size.height / 2
        self.add_child(self.background)

run(MyScene())

No error with photo of 4 Mb but error with photo of 5 Mb

cvp

Big photo is here

JonB

what if you create the SpriteNode outside of the scene?

cvp

@JonB idem

JonB

ohh, ok. scene Textures are opengl based, i think. So you cannot have a Texture that is larger in pixels that the graphics memory region, which is something like twice largest screen size (for retina displays).

with ui.ImageContext(1024*2,1024*2) as ctx: #ui.Image.named('IMG_0625.JPG').draw() Texture(ctx.get_image())
The above works, on my Ipad3. Anything up to 2048 in either row/col works, but even 2049x1 fails. so, that tells me the opengl textures must be <=2048 in either dimension.

cvp

@JonB that's ok, thanks for @iOSBrett

Drizzel

I‘m having a somewhat similar issue, I can’t even load an image that’s 511 kB in size. I haven’t used the scene module for some time now and my memory is a bit rusty, but the following code should run without an issue, right?
Here it is:

from scene import *
import os

A = Action
class MyScene (Scene):
    def setup(self):
        img = 'rectangle_1.png'
        self.background_color = 'white'
        if img in os.listdir(): print('yes')    #prints 'yes' in console
        print(ui.Image.named('IMG_5903.JPG')) #prints None in console
        #a = Texture(img) #Image not found
        a = SpriteNode(img) #could not load texture
        self.add_child(a)
        pass

if __name__ == '__main__':
    run(MyScene(), show_fps=False)

pulbrich

Wouldn’t you need an

import ui

before using ui.Image?

JonB

Try

    img = 'rectangle_1.png'
    self.background_color = 'white'
    if img in os.listdir(): print('yes')            
    printt(ui.Image.named(img))

You were checking one file, but trying to open a different one

Drizzel

@pulbrich no, it actually functions. The output is the same if I use “import ui” and when I don’t.

@JonB your suggestion prints the following:

yes
<_ui.Image object at 0x108fe9e40>

Drizzel

I figured out the issue. Whenever I import an image from my camera roll, it’s ending is in caps (.PNG). Renaming it to .png causes the issue, so I’m just going to not do that :)