Alternative to image_size in a CG Gallery

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
User avatar
amaturemanga
Regular
Posts: 94
Joined: Sun Mar 08, 2015 6:55 pm
Deviantart: amaturemanga
Skype: amature.manga
Contact:

Alternative to image_size in a CG Gallery

#1 Post by amaturemanga » Fri Oct 23, 2020 9:39 pm

Hi there i was following Empish's tutorial on youtube for a CG Gallery and it works just fine however she did note that renpy.image_size is really slow. I didnt notice any delay when i launched the game. However i was wondering what would be the alternative to doing that. This is what i have note all my images are 1280 x 720 and the thumbs are 267 x 194

Images

Code: Select all

##############################CG For Gallery##########################
image img1 = "main_menu.png"
image img2 = "CG1.png"
image img3 = "CG2.jpg"
image img4 = "CG3.jpg"
image img5 = "CG4.jpg"
image img6 = "CG5.png"
image img7 = "CG6.png"
image img8 = "CG7.png"
##############################Thumbnails##############################

image lockedthumb = "thumbnail_locked.png"
image thumb1 = "thumbnail_main_menu.png"
image thumb2 = "thumbnail_CG1.png"
image thumb3 = "thumbnail_CG2.jpg"
image thumb4 = "thumbnail_CG3.jpg"
image thumb5 = "thumbnail_CG4.jpg"
image thumb6 = "thumbnail_CG5.png"
image thumb7 = "thumbnail_CG6.png"
image thumb8 = "thumbnail_CG7.png"
Galley.rpy

Code: Select all

screen gallery:
    tag menu

    add "gui/game_menu.png"

    $start = gallery_page * maxperpage
    $end = min(start + maxperpage - 1, len(gallery_items) - 1)

    grid maxnumx maxnumy:
        xfill True
        yfill True

        for i in range(start, end + 1):
            $gallery_items[i].refresh_lock()
            if gallery_items[i].is_locked:
                add gallery_items[i].locked:
                    xalign 0.5
                    yalign 0.5
            else:
                imagebutton idle gallery_items[i].thumb:
                    action Show("gallery_closeup", dissolve, gallery_items[i].images)
                    xalign 0.5
                    yalign 0.5

        for i in range(end - start + 1, maxperpage):
            null

    if gallery_page > 0:
        textbutton "Previous":
            action SetVariable("gallery_page", gallery_page - 1)
            xalign 0.1
            yalign 0.95
    if (gallery_page + 1) * maxperpage < len(gallery_items):
        textbutton "Next":
            action SetVariable("gallery_page", gallery_page + 1)
            xalign 0.9
            yalign 0.95

    textbutton "Return" action Return() xalign 0.03 yalign 0.95

screen gallery_closeup(images):
    imagebutton idle images[closeup_page] at truecenter:
        action [SetVariable("closeup_page", 0), Hide("gallery_closeup", dissolve)]
gallery_setup.rpy

Code: Select all

init python:

    def MaxScale(img, minwidth=config.screen_width, minheight=config.screen_height):
        currwidth, currheight = renpy.image_size(img)
        xscale = float(minwidth) / currwidth
        yscale = float(minheight) / currheight

        if xscale > yscale:
            maxscale = xscale
        else:
            maxscale = yscale

        return im.FactorScale(img, maxscale, maxscale)

    def MinScale(img, maxwidth=config.screen_width, maxheight=config.screen_height):
        currwidth, currheight = renpy.image_size(img)
        xscale = float(maxwidth) / currwidth
        yscale = float(maxheight) / currheight

        if xscale < yscale:
            minscale = xscale
        else:
            minscale = yscale

        return im.FactorScale(img, minscale, minscale)

    maxnumx = 4
    maxnumy = 2
    maxthumbx = config.screen_width / (maxnumx + 1)
    maxthumby = config.screen_height / (maxnumy + 1)
    maxperpage = maxnumx * maxnumy
    gallery_page = 0
    closeup_page = 0

    class GalleryItem:
        def __init__(self, name, images, thumb, locked="lockedthumb"):
            self.name = name
            self.images = images
            self.thumb = thumb
            self.locked = locked
            self.refresh_lock()

        def num_images(self):
            return len(self.images)

        def refresh_lock(self):
            self.num_unlocked = 0
            lockme = False
            for img in self.images:
                if not renpy.seen_image(img):
                    lockme = True
                else:
                    self.num_unlocked += 1
            self.is_locked = lockme

    gallery_items = []
    gallery_items.append(GalleryItem("Image 1", ["img1"], "thumb1"))
    gallery_items.append(GalleryItem("Image 2", ["img2"], "thumb2"))
    gallery_items.append(GalleryItem("Image 3", ["img3"], "thumb3"))
    gallery_items.append(GalleryItem("Image 4", ["img4"], "thumb4"))
    gallery_items.append(GalleryItem("Image 5", ["img5"], "thumb5"))
    gallery_items.append(GalleryItem("Image 6", ["img6"], "thumb6"))
    gallery_items.append(GalleryItem("Image 7", ["img7"], "thumb7"))
    gallery_items.append(GalleryItem("Image 8", ["img8"], "thumb8"))

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3636
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

Re: Alternative to image_size in a CG Gallery

#2 Post by Imperf3kt » Sat Oct 24, 2020 2:03 am

If you're talking about im.FactorScale, I haven't used it enough to say for sure, though I do believe it is advised not to use it if possible.
I haven't noticed any issues myself, but I only have a dozen or so images in the project I use it in.

The reason it would be slow, I assume, is because Ren'Py will have to resize your images before it can load them into RAM, compared to just loading already properly sized thumbnail images.
I don't think this would be noticeable though unless you have dozens of images on screen at once.


If you're concerned about potential slow loading or delays when opening the gallery screen caused by this, an alternative would be to create thumbnails in your image editing program and package them with your game, and use the thumbnails instead of the resized images the code produces
The trade-off is that you will increase the amount of physical disc space your game will require.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor
Free Android GUI - Updated occasionally
Twitter
Imperf3kt Blackjack - a WIP blackjack game for Android made using Ren'Py

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Alternative to image_size in a CG Gallery

#3 Post by Remix » Sat Oct 24, 2020 9:10 am

You are not using that code.
Your code defines each thumbnail as a physical file so does not use the dynamic resizing which might be slower.

renpy.image_size is only (comparatively) slow because it has to read the file from disk...

If you were using im.FactorScale (or any other method to scale) you need to read the file anyway, so there should be no additional time taken (as it has already been read)
## Edit
Scratch that, it seems renpy.image_size does not use the cache, so it would add another load cycle (and should mostly be avoided if possible)

In the coming Ren''Py 7.4 (iirc) there should be extra facilities to scale images to within bounds (with triggers to indicate how to manage the scaling)

As yours are all the same initial size and all are cropped and scaled to a set size, you could just transform them... Though doing as you are (creating them as physical files and defining them) is obviously best.
Last edited by Remix on Sat Oct 24, 2020 1:55 pm, edited 1 time in total.
Frameworks & Scriptlets:

User avatar
amaturemanga
Regular
Posts: 94
Joined: Sun Mar 08, 2015 6:55 pm
Deviantart: amaturemanga
Skype: amature.manga
Contact:

Re: Alternative to image_size in a CG Gallery

#4 Post by amaturemanga » Sat Oct 24, 2020 11:44 am

Remix wrote:
Sat Oct 24, 2020 9:10 am
You are not using that code.
Your code defines each thumbnail as a physical file so does not use the dynamic resizing which might be slower.

renpy.image_size is only (comparatively) slow because it has to read the file from disk...

If you were using im.FactorScale (or any other method to scale) you need to read the file anyway, so there should be no additional time taken (as it has already been read)

In the coming Ren''Py 7.4 (iirc) there should be extra facilities to scale images to within bounds (with triggers to indicate how to manage the scaling)

As yours are all the same initial size and all are cropped and scaled to a set size, you could just transform them... Though doing as you are (creating them as physical files and defining them) is obviously best.
so basically it should be fine the way it is? the main thing i was concerned about is if the reason was why i didnt notice any delay is because my computer really fast and someone who buys my game will experience the delay.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Alternative to image_size in a CG Gallery

#5 Post by Remix » Sat Oct 24, 2020 1:54 pm

You are creating your own thumbnails, so there is no overhead from dynamically resizing them in game. All good.
Frameworks & Scriptlets:

Post Reply

Who is online

Users browsing this forum: Bing [Bot]