Automagically recreate project with smaller image sizes

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
Ozitiho
Regular
Posts: 90
Joined: Mon Sep 22, 2014 3:29 pm
Location: Netherlands
Contact:

Automagically recreate project with smaller image sizes

#1 Post by Ozitiho »

I exported all of my project assets in 4k resolution, even though my default window size is 720. Ren'Py doesn't agree with this.
Don't misunderstand, I'm not admitting my mistake. I haven't yet given up on having 4K assets. But until I find a solution,..

This python script creates a clone of a given input folder at a given output location. In the cloned folder, all files are at a new resolution.

Executing this requires a certain level of comfort with command line to execute. If you want to use this script but don't know how, leave a reply here and I'll be happy to help. :)

Code: Select all

import os
from PIL import Image
from resizeimage import resizeimage	
from resizeimage.imageexceptions import ImageSizeError	
from shutil import copyfile

import sys, getopt

inputdir = ''
outputdir = ''
height = 0;
width = 0;

def printHelp(illegal):
	if(illegal):
		print 'Illegal argument detected. Allowed inputs are: '
	print '-h: displays this'
	print '-i: path to Ren\'Py project'
	print '-o: path to clone to'
	print '-h: heigth to resize to'
	print '-w: width to resize to'
	print 'NOTE: If a height and width is defined, only height will be used'
	if (illegal):
		sys.exit(2)
	sys.exit(0)

def getArgs(argv):
	global inputdir
	global outputdir
	global height
	global width

	try:
		opts, args = getopt.getopt(argv,"i:o:h:w:",["idir=","odir=","height=","width=","help"])
	except getopt.GetoptError:
		printHelp(True)

	for opt, arg in opts:
		if opt == '--help':
			print 'test.py -i <inputdir> -o <outputdir>'
			printHelp(False)
		elif opt in ("-i", "--ifile"):
			inputdir = arg
		elif opt in ("-o", "--ofile"):
			outputdir = arg
		elif opt in ("-h", "--height"):
			height = int(arg)
		elif opt in ("-w", "--width"):
			width = int(arg)
	if not inputdir or not outputdir or not (height or width):
		print 'Please specify an input, an output and heigth or width. Call this file with --help for help.'
		sys.exit(2)
	return (inputdir, outputdir, height, width)

def ensuredir(directory):
    if not os.path.exists(directory):
        os.makedirs(directory)

def recursiveCrawl(path):
	global inputdir, outputdir, height, width
	projPath = inputdir + path
	ensuredir(outputdir + path);
	files = os.listdir(projPath)
	print projPath
	for file in files:
		filePath = projPath + '/' + file;
		if os.path.isdir(filePath):
			recursiveCrawl(path + '/' + file)
		elif file.lower().endswith(('.png', '.jpg', '.jpeg')):
			resizeImage(path + '/', file)
		else:
			copyfile(filePath, outputdir + path + '/' + file)

def resizeImage(path, file):
	global inputdir, outputdir, height, width
	with open(inputdir + path + file, 'r+b') as f:
		with Image.open(f) as image:
			try:
				if height:
					newImage = resizeimage.resize_height(image, height)
					newImage.save(outputdir + path + file, image.format)
				elif width:
					newImage = resizeimage.resize_width(image, height)
					newImage.save(outputdir + path + file, image.format)
			except ImageSizeError as err:
				copyfile(inputdir + path + file, outputdir + path + file)

def main(argv):
	args = getArgs(argv)
	# Crawl through all folders in input folder
	recursiveCrawl('')


if __name__ == "__main__":
	main(sys.argv[1:])

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

Re: Automagically recreate project with smaller image sizes

#2 Post by Imperf3kt »

Why 4k if you're only displaying them at 720 ?
4k is 5 times the resilution of 720
Seems like a massive waste. At least use 1920*1080

Or you could manually try 4k resolution by editing it in gui.rpy. Ren'Py will scale itself, though at smaller sizes, with such large assets, its going to look like dog crap.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Ozitiho
Regular
Posts: 90
Joined: Mon Sep 22, 2014 3:29 pm
Location: Netherlands
Contact:

Re: Automagically recreate project with smaller image sizes

#3 Post by Ozitiho »

I want my game to support players with any screen resolution. Big and small. I'm aware of the challenges involved with this.

Also: 4k surprisingly does not refer to the height of the resolution. 4k is only 3 times the resolution 720. Isn't that weird?

User avatar
gas
Miko-Class Veteran
Posts: 842
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Automagically recreate project with smaller image sizes

#4 Post by gas »

Ozitiho wrote:I want my game to support players with any screen resolution. Big and small. I'm aware of the challenges involved with this.

Also: 4k surprisingly does not refer to the height of the resolution. 4k is only 3 times the resolution 720. Isn't that weird?
'cause it refer to the width.

Anyway, batch procedures of a graphical editor... are not an option for mass resizes?
Even GIMP could to that.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

User avatar
Ozitiho
Regular
Posts: 90
Joined: Mon Sep 22, 2014 3:29 pm
Location: Netherlands
Contact:

Re: Automagically recreate project with smaller image sizes

#5 Post by Ozitiho »

I never thought about that. I guess anyone thinking of using this script could alternatively look into that.

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

Re: Automagically recreate project with smaller image sizes

#6 Post by Imperf3kt »

I was referring to the number of pixels.
1920*1080 is 2.25x as many pixels as 1280*720
4k (3840*2160) is double 1920*1080
If you math it, it comes out as 4.5x
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
gas
Miko-Class Veteran
Posts: 842
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Automagically recreate project with smaller image sizes

#7 Post by gas »

Imperf3kt wrote:I was referring to the number of pixels.
1920*1080 is 2.25x as many pixels as 1280*720
4k (3840*2160) is double 1920*1080
If you math it, it comes out as 4.5x
You're correct, before 4k anyway all screens measures was based by height not width, so someone could get misleaded by that (4k in the old system count 2100 aprox, so in fact just 3 times 720, while you're expecting 4000, so 5 times based on old standard XD).
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

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

Re: Automagically recreate project with smaller image sizes

#8 Post by Imperf3kt »

Ah, I forgot about that. Damn companies and stupid resolution standards anyway.

Can't we all just use mod16 and ignore every other resolution standard :P
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
ISAWHIM
Veteran
Posts: 318
Joined: Sun Nov 06, 2016 5:34 pm
Contact:

Re: Automagically recreate project with smaller image sizes

#9 Post by ISAWHIM »

Make two or three versions.

Low, Normal, 4K

Do a batch conversion for your images. First do 50% reduction from 4K to 1080p, then again for 33.33% reduction from 1080p, to get it to 720p. (Or 66% reduction to get 720p from 4K resolution.)

You have to change the screen-sizes for RenPy to do the calculations for the correct base-scale for all images. (Eg, you can't just swap smaller images or the images will all just be smaller as RenPy scales the 4K resolution down to 720p size and it scales every image and position and transition with the same scale.)

Eg, when you tell RenPy to place an image 800 pixels to the left, it does that based on the base-resolution, not the resolution of the resized game. If you use 1/3 sized images, you have to change all the fixed values to be 1/3 or it will still be 800 pixels to the left, which would be off the screen at 720p. If you don't change the resolution or the fixed values, the image will be 1/3 the size, floating at 800 relative pixels in a 4K resized screen on the 720p screen. (Thus, it would not be the correct size on the screen, but it would be in the correct location.)

The only way to get it to work the same for all devices, is to use 4K as your base resolution. Thus, everyone will HAVE to use 4K images for the game to scale correctly on a smaller-screen. (That is a LOT of wasted download for a 720p screen.)

However... You can do this...

For the actual 4K devices, use JPG compression of only 5% (95% quality).
For the 1080p... use 30% compression of the 4K image (70% quality)
For the 720p devices, use JPG compression of 80% of the 4K image (20% quality).

Why? Because at 1/3 rescaled resolution, you have nearly 9 pixels {squared} (3x3 pixels), to make-up one pixel on the screen. You will not see the horrible JPG artifacts at 720p, from the 4K image.

You can do a similar thing with PNG if you scale the images down to 720p, then back up to 4K, with "Cubic resize", when you go back up to 4K. That makes 3x3 blocks (9 square), which PNG will compress rather well as a "block", as if it were 720p resolution. Thus, that will also scale perfectly with lower "data-size". (4k to 1080p would be 2x2 or 4-square blocks and have similar LWZ data compression results.)

Then they just pick the image-pack they want for the game. (Set the base download with the low-quality, since that is smaller. The two higher quality would then download the replacement files, since they have the burden of desire for HD.)

Post Reply

Who is online

Users browsing this forum: No registered users