easy way to check if an image exists?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
Thundy
Regular
Posts: 88
Joined: Tue Dec 05, 2017 9:08 am
Contact:

easy way to check if an image exists?

#1 Post by Thundy »

Hey folks.

I wanted to find out if anyone knew of an easy way to check whether or not an image exists?

I know that renpy.loadable() would provide me with one method but i was hoping for something a bit more elegant that doesn't require me to check all possible file extensions. i.e I want to check on the existence of this_picture and not this_picture.jpg OR this_picture.png OR this_picture.webm etc etc

many thanks in advance :)

martingerdes
Regular
Posts: 26
Joined: Sun Oct 07, 2018 5:14 am
Contact:

Re: easy way to check if an image exists?

#2 Post by martingerdes »

I'll admit that I'd be curious about that too.
Right now my code simply knows what the picture path is, if it exists:

Code: Select all

if not image_s:
  image_s="base/"+self.class_string_plural+"/"+self.base_name+".jpg"
  if not renpy.exists("images/"+image_s): image_s=None
self.image_o=placeholder_800x600 if image_s==None else Image(image_s)
so basically I am creating an image object from the path image_s. If I have not been given that argument, I build up an image path, and then check for its existence with renpy.exists by prepending "images/" (and use a placeholder if it is missing).
Sure, being able to check all the locations and externsions the same way the renpy code does would be cool, just wasn't important enough (since I know where my images are, if they exist at all).
If you figure out the python function to call to load an image without giving the path, you could just wrap it in a try / except block...

I'll update my code to use renpy.loadable instead of renpy.exists, thanks for that hint! :)

Post Reply

Who is online

Users browsing this forum: DewyNebula