Page 1 of 1

Can I return the width of an image?

Posted: Sat Jun 19, 2010 12:23 pm
by kuroi
Hi out there in Renpy land~

I'm working on an update to the Kuroi Games manga engine and I can't seem to find a rather simple function that I'd like to use. Is there a function in Renpy which will return the width in pixels of an image? I was hoping to find something such as:

Code: Select all

image p0f = "frames/intmanga000f.png"
$ p0f_width = p0f.getwidth()
It's simple enough that I have to assume that Renpy has a function which does this that is used somewhere in the guts of the code but I can't for the life of me find it in the Renpy documentation. Does anyone have any ideas? Anyone else had to track down this function and could maybe point me in the right direction?

Thanks a ton
~Kuroi

Re: Can I return the width of an image?

Posted: Sat Jun 19, 2010 1:10 pm
by Jake
kuroi wrote:Hi out there in Renpy land~

I'm working on an update to the Kuroi Games manga engine and I can't seem to find a rather simple function that I'd like to use. Is there a function in Renpy which will return the width in pixels of an image?
~Kuroi
The short version is "yes, but it's buried fairly deep". Here's a thread in which I explained the method to Jack Norton a while ago - I get the impression that PyTom doesn't really expect people to do stuff like this, you're normally supposed to just know the sizes of the images you load in before you load them.

Re: Can I return the width of an image?

Posted: Sat Jun 19, 2010 4:12 pm
by kuroi
Thank you!! I'm always happy to find a use programing in a way that means I don't actually have to know something. Soon I'll have built enough code that I'll never have to know anything at all!! MUAHAHAHAHA!!

Re: Can I return the width of an image?

Posted: Sun Jul 04, 2010 2:10 pm
by kuroi
After using this method to find the size of a displayable within my game code, I've found that this works really well with a single terribly annoying exception.

I find that I cannot use this method to find the size of a displayable during init.

When attempt to call a function containing this code during init such as:

Code: Select all

init -10 python:
def make_lcomp(myimage):
myDisplayable = im.Image(myimage)
                myRender = renpy.render(myDisplayable, 800, 600, 0, 0)
                sizes = myRender.get_size()
return LiveComposite ( sizes, (0,0), myimage)

init:
my_lcomp = make_lcomp("images/myimage.png")
If I were to call this code, I would recieve the error:
AttributeError: 'NoneType' object has no attribute 'frame_time'

I've not been able to find a way around this and it would be kinda rough having to manually enter the sizes of all of the images which I'm trying to use this function for. Is there perhaps a way around this?

lol The poor girl who's doing the data entry part of the manga engine is pretty overworked as it is.

Re: Can I return the width of an image?

Posted: Sun Jul 04, 2010 2:55 pm
by PyTom
If we restrict this to only be image manipulators, the answer is yes. You can run the code:

Code: Select all

renpy.image_size("foo.jpg")
and it will load foo.jpg and return the size. This is a fairly terrible thing to do, though, as it means your game will have to read in all the data during start up, which could slow startup quite a bit. The init code runs before the splashscreen, so they'll only be the presplash to keep them entertained. Also, this will often be read from a cold disk cache.

Re: Can I return the width of an image?

Posted: Sat Dec 05, 2015 10:04 am
by barsunduk
PyTom wrote:

Code: Select all

renpy.image_size("foo.jpg")
unfortunately it doesn't working if using «build.classify('game/**', 'archive')» to hide game content.

Re: Can I return the width of an image?

Posted: Sat Oct 29, 2022 7:56 am
by barsunduk
I still haven't found an answer. Can anyone help?

Image

Code: Select all

init python:
    def get_size(displayable):
        w, h = renpy.render(displayable, 0, 0, 0, 0).get_size()
        return int(w), int(h)

    box = Crop((0, 0, 580, 280), "#fff4")

label start:
    $ t = "Some text. Some text. Some text. Some text. Some text. Some text. Some text. Some text. Some text. Some text. Some text. Some text. Some text. Some text. Some text. Some text. Some text. Some text. Some text. Some text."

    $ txt = Text(t, xysize=(580, 280))

    $ wh = get_size(txt)

    $ wh2 = get_size(box)

    show expression box at truecenter
    show expression txt at truecenter

    "Text size: [wh] WRONG!\nGrey rectangle: [wh2] RIGHT"

    return

Re: Can I return the width of an image?

Posted: Sat Oct 29, 2022 10:44 am
by _ticlock_
barsunduk wrote: Sat Oct 29, 2022 7:56 am I still haven't found an answer. Can anyone help?
It likely shows the wrong size because you put 0,0 as width and height in the render. You can try the following:

Code: Select all

init python:
    def get_size(displayable):
        w, h = renpy.render(displayable, config.screen_width, config.screen_height, 0, 0).get_size()
        return int(w), int(h)
Although, I am not sure if it is a good idea to use render in the function like that. What are you trying to do?

I would suggest creating a simple CDD, that would do similar function. For example, a displayable that allocates text on top of the background image based on the background width,height. It would also look cleaner in the label flow.

Re: Can I return the width of an image?

Posted: Sun Oct 30, 2022 1:05 am
by barsunduk
_ticlock_ wrote: Sat Oct 29, 2022 10:44 am

Code: Select all

init python:
    def get_size(displayable):
        w, h = renpy.render(displayable, config.screen_width, config.screen_height, 0, 0).get_size()
        return int(w), int(h)
What are you trying to do?
THANK YOU VERY MUCH!
I'm trying to make my analogue of the NVL mode in the form of books with two pages on the screen. But for ADV mode. So that any text can also be viewed in book mode. But to automatically split text into pages, I need to know the height of paragraphs with a given width.

Re: Can I return the width of an image?

Posted: Mon Nov 07, 2022 6:38 am
by barsunduk
_ticlock_ wrote: Sat Oct 29, 2022 10:44 am

Code: Select all

        w, h = renpy.render(displayable, config.screen_width, config.screen_height, 0, 0).get_size()
Thanks to you, everything worked out. Now the same script can be played in the game or read as a book.