2
$\begingroup$

How do I get the height and width (in pixels) of an image that's been imported in Blender?

bpy.data.images['myImage'].???

$\endgroup$
2
  • $\begingroup$ Suggest use the python console to figure out such properties: docs.blender.org/manual/en/latest/editors/python_console.html $\endgroup$
    – brockmann
    Commented May 15, 2020 at 15:15
  • $\begingroup$ thanks. I used it as well as the tab function and thought size may be it but didn't find how to return the x and y values from it. @aliasguru's answer clear it out for me. $\endgroup$ Commented May 15, 2020 at 16:37

1 Answer 1

4
$\begingroup$

You can use the size property to get the X and Y dimensions of an image:

width, height = bpy.data.images['myImage'].size
# the variables width and height will now contain the pixel size
$\endgroup$
3
  • $\begingroup$ Thanks! How come I can't return the values without assigning them? $\endgroup$ Commented May 15, 2020 at 15:00
  • 1
    $\begingroup$ @ChameleonScales Well, you could. the size property is simply an array, you could access the values like bpy.data.images['myImage'].size[0] and bpy.data.images['myImage'].size[1]. However, I tend to find the given answer to be more Pythonic. See note.nkmk.me/en/python-tuple-list-unpack for a deeper explanation $\endgroup$
    – aliasguru
    Commented May 15, 2020 at 15:33
  • $\begingroup$ oh, of course, where's my head $\endgroup$ Commented May 15, 2020 at 16:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .