Ren'Py Getting dimensions of an image after .Render() and .blit()

Tribe

Member
Game Developer
May 5, 2021
226
492
Merry Christmas! :coffee:

I have a class that resizes any image to fit into a specific space (given to me by someone we all know and love but slightly modified). I'm creating a screen that requires frames for these images, so I need to know the dimensions of the images after they've been resized. The code is as follows:


Python:
python:
    class Event_Resize(renpy.Displayable):
        def __init__(self, img, width=800, height=450, **kwargs):

            self.img = img
            self.width   = float( width )
            self.height  = float( height )

            super(Event_Resize, self).__init__(**kwargs)

        def render(self, width, height, st, at):

            name = renpy.substitutions.substitute( self.img, translate=False )[0]
            if not renpy.loadable( name ):
                raise ValueError( "Image '{}' can NOT be found".format( name ) )
            img  = renpy.easy.displayable( name )

            src = renpy.display.im.cache.get( img )

            wBase, hBase = src.get_size()

            rW = self.width  / wBase if wBase > self.width  else 1.0
            rH = self.height / hBase if hBase > self.height else 1.0
            f = min( rW, rH )
            rWidth  = int( wBase * f )
            rHeight = int( hBase * f )

            if wBase < self.width and hBase < self.height:
                rW = self.width  / wBase if wBase < self.width  else 1.0
                rH = self.height / hBase if hBase < self.height else 1.0
                f = min( rW, rH )
                rWidth  = int( wBase * f )
                rHeight = int( hBase * f )

            src = renpy.display.scale.real_bilinear( src, ( rWidth, rHeight ) )

            r = renpy.Render( rWidth, rHeight )

            r.blit( src, ( 0, 0 ) )

            return r
Code:
screen screen event_screen():
    add frame_I_wish_could_go_here
    add event_actor1

label start:
    $ event_actor1 = Event_Resize(event_actor1)
    call screen event_screen()
Ideally, the class would also fork out some dimensions for me to apply to a frame but I'll settle for something that just gives me the numbers so I can get this thing done. If anyone could help me by shedding some light on how to do this I would deeply appreciate it.
Thanks everyone. :cry:

Edit: I suppose I should also mention that I didn't just post this on a whim. I've tried .get_size() and returning the dimensions from render() within the class and all sorts of other fun stuff. (Stuff that I'm too ashamed to post here.) Thanks again.
 
Last edited:

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,966
16,214
I have a class that resizes any image to fit into a specific space
You mean, like , or like using the xsize/ysize properties ?


I'm creating a screen that requires frames for these images, so I need to know the dimensions of the images after they've been resized
Why ?

But well, make rWidth and rHeight available as attributes, and use the property, it should be enough.



Ideally, the class would also fork out some dimensions for me to apply to a frame but I'll settle for something that just gives me the numbers so I can get this thing done.
I'm on my way out to go visit my daughter for Christmas, so I can't test it, and I can have read too fast.
But I'm pretty sure that if you apply a size to a frame, through the said xsize and ysize, the content will be proportionally resized. What mean that you don't need the custom resize class, nor do you need the exact size of the resized image. As long as the images have the same ratio, and are displayed in the same frame, they'll have the same final size.
 
  • Star-struck
Reactions: Tribe

Tribe

Member
Game Developer
May 5, 2021
226
492
You mean, like , or like using the xsize/ysize properties ?

But well, make rWidth and rHeight available as attributes, and use the property, it should be enough.

I'm on my way out to go visit my daughter for Christmas, so I can't test it, and I can have read too fast.
But I'm pretty sure that if you apply a size to a frame, through the said xsize and ysize, the content will be proportionally resized. What mean that you don't need the custom resize class, nor do you need the exact size of the resized image. As long as the images have the same ratio, and are displayed in the same frame, they'll have the same final size.
Thank you for your reply. I wish you the best during the holiday. Your response has helped me explain it better, but I feel this is still a case of me "not knowing how to ask the right question."

The class scales random images with random dimensions to fit a given space. The background for the screen varies wildly as well, and I need frames to ensure that they are visible in conjunction with the random images. As a frame, I'm using a simple solid black .jpg that I want to display behind the images 20 pixels larger than whatever the resize class outputs.

I have no idea what the image's width will be once the screen is displayed and, in rare cases, the ysize may vary as well. I could easily use xsize ysize if the image wasn't being altered or if I knew how to tell ren'py to tell me how large the image is, but I can't seem to find anything that works (such as get_size()). When I try to return rWidth and rHeight from the render() function, I get an error that says I'm not allowed to unpack it. I can't seem to find anything that tells me what size the image was changed to after I run it through the resize class.

Ideally, I'd just take rWidth +20 and rHeight +20 and display the black .jpg 10 pixels up and to the left of the image, but I can't get it to spit out those numbers.
 
Last edited: