10

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.

ideasman42
  • 47,387
  • 10
  • 141
  • 223
LongBoolean
  • 103
  • 1
  • 6
  • 4
    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). – Mutant Bob Dec 07 '15 at 22:25
  • @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. – LongBoolean Dec 07 '15 at 22:38

2 Answers2

6

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.

ideasman42
  • 47,387
  • 10
  • 141
  • 223
5

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).

TLousky
  • 16,043
  • 1
  • 40
  • 72