Class Image
Image datatype.
- Inheritance
-
Image
Remarks
Native image datatype. Contains image data which can be converted to an ImageTexture and provides commonly used image processing methods. The maximum width and height for an Image are MAX_WIDTH and MAX_HEIGHT.
An Image cannot be assigned to a texture property of an object directly (such as texture), and has to be converted manually to an ImageTexture first.
Note: The maximum image size is 16384×16384 pixels due to graphics hardware limitations. Larger images may fail to import.
See Also
Fields
MAX_WIDTH
The maximal width allowed for Image resources.
const MAX_WIDTH = 16777216
MAX_HEIGHT
The maximal height allowed for Image resources.
const MAX_HEIGHT = 16777216
Properties
data
Holds all the image's color data in a given format. See Format constants.
var data : Dictionary = { "data": PackedByteArray(), "format": "Lum8", "height": 0, "mipmaps": false, "width": 0 }
Property Value
Methods
adjust_bcs(float, float, float)
Adjusts this image's brightness
, contrast
, and saturation
by the given values. Does not work if the image is compressed (see is_compressed).
void adjust_bcs(float brightness, float contrast, float saturation)
Parameters
blend_rect(Image, Rect2i, Vector2i)
Alpha-blends src_rect
from src
image to this image at coordinates dst
, clipped accordingly to both image bounds. This image and src
image must have the same format. src_rect
with non-positive size is treated as empty.
void blend_rect(Image src, Rect2i src_rect, Vector2i dst)
Parameters
blend_rect_mask(Image, Image, Rect2i, Vector2i)
Alpha-blends src_rect
from src
image to this image using mask
image at coordinates dst
, clipped accordingly to both image bounds. Alpha channels are required for both src
and mask
. dst
pixels and src
pixels will blend if the corresponding mask pixel's alpha value is not 0. This image and src
image must have the same format. src
image and mask
image must have the same size (width and height) but they can have different formats. src_rect
with non-positive size is treated as empty.
void blend_rect_mask(Image src, Image mask, Rect2i src_rect, Vector2i dst)
Parameters
blit_rect(Image, Rect2i, Vector2i)
Copies src_rect
from src
image to this image at coordinates dst
, clipped accordingly to both image bounds. This image and src
image must have the same format. src_rect
with non-positive size is treated as empty.
Note: The alpha channel data in src
will overwrite the corresponding data in this image at the target position. To blend alpha channels, use Image.blend_rect instead.
void blit_rect(Image src, Rect2i src_rect, Vector2i dst)
Parameters
blit_rect_mask(Image, Image, Rect2i, Vector2i)
Blits src_rect
area from src
image to this image at the coordinates given by dst
, clipped accordingly to both image bounds. src
pixel is copied onto dst
if the corresponding mask
pixel's alpha value is not 0. This image and src
image must have the same format. src
image and mask
image must have the same size (width and height) but they can have different formats. src_rect
with non-positive size is treated as empty.
void blit_rect_mask(Image src, Image mask, Rect2i src_rect, Vector2i dst)
Parameters
bump_map_to_normal_map(float)
Converts a bump map to a normal map. A bump map provides a height offset per-pixel, while a normal map provides a normal direction per pixel.
void bump_map_to_normal_map(float bump_scale)
Parameters
bump_scale
float
clear_mipmaps
Removes the image's mipmaps.
void clear_mipmaps
compress(int, int, int)
Compresses the image to use less memory. Can not directly access pixel data while the image is compressed. Returns error if the chosen compression mode is not available.
The source
parameter helps to pick the best compression method for DXT and ETC2 formats. It is ignored for ASTC compression.
For ASTC compression, the astc_format
parameter must be supplied.
int compress(int mode, int source, int astc_format)
Parameters
compress_from_channels(int, int, int)
Compresses the image to use less memory. Can not directly access pixel data while the image is compressed. Returns error if the chosen compression mode is not available.
This is an alternative to Image.compress that lets the user supply the channels used in order for the compressor to pick the best DXT and ETC2 formats. For other formats (non DXT or ETC2), this argument is ignored.
For ASTC compression, the astc_format
parameter must be supplied.
int compress_from_channels(int mode, int channels, int astc_format)
Parameters
compute_image_metrics(Image, bool)
Compute image metrics on the current image and the compared image.
The dictionary contains max
, mean
, mean_squared
, root_mean_squared
and peak_snr
.
Dictionary compute_image_metrics(Image compared_image, bool use_luma)
Parameters
convert(int)
Converts the image's format. See Format constants.
void convert(int format)
Parameters
format
int
copy_from(Image)
Copies src
image to this image.
void copy_from(Image src)
Parameters
src
Image
create(int, int, bool, int)
Qualifiers: static
Creates an empty image of given size and format. See Format constants. If use_mipmaps
is true
, then generate mipmaps for this image. See the Image.generate_mipmaps.
Image create(int width, int height, bool use_mipmaps, int format)
Parameters
create_empty(int, int, bool, int)
Qualifiers: static
Creates an empty image of given size and format. See Format constants. If use_mipmaps
is true
, then generate mipmaps for this image. See the Image.generate_mipmaps.
Image create_empty(int width, int height, bool use_mipmaps, int format)
Parameters
create_from_data(int, int, bool, int, PackedByteArray)
Qualifiers: static
Creates a new image of given size and format. See Format constants. Fills the image with the given raw data. If use_mipmaps
is true
then loads mipmaps for this image from data
. See Image.generate_mipmaps.
Image create_from_data(int width, int height, bool use_mipmaps, int format, PackedByteArray data)
Parameters
width
intheight
intuse_mipmaps
boolformat
intdata
PackedByteArray
crop(int, int)
Crops the image to the given width
and height
. If the specified size is larger than the current size, the extra area is filled with black pixels.
void crop(int width, int height)
Parameters
decompress
Decompresses the image if it is VRAM compressed in a supported format. Returns @GlobalScope.OK if the format is supported, otherwise @GlobalScope.ERR_UNAVAILABLE.
Note: The following formats can be decompressed: DXT, RGTC, BPTC. The formats ETC1 and ETC2 are not supported.
int decompress
detect_alpha
Qualifiers: const
Returns Image.ALPHA_BLEND if the image has data for alpha values. Returns Image.ALPHA_BIT if all the alpha values are stored in a single bit. Returns Image.ALPHA_NONE if no data for alpha values is found.
int detect_alpha
detect_used_channels(int)
Qualifiers: const
Returns the color channels used by this image, as one of the UsedChannels constants. If the image is compressed, the original source
must be specified.
int detect_used_channels(int source)
Parameters
source
int
fill(Color)
Fills the image with color
.
void fill(Color color)
Parameters
color
Color
fill_rect(Rect2i, Color)
Fills rect
with color
.
void fill_rect(Rect2i rect, Color color)
Parameters
fix_alpha_edges
Blends low-alpha pixels with nearby pixels.
void fix_alpha_edges
flip_x
Flips the image horizontally.
void flip_x
flip_y
Flips the image vertically.
void flip_y
generate_mipmaps(bool)
Generates mipmaps for the image. Mipmaps are precalculated lower-resolution copies of the image that are automatically used if the image needs to be scaled down when rendered. They help improve image quality and performance when rendering. This method returns an error if the image is compressed, in a custom format, or if the image's width/height is 0
. Enabling renormalize
when generating mipmaps for normal map textures will make sure all resulting vector values are normalized.
It is possible to check if the image has mipmaps by calling has_mipmaps or get_mipmap_count. Calling Image.generate_mipmaps on an image that already has mipmaps will replace existing mipmaps in the image.
int generate_mipmaps(bool renormalize)
Parameters
renormalize
bool
get_data
Qualifiers: const
Returns a copy of the image's raw data.
PackedByteArray get_data
get_data_size
Qualifiers: const
Returns size (in bytes) of the image's raw data.
int get_data_size
get_format
Qualifiers: const
Returns the image's format. See Format constants.
int get_format
get_height
Qualifiers: const
Returns the image's height.
int get_height
get_mipmap_count
Qualifiers: const
Returns the number of mipmap levels or 0 if the image has no mipmaps. The largest main level image is not counted as a mipmap level by this method, so if you want to include it you can add 1 to this count.
int get_mipmap_count
get_mipmap_offset(int)
Qualifiers: const
Returns the offset where the image's mipmap with index mipmap
is stored in the data dictionary.
int get_mipmap_offset(int mipmap)
Parameters
mipmap
int
get_pixel(int, int)
Qualifiers: const
Returns the color of the pixel at (x, y)
.
This is the same as Image.get_pixelv, but with two integer arguments instead of a Vector2i argument.
Color get_pixel(int x, int y)
Parameters
get_pixelv(Vector2i)
Qualifiers: const
Returns the color of the pixel at point
.
This is the same as Image.get_pixel, but with a Vector2i argument instead of two integer arguments.
Color get_pixelv(Vector2i point)
Parameters
point
Vector2i
get_region(Rect2i)
Qualifiers: const
Returns a new Image that is a copy of this Image's area specified with region
.
Image get_region(Rect2i region)
Parameters
region
Rect2i
get_size
Qualifiers: const
Returns the image's size (width and height).
Vector2i get_size
get_used_rect
Qualifiers: const
Returns a Rect2i enclosing the visible portion of the image, considering each pixel with a non-zero alpha channel as visible.
Rect2i get_used_rect
get_width
Qualifiers: const
Returns the image's width.
int get_width
has_mipmaps
Qualifiers: const
Returns true
if the image has generated mipmaps.
bool has_mipmaps
is_compressed
Qualifiers: const
Returns true
if the image is compressed.
bool is_compressed
is_empty
Qualifiers: const
Returns true
if the image has no data.
bool is_empty
is_invisible
Qualifiers: const
Returns true
if all the image's pixels have an alpha value of 0. Returns false
if any pixel has an alpha value higher than 0.
bool is_invisible
linear_to_srgb
Converts the entire image from the linear colorspace to the sRGB colorspace. Only works on images with Image.FORMAT_RGB8 or Image.FORMAT_RGBA8 formats.
void linear_to_srgb
load(String)
Loads an image from file path
. See Supported image formats for a list of supported image formats and limitations.
Warning: This method should only be used in the editor or in cases when you need to load external images at run-time, such as images located at the user://
directory, and may not work in exported projects.
See also ImageTexture description for usage examples.
int load(String path)
Parameters
path
String
load_bmp_from_buffer(PackedByteArray)
Loads an image from the binary contents of a BMP file.
Note: Godot's BMP module doesn't support 16-bit per pixel images. Only 1-bit, 4-bit, 8-bit, 24-bit, and 32-bit per pixel images are supported.
Note: This method is only available in engine builds with the BMP module enabled. By default, the BMP module is enabled, but it can be disabled at build-time using the module_bmp_enabled=no
SCons option.
int load_bmp_from_buffer(PackedByteArray buffer)
Parameters
buffer
PackedByteArray
load_from_file(String)
Qualifiers: static
Creates a new Image and loads data from the specified file.
Image load_from_file(String path)
Parameters
path
String
load_jpg_from_buffer(PackedByteArray)
Loads an image from the binary contents of a JPEG file.
int load_jpg_from_buffer(PackedByteArray buffer)
Parameters
buffer
PackedByteArray
load_ktx_from_buffer(PackedByteArray)
Loads an image from the binary contents of a KTX file. Unlike most image formats, KTX can store VRAM-compressed data and embed mipmaps.
Note: Godot's libktx implementation only supports 2D images. Cubemaps, texture arrays, and de-padding are not supported.
Note: This method is only available in engine builds with the KTX module enabled. By default, the KTX module is enabled, but it can be disabled at build-time using the module_ktx_enabled=no
SCons option.
int load_ktx_from_buffer(PackedByteArray buffer)
Parameters
buffer
PackedByteArray
load_png_from_buffer(PackedByteArray)
Loads an image from the binary contents of a PNG file.
int load_png_from_buffer(PackedByteArray buffer)
Parameters
buffer
PackedByteArray
load_svg_from_buffer(PackedByteArray, float)
Loads an image from the UTF-8 binary contents of an uncompressed SVG file (.svg).
Note: Beware when using compressed SVG files (like .svgz), they need to be decompressed
before loading.
Note: This method is only available in engine builds with the SVG module enabled. By default, the SVG module is enabled, but it can be disabled at build-time using the module_svg_enabled=no
SCons option.
int load_svg_from_buffer(PackedByteArray buffer, float scale)
Parameters
buffer
PackedByteArrayscale
float
load_svg_from_string(String, float)
Loads an image from the string contents of an SVG file (.svg).
Note: This method is only available in engine builds with the SVG module enabled. By default, the SVG module is enabled, but it can be disabled at build-time using the module_svg_enabled=no
SCons option.
int load_svg_from_string(String svg_str, float scale)
Parameters
load_tga_from_buffer(PackedByteArray)
Loads an image from the binary contents of a TGA file.
Note: This method is only available in engine builds with the TGA module enabled. By default, the TGA module is enabled, but it can be disabled at build-time using the module_tga_enabled=no
SCons option.
int load_tga_from_buffer(PackedByteArray buffer)
Parameters
buffer
PackedByteArray
load_webp_from_buffer(PackedByteArray)
Loads an image from the binary contents of a WebP file.
int load_webp_from_buffer(PackedByteArray buffer)
Parameters
buffer
PackedByteArray
normal_map_to_xy
Converts the image's data to represent coordinates on a 3D plane. This is used when the image represents a normal map. A normal map can add lots of detail to a 3D surface without increasing the polygon count.
void normal_map_to_xy
premultiply_alpha
Multiplies color values with alpha values. Resulting color values for a pixel are (color * alpha)/256
. See also blend_mode.
void premultiply_alpha
resize(int, int, int)
Resizes the image to the given width
and height
. New pixels are calculated using the interpolation
mode defined via Interpolation constants.
void resize(int width, int height, int interpolation)
Parameters
resize_to_po2(bool, int)
Resizes the image to the nearest power of 2 for the width and height. If square
is true
then set width and height to be the same. New pixels are calculated using the interpolation
mode defined via Interpolation constants.
void resize_to_po2(bool square, int interpolation)
Parameters
rgbe_to_srgb
Converts a standard RGBE (Red Green Blue Exponent) image to an sRGB image.
Image rgbe_to_srgb
rotate_90(int)
Rotates the image in the specified direction
by 90
degrees. The width and height of the image must be greater than 1
. If the width and height are not equal, the image will be resized.
void rotate_90(int direction)
Parameters
direction
int
rotate_180
Rotates the image by 180
degrees. The width and height of the image must be greater than 1
.
void rotate_180
save_exr(String, bool)
Qualifiers: const
Saves the image as an EXR file to path
. If grayscale
is true
and the image has only one channel, it will be saved explicitly as monochrome rather than one red channel. This function will return @GlobalScope.ERR_UNAVAILABLE if Godot was compiled without the TinyEXR module.
Note: The TinyEXR module is disabled in non-editor builds, which means Image.save_exr will return @GlobalScope.ERR_UNAVAILABLE when it is called from an exported project.
int save_exr(String path, bool grayscale)
Parameters
save_exr_to_buffer(bool)
Qualifiers: const
Saves the image as an EXR file to a byte array. If grayscale
is true
and the image has only one channel, it will be saved explicitly as monochrome rather than one red channel. This function will return an empty byte array if Godot was compiled without the TinyEXR module.
Note: The TinyEXR module is disabled in non-editor builds, which means Image.save_exr will return an empty byte array when it is called from an exported project.
PackedByteArray save_exr_to_buffer(bool grayscale)
Parameters
grayscale
bool
save_jpg(String, float)
Qualifiers: const
Saves the image as a JPEG file to path
with the specified quality
between 0.01
and 1.0
(inclusive). Higher quality
values result in better-looking output at the cost of larger file sizes. Recommended quality
values are between 0.75
and 0.90
. Even at quality 1.00
, JPEG compression remains lossy.
Note: JPEG does not save an alpha channel. If the Image contains an alpha channel, the image will still be saved, but the resulting JPEG file won't contain the alpha channel.
int save_jpg(String path, float quality)
Parameters
save_jpg_to_buffer(float)
Qualifiers: const
Saves the image as a JPEG file to a byte array with the specified quality
between 0.01
and 1.0
(inclusive). Higher quality
values result in better-looking output at the cost of larger byte array sizes (and therefore memory usage). Recommended quality
values are between 0.75
and 0.90
. Even at quality 1.00
, JPEG compression remains lossy.
Note: JPEG does not save an alpha channel. If the Image contains an alpha channel, the image will still be saved, but the resulting byte array won't contain the alpha channel.
PackedByteArray save_jpg_to_buffer(float quality)
Parameters
quality
float
save_png(String)
Qualifiers: const
Saves the image as a PNG file to the file at path
.
int save_png(String path)
Parameters
path
String
save_png_to_buffer
Qualifiers: const
Saves the image as a PNG file to a byte array.
PackedByteArray save_png_to_buffer
save_webp(String, bool, float)
Qualifiers: const
Saves the image as a WebP (Web Picture) file to the file at path
. By default it will save lossless. If lossy
is true
, the image will be saved lossy, using the quality
setting between 0.0
and 1.0
(inclusive). Lossless WebP offers more efficient compression than PNG.
Note: The WebP format is limited to a size of 16383×16383 pixels, while PNG can save larger images.
int save_webp(String path, bool lossy, float quality)
Parameters
save_webp_to_buffer(bool, float)
Qualifiers: const
Saves the image as a WebP (Web Picture) file to a byte array. By default it will save lossless. If lossy
is true
, the image will be saved lossy, using the quality
setting between 0.0
and 1.0
(inclusive). Lossless WebP offers more efficient compression than PNG.
Note: The WebP format is limited to a size of 16383×16383 pixels, while PNG can save larger images.
PackedByteArray save_webp_to_buffer(bool lossy, float quality)
Parameters
set_data(int, int, bool, int, PackedByteArray)
Overwrites data of an existing Image. Non-static equivalent of Image.create_from_data.
void set_data(int width, int height, bool use_mipmaps, int format, PackedByteArray data)
Parameters
width
intheight
intuse_mipmaps
boolformat
intdata
PackedByteArray
set_pixel(int, int, Color)
Sets the Color of the pixel at (x, y)
to color
.
var img_width = 10
var img_height = 5
var img = Image.create(img_width, img_height, false, Image.FORMAT_RGBA8)
img.set_pixel(1, 2, Color.RED) # Sets the color at (1, 2) to red.
This is the same as Image.set_pixelv, but with a two integer arguments instead of a Vector2i argument.
void set_pixel(int x, int y, Color color)
Parameters
set_pixelv(Vector2i, Color)
Sets the Color of the pixel at point
to color
.
var img_width = 10
var img_height = 5
var img = Image.create(img_width, img_height, false, Image.FORMAT_RGBA8)
img.set_pixelv(Vector2i(1, 2), Color.RED) # Sets the color at (1, 2) to red.
This is the same as Image.set_pixel, but with a Vector2i argument instead of two integer arguments.
void set_pixelv(Vector2i point, Color color)
Parameters
shrink_x2
Shrinks the image by a factor of 2 on each axis (this divides the pixel count by 4).
void shrink_x2
srgb_to_linear
Converts the raw data from the sRGB colorspace to a linear scale. Only works on images with Image.FORMAT_RGB8 or Image.FORMAT_RGBA8 formats.
void srgb_to_linear