Say I have an image created like so:

 img = bpy.data.images.new('imageName',1024,1024)

Now later on I would like to change the size of this image to be something else, say 2048 X 2048.

Is there a way to change the size of the image itself without removing the image and then calling new again?

Note, the image is used to store a baked texture so it does not matter what happens to the content of the image as it will be overwritten.

  • 3
    $\begingroup$ Even if there were, what effect would you hope to accomplish? Do you want to upsample the image, and if so, do you want nearest neighbor, linear, or other interpolation? Or maybe just reinterpret the existing sample array at the larger width (which seems strange, but you haven't specified what problem this resize is solving). $\endgroup$ – Mutant Bob Dec 7 '15 at 22:25
  • $\begingroup$ @Mutant Bob, I am using the image to store a baked texture so it does not matter what happens to the content of the image as it will be overwritten. $\endgroup$ – LongBoolean Dec 7 '15 at 22:38

As Mutant Bob said in his comment to your question, the answer may vary depending on what you hope to accomplish with the image's resize.

Generally, you can scale an existing image by using the scale function on an existing image datablock:

#                                 width, height  
bpy.data.images['imageName'].scale( 2048, 1024 )

If you just want to resize an empty, new image, this would be fine. If you're resizing an actual image that has important content, you might want to do this some other way and use resampling filters, as Mutant Bob hinted.

One other way to do this could be using the Python Image Library (PIL).

| improve this answer | |
  • $\begingroup$ great, just what I needed for my specific use case. $\endgroup$ – LongBoolean Dec 7 '15 at 22:53
  • $\begingroup$ @TLousky Just a note, as far as I am aware, the PIL library isn't included in blender's version of python by default. $\endgroup$ – Ray Mairlot Dec 11 '15 at 15:57
  • $\begingroup$ @RayMairlot, yeah that's a good point. I guess I mentioned it as a kind of basic lead rather than a real part of my answer. $\endgroup$ – TLousky Dec 11 '15 at 16:23

To change the size of a generated image:

img = bpy.data.images.new('imageName',1024, 1024)

# ...

img.generated_width = 2048
img.generated_height = 2048

Otherwise, call image.scale(w, h) to preserve existing pixel data.

| improve this answer | |
  • $\begingroup$ image.scale() at least as of 2.79 is broken (tested in console) $\endgroup$ – VSB Feb 28 '18 at 0:36
  • $\begingroup$ Works in 2.79 here, please report a bug with details. $\endgroup$ – ideasman42 Mar 3 '18 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.