Specifying variable image file in blur

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
HammeredEnt
Regular
Posts: 33
Joined: Sat May 04, 2019 8:09 am
Contact:

Specifying variable image file in blur

#1 Post by HammeredEnt »

Hi team!

I just discovered the blur function for images and want to apply it to all the backgrounds in my scenes once we get into a dialogue scene, but without having to set up code for each and every possible location. I keep all my backgrounds in a folder called 'bgs' in the image folder and they all follow the naming convention of 'bg LOCATIONNAME.jpg'. I thought I'd be clever and set up a variable called 'stloc' that would mirror that one variable aspect of the background naming convention , so it could just be plugged in to a bit of code like this:

Code: Select all

image storyback = im.Blur('bgs/bg [stloc].jpg', 1.5)
And then just have 'storyback' as the image used for the scene. Of course, that doesn't work, returning '"OError: Couldn't find the file 'bgs/bg [stloc].jpg'."

If I do it for a specific image like the 'bedroom' location:

Code: Select all

image storyback = im.Blur("bgs/bg bedroom.jpg", 1.5)
then it works fine, but of course then I'd have to code up a line for every background which seems excessive. So I think it's just how I incorporate that variable into the image file name on the blur, but I'm having trouble on figuring out the how of that. Help?

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Specifying variable image file in blur

#2 Post by RicharDann »

im.Blur does require a specific path for the image, so using image statement will not work, however if you use the same convention for all of your backgrounds, you could try something like this:

Code: Select all

init python:
    # A function that receives a string and optional xrad and yrad parameters, and returns a blurred image formatted with those
    def bg_blur(img, xr=1.5, yr=None):
        return im.Blur("bgs/bg {}.jpg".format(img), xr, yr)

Then in your script you can use that like:

Code: Select all

label start:
    scene expression bg_blur("bedroom")
The most important step is always the next one.

HammeredEnt
Regular
Posts: 33
Joined: Sat May 04, 2019 8:09 am
Contact:

Re: Specifying variable image file in blur

#3 Post by HammeredEnt »

Thanks for your help RicharDann, that works! Certainly more efficient than what I would have otherwise done, so you have my gratitude for that!

Is there any way to incorporate the 'location' variable into the expression you've come up with at all? I thought to use the same sort of code to add a blurred background of the current location when the player activates a cellphone interface, but I haven't succeeded with my 'trial and error' attempts (just runs into the same problem I had in the first place) and can't see anything in the documentation that jumps out as being useful in terms of that.

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Specifying variable image file in blur

#4 Post by RicharDann »

Is there any way to incorporate the 'location' variable into the expression you've come up with at all?
If I'm understanding this question correctly, you want to specify the location using a variable? You can just use the variable itself as parameter:

Code: Select all

    $ current_location = "bedroom"
    
    scene expression bg_blur(current_location) 
The most important step is always the next one.

HammeredEnt
Regular
Posts: 33
Joined: Sat May 04, 2019 8:09 am
Contact:

Re: Specifying variable image file in blur

#5 Post by HammeredEnt »

Ah-hah! Got it! Sorry, was having all sorts of unnecessary issues trying to get that to work through tricky means when it really didn't need to!

Thanks very much for your help - it'll end up being a subtle feature but it just adds a nice bit of polish to what I'm trying to do. Thanks again!

Post Reply

Who is online

Users browsing this forum: No registered users