Saturday, April 27, 2019

Downsizing textures

DAZ characters usually have many large and beautiful textures. This can be a problem if you try to render a scene on an old computer with limited RAM memory, because once the memory is full Blender comes to a halt. However, for many purposes the texture resolution is unnecessarily high. Therefore, I have provided a utility program that downsizes all textures in a given directory, and a button in Blender that replaces the original textures with the downsized ones.

Install OpenCV

The script which resizes images cannot be used from inside Blender, because it uses the OpenCV library which is not included in Blender python. Installing OpenCV is quite complex, but the instructions at https://www.learnopencv.com/ are very useful.

Windows: https://www.learnopencv.com/install-opencv-4-on-windows/
Mac: https://www.learnopencv.com/install-opencv-4-on-macos/
Other operating systems: https://www.learnopencv.com/?s=install.

On Windows you start by installing the following software:
  1. Visual Studio.
  2. CMake.
  3. Anaconda. I installed the Python 3 version but Python 2 will probably work too.
  4. Git for Windows.
The instructions refer to install scripts whose location is not immediately obvious. The Windows versions can be found at https://github.com/spmallick/learnopencv/tree/master/InstallScripts/Windows-4.

Usage

In Blender, import a DAZ scene as usual. Save the blend file and press Save Local Textures.  This creates a subdirectory called textures and copies all used textures there. Note that this button has been moved from the Finishing section to the Materials section in the Setup panel.

The downsizing script is called resize.py and is located in the standalone folder. Once OpenCV has been installed, open the command prompt, go to the standalone folder, and type

activate OpenCV-master-py3
python resize.py <path to texture directory> <resize steps>


This creates new textures whose resolution is 1/2^steps of the original resolution, where steps is an integer between 0 and 4. E.g., if the original image has 4096x4096 pixels and steps = 2, the downsized image has 1024x1024 pixels. The total number of pixels is thus reduced by a factor 16.

In Blender, press Resize Textures, select the number of steps, and press OK. Clearly the resized images must exist in the right directory.

Results

Here are the results on my computer for a scene with a single nude Genesis 8 Female:



                   Step Total size  Render time
Original 0 24.9 Mb 59.24 s
25% 2 3.79 Mb 55.92 s
6.25% 4 342 kb 56.23 s

When going to maximal downsizing, the total disk space occupied by the textures is reduced by more than a factor 70. This is less than the factor 256 that one would naively expect for a bitmap, because the images are stored in compressed formats such as jpg or png. On the other hand, the render time (with Cycles on Blender 2.79b) does not change significantly in this case, because all textures fit into RAM memory. In realistic scenes, which contain multiple characters with hair and clothes, the original textures may fill up RAM memory and Blender starts swapping to disk. In that case the reduction in render time will be dramatic.

Update July 28, 2019:

When the Resize Textures button is pressed, a pop-up menu appears and asks for the number of rescale steps; one step means that the texture is rescaled by 50% in each direction, two steps that the scale factor is 25%, etc.

The name of the rescaled texture is the original filename followed by the scale factor multiplied by 100. So if the original filename is foo.png, the rescaled textures are named as follows:
StepsScale factorFile name
150%foo-050.png
225%foo-025.png
312.5%foo-012.png
46.25%foo-006.png

This button only replaces the textures in the Blender materials; the rescaled textures must already exist in the same directory. If you have access to some other program that resizes image files, you can of course use that instead of the standalone script bundled with the DAZ Importer, as long as you follow the naming convention above.

If you know that you will never need the original, high-resolution image files, you can alternatively overwrite the original filenames with the scaled files and reload the blend file.