Forum Archive

How to load a jpg file into Scene

upwart

I would like to use some jpg files in an application. How can I realize that?

Sebastian

Doesn't scene.load_image_file(image_path) work?

bashedcrab

There are multiple ways to do this but here is what I do.

  • Upload your .jpg/.png etc files to a photo sharing website (photobucket etc).
  • Take note of the complete url to access just the .jpg (It will look something like http://i427.photobucket.com/albums/pp359/haru2858/starwarscat.jpg)
  • Use urllib to open a link to the jpg.
  • Using the url, save the jpg to your filesystem.
  • Use PIL to load the jpg from your filesystem.
  • Use scene.load pil image() to use within your scene.

Write your "load_remote_image()" or whatever function to first check if the photo exists on the local file system. If it does exist, go directly to loading it with PIL, else download and save it first using urllib. This way if you share your code, anyone else who runs it will download the image if they don't have it locally. It also means you won't have to download the image every time your code is run.

ccc

Given that many of us use DropboxSync to backup/restore our scripts, you may already have JPEG files in a local "Camera Uploads" directory. If this is the case, you can try:

def get_ramdom_photo():  # returns the name (a string) of a random photo from a local directory
    file_list = None
    image_dir = '../Camera Uploads'
    try:
        file_list = [f for f in os.listdir(image_dir) if f.endswith('.jpg')]
    except OSError:  # image_dir not found
        pass
    if file_list:
        return  scene.load_image_file(image_dir + '/' + random.choice(file_list))
    return None
coolius

If you open the image in Pythonista, it will appear in the 'Inbox' folder. Move the image to you working folder, then simply use the image() function with name and extension.
Works for me.