We can open an image/texture in Blender using the Python API by calling the bpy.opa.image.open(…) operator, or directly through the blend data collection by calling the bpy.data.images.load(…) method.
However, if the desired image format is not supported by Blender, the code execution is not interrupted, Blender only prints an error message to the system console.
IMB_ibImageFromMemory: unknown file-format (c:\….f)
Before opening an image, we can check if its type is supported by Blender by checking its extension.
We can get a list of all image extensions supported by Blender from the bpy.path.extensions_image list:
1 2 |
bpy.path.extensions_image # frozenset({'.cin', '.jpeg', '.hdr', '.psd', '.png', '.jp2', '.dpx', '.tiff', '.jpg', '.pdd', '.j2c', '.bmp', '.dds', '.tx', '.rgba', '.sgi', '.tif', '.rgb', '.tga', '.exr', '.webp', '.psb'}) |
Comparing the extension of the image with this list, we can determine if this type of image is supported in Blender:
1 2 3 4 5 6 7 8 9 |
import bpy import os image_path = 'c:/image.xcf' if not image_path.endswith(tuple(bpy.path.extensions_image)): print('Not supported!') else: bpy.data.images.load(image_path, check_existing=True) |