Image scale is reset after rendering in background mode #85772

Open
opened 2021-02-18 18:24:33 +01:00 by Simon Wendsche · 5 comments

System Information
Operating system: Windows 10

Blender Version
Broken: 2.91.2

Short description of error

I am starting Blender from command line in background mode (-b) and execute a Python script which does the following:

  • Load an image
  • Scale the image
  • Render with Cycles

After the rendering is done, it is as if the image scaling never happened. The image is back to its original size, and if I save it, it is identical to the original unscaled image.
So it looks like somewhere during bpy.ops.render.render(), the changes to the image are undone.

This only happens when Blender is started in background mode. If started without -b argument, the problem does not occur.

Exact steps for others to reproduce the error

Download this test image: test.jpg

Run Blender from command line with the following command. Replace path to Blender executable and path to the test.jpg image with the correct paths for your machine, i.e. replace the following strings:

  • "C:\Program Files\Blender Foundation\Blender 2.91\blender.exe"
  • C:\Users\Simon\Downloads\test.jpg
"C:\Program Files\Blender Foundation\Blender 2.91\blender.exe" --factory-startup --addons "io_import_images_as_planes" -b --engine CYCLES --python-expr "import bpy; import bpy_extras; image = bpy_extras.image_utils.load_image(r'C:\Users\Simon\Downloads\test.jpg', check_existing=True, force_reload=False); print('original size:', image.size[:]); image.scale(99, 99); print('before render:', image.size[:]); bpy.ops.render.render(); print('after render:', image.size[:]);"

With -b flag

Blender will print a log like this:

"C:\Program Files\Blender Foundation\Blender 2.91\blender.exe" --factory-startup --addons "io_import_images_as_planes" -b --engine CYCLES --python-expr "import bpy; import bpy_extras; image = bpy_extras.image_utils.load_image(r'C:\Users\Simon\Downloads\test.jpg', check_existing=True, force_reload=False); print('original size:', image.size[:]); image.scale(99, 99); print('before render:', image.size[:]); bpy.ops.render.render(); print('after render:', image.size[:]);"
Blender 2.91.2 (hash 5be9ef417703 built 2021-01-19 16:25:50)
found bundled python: C:\Program Files\Blender Foundation\Blender 2.91\2.91\python
original size: (712, 400)
before render: (99, 99)
Fra:1 Mem:47.08M (Peak 47.15M) | Time:00:00.00 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Synchronizing object | Cube
Fra:1 Mem:47.09M (Peak 47.15M) | Time:00:00.00 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Initializing
Fra:1 Mem:46.97M (Peak 47.15M) | Time:00:00.00 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Waiting for render to start
Fra:1 Mem:47.00M (Peak 47.15M) | Time:00:00.00 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Loading render kernels (may take a few minutes the first time)

[... cut out unimportant output ...]

Fra:1 Mem:49.34M (Peak 50.97M) | Time:00:09.92 | Remaining:00:00.01 | Mem:2.09M, Peak:3.57M | Scene, View Layer | Rendered 505/510 Tiles
Fra:1 Mem:49.23M (Peak 50.97M) | Time:00:09.92 | Remaining:00:00.01 | Mem:1.98M, Peak:3.57M | Scene, View Layer | Rendered 506/510 Tiles
Fra:1 Mem:49.12M (Peak 50.97M) | Time:00:09.93 | Remaining:00:00.00 | Mem:1.87M, Peak:3.57M | Scene, View Layer | Rendered 507/510 Tiles
Fra:1 Mem:49.01M (Peak 50.97M) | Time:00:09.94 | Remaining:00:00.00 | Mem:1.76M, Peak:3.57M | Scene, View Layer | Rendered 508/510 Tiles
Fra:1 Mem:48.90M (Peak 50.97M) | Time:00:09.95 | Remaining:00:00.00 | Mem:1.65M, Peak:3.57M | Scene, View Layer | Rendered 509/510 Tiles, Sample 128/128
Fra:1 Mem:48.78M (Peak 50.97M) | Time:00:09.96 | Mem:1.53M, Peak:3.57M | Scene, View Layer | Rendered 510/510 Tiles
Fra:1 Mem:48.78M (Peak 50.97M) | Time:00:09.96 | Mem:1.53M, Peak:3.57M | Scene, View Layer | Finished
Fra:1 Mem:47.17M (Peak 50.97M) | Time:00:09.96 | Sce: Scene Ve:0 Fa:0 La:0
after render: (712, 400)

Blender quit

You can see that the image is loaded, the original size (712, 400) is printed, then after scaling and before rendering, the correct new size (99, 99) is printed, and then again after rendering, the old and now incorrect size (712, 400) is printed.

Without -b flag

When run without -b flag, we get the following output where Blender behaves correctly:

C:\Users\Simon>"C:\Program Files\Blender Foundation\Blender 2.91\blender.exe" --factory-startup --addons "io_import_images_as_planes" --engine CYCLES --python-expr "import bpy; import bpy_extras; image = bpy_extras.image_utils.load_image(r'C:\Users\Simon\Downloads\test.jpg', check_existing=True, force_reload=False); print('original size:', image.size[:]); image.scale(99, 99); print('before render:', image.size[:]); bpy.ops.render.render(); print('after render:', image.size[:]);"
found bundled python: C:\Program Files\Blender Foundation\Blender 2.91\2.91\python
original size: (712, 400)
before render: (99, 99)
after render: (99, 99)

The script in more legible form

Here's the script that is passed to --python-expr, expanded to be more legible:

import bpy
import bpy_extras

image = bpy_extras.image_utils.load_image(r'C:\Users\Simon\Downloads\test.jpg', check_existing=True, force_reload=False)
print('original size:', image.size[:])

image.scale(99, 99)

print('before render:', image.size[:])
bpy.ops.render.render()
print('after render:', image.size[:])
**System Information** Operating system: Windows 10 **Blender Version** Broken: 2.91.2 **Short description of error** I am starting Blender from command line in background mode (-b) and execute a Python script which does the following: * Load an image * Scale the image * Render with Cycles After the rendering is done, *it is as if the image scaling never happened.* The image is back to its original size, and if I save it, it is identical to the original unscaled image. So it looks like somewhere during bpy.ops.render.render(), the changes to the image are undone. *This only happens when Blender is started in background mode.* If started without -b argument, the problem does not occur. **Exact steps for others to reproduce the error** Download this test image: ![test.jpg](https://archive.blender.org/developer/F9815105/test.jpg) Run Blender from command line with the following command. Replace path to Blender executable and path to the test.jpg image with the correct paths for your machine, i.e. replace the following strings: * `"C:\Program Files\Blender Foundation\Blender 2.91\blender.exe"` * `C:\Users\Simon\Downloads\test.jpg` ``` "C:\Program Files\Blender Foundation\Blender 2.91\blender.exe" --factory-startup --addons "io_import_images_as_planes" -b --engine CYCLES --python-expr "import bpy; import bpy_extras; image = bpy_extras.image_utils.load_image(r'C:\Users\Simon\Downloads\test.jpg', check_existing=True, force_reload=False); print('original size:', image.size[:]); image.scale(99, 99); print('before render:', image.size[:]); bpy.ops.render.render(); print('after render:', image.size[:]);" ``` **With -b flag** Blender will print a log like this: ``` "C:\Program Files\Blender Foundation\Blender 2.91\blender.exe" --factory-startup --addons "io_import_images_as_planes" -b --engine CYCLES --python-expr "import bpy; import bpy_extras; image = bpy_extras.image_utils.load_image(r'C:\Users\Simon\Downloads\test.jpg', check_existing=True, force_reload=False); print('original size:', image.size[:]); image.scale(99, 99); print('before render:', image.size[:]); bpy.ops.render.render(); print('after render:', image.size[:]);" Blender 2.91.2 (hash 5be9ef417703 built 2021-01-19 16:25:50) found bundled python: C:\Program Files\Blender Foundation\Blender 2.91\2.91\python original size: (712, 400) before render: (99, 99) Fra:1 Mem:47.08M (Peak 47.15M) | Time:00:00.00 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Synchronizing object | Cube Fra:1 Mem:47.09M (Peak 47.15M) | Time:00:00.00 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Initializing Fra:1 Mem:46.97M (Peak 47.15M) | Time:00:00.00 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Waiting for render to start Fra:1 Mem:47.00M (Peak 47.15M) | Time:00:00.00 | Mem:0.00M, Peak:0.00M | Scene, View Layer | Loading render kernels (may take a few minutes the first time) [... cut out unimportant output ...] Fra:1 Mem:49.34M (Peak 50.97M) | Time:00:09.92 | Remaining:00:00.01 | Mem:2.09M, Peak:3.57M | Scene, View Layer | Rendered 505/510 Tiles Fra:1 Mem:49.23M (Peak 50.97M) | Time:00:09.92 | Remaining:00:00.01 | Mem:1.98M, Peak:3.57M | Scene, View Layer | Rendered 506/510 Tiles Fra:1 Mem:49.12M (Peak 50.97M) | Time:00:09.93 | Remaining:00:00.00 | Mem:1.87M, Peak:3.57M | Scene, View Layer | Rendered 507/510 Tiles Fra:1 Mem:49.01M (Peak 50.97M) | Time:00:09.94 | Remaining:00:00.00 | Mem:1.76M, Peak:3.57M | Scene, View Layer | Rendered 508/510 Tiles Fra:1 Mem:48.90M (Peak 50.97M) | Time:00:09.95 | Remaining:00:00.00 | Mem:1.65M, Peak:3.57M | Scene, View Layer | Rendered 509/510 Tiles, Sample 128/128 Fra:1 Mem:48.78M (Peak 50.97M) | Time:00:09.96 | Mem:1.53M, Peak:3.57M | Scene, View Layer | Rendered 510/510 Tiles Fra:1 Mem:48.78M (Peak 50.97M) | Time:00:09.96 | Mem:1.53M, Peak:3.57M | Scene, View Layer | Finished Fra:1 Mem:47.17M (Peak 50.97M) | Time:00:09.96 | Sce: Scene Ve:0 Fa:0 La:0 after render: (712, 400) Blender quit ``` You can see that the image is loaded, the original size (712, 400) is printed, then after scaling and before rendering, the correct new size (99, 99) is printed, and then again after rendering, the old and now incorrect size (712, 400) is printed. **Without -b flag** When run without -b flag, we get the following output where Blender behaves correctly: ``` C:\Users\Simon>"C:\Program Files\Blender Foundation\Blender 2.91\blender.exe" --factory-startup --addons "io_import_images_as_planes" --engine CYCLES --python-expr "import bpy; import bpy_extras; image = bpy_extras.image_utils.load_image(r'C:\Users\Simon\Downloads\test.jpg', check_existing=True, force_reload=False); print('original size:', image.size[:]); image.scale(99, 99); print('before render:', image.size[:]); bpy.ops.render.render(); print('after render:', image.size[:]);" found bundled python: C:\Program Files\Blender Foundation\Blender 2.91\2.91\python original size: (712, 400) before render: (99, 99) after render: (99, 99) ``` **The script in more legible form** Here's the script that is passed to --python-expr, expanded to be more legible: ``` import bpy import bpy_extras image = bpy_extras.image_utils.load_image(r'C:\Users\Simon\Downloads\test.jpg', check_existing=True, force_reload=False) print('original size:', image.size[:]) image.scale(99, 99) print('before render:', image.size[:]) bpy.ops.render.render() print('after render:', image.size[:]) ```
Author

Added subscriber: @BYOB

Added subscriber: @BYOB
Member

Added subscriber: @lichtwerk

Added subscriber: @lichtwerk
Member

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'
Member

Can confirm.

Can confirm.
Philipp Oeser removed the
Interest
Python API
label 2023-02-10 09:04:40 +01:00
Author

I found a workaround: Load the image, scale it down, create a new image with the same (small) dimensions, copy the pixel data over, delete the first image. You can then use the small copied image for rendering, and Blender won't undo the scaling. Note that you need to handle colorspace conversion (e.g. to sRGB) yourself in this case.

Example code:

import numpy as np

# Load full resolution image and scale it down.
# I'm copying the loaded and scaled image pixels into a new image because of this Blender bug:
# https://developer.blender.org/T85772 (it would cause the scaled image to revert back to full size after rendering)
temp_image = bpy_extras.image_utils.load_image(abspath, check_existing=True, force_reload=False)
if temp_image:
    temp_image.scale(thumb_resolution, thumb_resolution)
    temp_image.name = temp_image.name + "___temp"

    # Create new image with target (small) resolution
    image = bpy.data.images.new(image_name, thumb_resolution, thumb_resolution, alpha=True, float_buffer=True, is_data=False)

    # Note: Blender images are always created with 4 channels
    pixel_array_size = thumb_resolution * thumb_resolution * 4
    temp_pixels = np.zeros(pixel_array_size, dtype=np.float32)
    temp_image.pixels.foreach_get(temp_pixels)

    # Apply gamma correction
    gamma = np.full(pixel_array_size, 2.2, dtype=np.float32)
    gamma_alpha = gamma[3::4]  # A view of the gamma values affecting the alpha channel
    gamma_alpha[:] = 1.0  # Set all gamma values affecting alpha to 1.0 so we don't affect the alpha channel
    temp_pixels = np.power(temp_pixels, gamma)

    image.pixels.foreach_set(temp_pixels)
    bpy.data.images.remove(temp_image)
I found a workaround: Load the image, scale it down, create a new image with the same (small) dimensions, copy the pixel data over, delete the first image. You can then use the small copied image for rendering, and Blender won't undo the scaling. Note that you need to handle colorspace conversion (e.g. to sRGB) yourself in this case. Example code: ``` import numpy as np # Load full resolution image and scale it down. # I'm copying the loaded and scaled image pixels into a new image because of this Blender bug: # https://developer.blender.org/T85772 (it would cause the scaled image to revert back to full size after rendering) temp_image = bpy_extras.image_utils.load_image(abspath, check_existing=True, force_reload=False) if temp_image: temp_image.scale(thumb_resolution, thumb_resolution) temp_image.name = temp_image.name + "___temp" # Create new image with target (small) resolution image = bpy.data.images.new(image_name, thumb_resolution, thumb_resolution, alpha=True, float_buffer=True, is_data=False) # Note: Blender images are always created with 4 channels pixel_array_size = thumb_resolution * thumb_resolution * 4 temp_pixels = np.zeros(pixel_array_size, dtype=np.float32) temp_image.pixels.foreach_get(temp_pixels) # Apply gamma correction gamma = np.full(pixel_array_size, 2.2, dtype=np.float32) gamma_alpha = gamma[3::4] # A view of the gamma values affecting the alpha channel gamma_alpha[:] = 1.0 # Set all gamma values affecting alpha to 1.0 so we don't affect the alpha channel temp_pixels = np.power(temp_pixels, gamma) image.pixels.foreach_set(temp_pixels) bpy.data.images.remove(temp_image) ```
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#85772
No description provided.