Up to date
This page is up to date for Godot 4.0
.
If you still find outdated information, please open an issue.
Image¶
Inherits: Resource < RefCounted < Object
Image datatype.
Description¶
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 Sprite2D), 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.
Tutorials¶
Properties¶
|
Methods¶
void |
adjust_bcs ( float brightness, float contrast, float saturation ) |
void |
blend_rect ( Image src, Rect2i src_rect, Vector2i dst ) |
void |
blend_rect_mask ( Image src, Image mask, Rect2i src_rect, Vector2i dst ) |
void |
|
void |
blit_rect_mask ( Image src, Image mask, Rect2i src_rect, Vector2i dst ) |
void |
bump_map_to_normal_map ( float bump_scale=1.0 ) |
void |
clear_mipmaps ( ) |
compress ( CompressMode mode, CompressSource source=0, ASTCFormat astc_format=0 ) |
|
compress_from_channels ( CompressMode mode, UsedChannels channels, ASTCFormat astc_format=0 ) |
|
compute_image_metrics ( Image compared_image, bool use_luma ) |
|
void |
|
void |
|
create ( int width, int height, bool use_mipmaps, Format format ) static |
|
create_from_data ( int width, int height, bool use_mipmaps, Format format, PackedByteArray data ) static |
|
void |
|
decompress ( ) |
|
detect_alpha ( ) const |
|
detect_used_channels ( CompressSource source=0 ) const |
|
void |
|
void |
|
void |
fix_alpha_edges ( ) |
void |
flip_x ( ) |
void |
flip_y ( ) |
generate_mipmaps ( bool renormalize=false ) |
|
get_data ( ) const |
|
get_format ( ) const |
|
get_height ( ) const |
|
get_mipmap_offset ( int mipmap ) const |
|
get_pixelv ( Vector2i point ) const |
|
get_region ( Rect2i region ) const |
|
get_size ( ) const |
|
get_used_rect ( ) const |
|
get_width ( ) const |
|
has_mipmaps ( ) const |
|
is_compressed ( ) const |
|
is_empty ( ) const |
|
is_invisible ( ) const |
|
load_bmp_from_buffer ( PackedByteArray buffer ) |
|
load_from_file ( String path ) static |
|
load_jpg_from_buffer ( PackedByteArray buffer ) |
|
load_png_from_buffer ( PackedByteArray buffer ) |
|
load_tga_from_buffer ( PackedByteArray buffer ) |
|
load_webp_from_buffer ( PackedByteArray buffer ) |
|
void |
normal_map_to_xy ( ) |
void |
|
void |
resize ( int width, int height, Interpolation interpolation=1 ) |
void |
resize_to_po2 ( bool square=false, Interpolation interpolation=1 ) |
rgbe_to_srgb ( ) |
|
void |
rotate_180 ( ) |
void |
rotate_90 ( ClockDirection direction ) |
save_exr_to_buffer ( bool grayscale=false ) const |
|
save_jpg_to_buffer ( float quality=0.75 ) const |
|
save_png_to_buffer ( ) const |
|
save_webp ( String path, bool lossy=false, float quality=0.75 ) const |
|
save_webp_to_buffer ( bool lossy=false, float quality=0.75 ) const |
|
void |
set_data ( int width, int height, bool use_mipmaps, Format format, PackedByteArray data ) |
void |
|
void |
set_pixelv ( Vector2i point, Color color ) |
void |
shrink_x2 ( ) |
void |
srgb_to_linear ( ) |
Enumerations¶
enum Format:
Format FORMAT_L8 = 0
Texture format with a single 8-bit depth representing luminance.
Format FORMAT_LA8 = 1
OpenGL texture format with two values, luminance and alpha each stored with 8 bits.
Format FORMAT_R8 = 2
OpenGL texture format RED
with a single component and a bitdepth of 8.
Format FORMAT_RG8 = 3
OpenGL texture format RG
with two components and a bitdepth of 8 for each.
Format FORMAT_RGB8 = 4
OpenGL texture format RGB
with three components, each with a bitdepth of 8.
Note: When creating an ImageTexture, an sRGB to linear color space conversion is performed.
Format FORMAT_RGBA8 = 5
OpenGL texture format RGBA
with four components, each with a bitdepth of 8.
Note: When creating an ImageTexture, an sRGB to linear color space conversion is performed.
Format FORMAT_RGBA4444 = 6
OpenGL texture format RGBA
with four components, each with a bitdepth of 4.
Format FORMAT_RGB565 = 7
Format FORMAT_RF = 8
OpenGL texture format GL_R32F
where there's one component, a 32-bit floating-point value.
Format FORMAT_RGF = 9
OpenGL texture format GL_RG32F
where there are two components, each a 32-bit floating-point values.
Format FORMAT_RGBF = 10
OpenGL texture format GL_RGB32F
where there are three components, each a 32-bit floating-point values.
Format FORMAT_RGBAF = 11
OpenGL texture format GL_RGBA32F
where there are four components, each a 32-bit floating-point values.
Format FORMAT_RH = 12
OpenGL texture format GL_R32F
where there's one component, a 16-bit "half-precision" floating-point value.
Format FORMAT_RGH = 13
OpenGL texture format GL_RG32F
where there are two components, each a 16-bit "half-precision" floating-point value.
Format FORMAT_RGBH = 14
OpenGL texture format GL_RGB32F
where there are three components, each a 16-bit "half-precision" floating-point value.
Format FORMAT_RGBAH = 15
OpenGL texture format GL_RGBA32F
where there are four components, each a 16-bit "half-precision" floating-point value.
Format FORMAT_RGBE9995 = 16
A special OpenGL texture format where the three color components have 9 bits of precision and all three share a single 5-bit exponent.
Format FORMAT_DXT1 = 17
The S3TC texture format that uses Block Compression 1, and is the smallest variation of S3TC, only providing 1 bit of alpha and color data being premultiplied with alpha.
Note: When creating an ImageTexture, an sRGB to linear color space conversion is performed.
Format FORMAT_DXT3 = 18
The S3TC texture format that uses Block Compression 2, and color data is interpreted as not having been premultiplied by alpha. Well suited for images with sharp alpha transitions between translucent and opaque areas.
Note: When creating an ImageTexture, an sRGB to linear color space conversion is performed.
Format FORMAT_DXT5 = 19
The S3TC texture format also known as Block Compression 3 or BC3 that contains 64 bits of alpha channel data followed by 64 bits of DXT1-encoded color data. Color data is not premultiplied by alpha, same as DXT3. DXT5 generally produces superior results for transparent gradients compared to DXT3.
Note: When creating an ImageTexture, an sRGB to linear color space conversion is performed.
Format FORMAT_RGTC_R = 20
Texture format that uses Red Green Texture Compression, normalizing the red channel data using the same compression algorithm that DXT5 uses for the alpha channel.
Format FORMAT_RGTC_RG = 21
Texture format that uses Red Green Texture Compression, normalizing the red and green channel data using the same compression algorithm that DXT5 uses for the alpha channel.
Format FORMAT_BPTC_RGBA = 22
Texture format that uses BPTC compression with unsigned normalized RGBA components.
Note: When creating an ImageTexture, an sRGB to linear color space conversion is performed.
Format FORMAT_BPTC_RGBF = 23
Texture format that uses BPTC compression with signed floating-point RGB components.
Format FORMAT_BPTC_RGBFU = 24
Texture format that uses BPTC compression with unsigned floating-point RGB components.
Format FORMAT_ETC = 25
Ericsson Texture Compression format 1, also referred to as "ETC1", and is part of the OpenGL ES graphics standard. This format cannot store an alpha channel.
Format FORMAT_ETC2_R11 = 26
Ericsson Texture Compression format 2 (R11_EAC
variant), which provides one channel of unsigned data.
Format FORMAT_ETC2_R11S = 27
Ericsson Texture Compression format 2 (SIGNED_R11_EAC
variant), which provides one channel of signed data.
Format FORMAT_ETC2_RG11 = 28
Ericsson Texture Compression format 2 (RG11_EAC
variant), which provides two channels of unsigned data.
Format FORMAT_ETC2_RG11S = 29
Ericsson Texture Compression format 2 (SIGNED_RG11_EAC
variant), which provides two channels of signed data.
Format FORMAT_ETC2_RGB8 = 30
Ericsson Texture Compression format 2 (RGB8
variant), which is a follow-up of ETC1 and compresses RGB888 data.
Note: When creating an ImageTexture, an sRGB to linear color space conversion is performed.
Format FORMAT_ETC2_RGBA8 = 31
Ericsson Texture Compression format 2 (RGBA8
variant), which compresses RGBA8888 data with full alpha support.
Note: When creating an ImageTexture, an sRGB to linear color space conversion is performed.
Format FORMAT_ETC2_RGB8A1 = 32
Ericsson Texture Compression format 2 (RGB8_PUNCHTHROUGH_ALPHA1
variant), which compresses RGBA data to make alpha either fully transparent or fully opaque.
Note: When creating an ImageTexture, an sRGB to linear color space conversion is performed.
Format FORMAT_ETC2_RA_AS_RG = 33
Format FORMAT_DXT5_RA_AS_RG = 34
Format FORMAT_ASTC_4x4 = 35
Adaptive Scalable Texutre Compression. This implements the 4x4 (high quality) mode.
Format FORMAT_ASTC_4x4_HDR = 36
Same format as FORMAT_ASTC_4x4, but with the hint to let the GPU know it is used for HDR.
Format FORMAT_ASTC_8x8 = 37
Adaptive Scalable Texutre Compression. This implements the 8x8 (low quality) mode.
Format FORMAT_ASTC_8x8_HDR = 38
Same format as FORMAT_ASTC_8x8, but with the hint to let the GPU know it is used for HDR.
Format FORMAT_MAX = 39
Represents the size of the Format enum.
enum Interpolation:
Interpolation INTERPOLATE_NEAREST = 0
Performs nearest-neighbor interpolation. If the image is resized, it will be pixelated.
Interpolation INTERPOLATE_BILINEAR = 1
Performs bilinear interpolation. If the image is resized, it will be blurry. This mode is faster than INTERPOLATE_CUBIC, but it results in lower quality.
Interpolation INTERPOLATE_CUBIC = 2
Performs cubic interpolation. If the image is resized, it will be blurry. This mode often gives better results compared to INTERPOLATE_BILINEAR, at the cost of being slower.
Interpolation INTERPOLATE_TRILINEAR = 3
Performs bilinear separately on the two most-suited mipmap levels, then linearly interpolates between them.
It's slower than INTERPOLATE_BILINEAR, but produces higher-quality results with far fewer aliasing artifacts.
If the image does not have mipmaps, they will be generated and used internally, but no mipmaps will be generated on the resulting image.
Note: If you intend to scale multiple copies of the original image, it's better to call generate_mipmaps] on it in advance, to avoid wasting processing power in generating them again and again.
On the other hand, if the image already has mipmaps, they will be used, and a new set will be generated for the resulting image.
Interpolation INTERPOLATE_LANCZOS = 4
Performs Lanczos interpolation. This is the slowest image resizing mode, but it typically gives the best results, especially when downscalng images.
enum AlphaMode:
AlphaMode ALPHA_NONE = 0
Image does not have alpha.
AlphaMode ALPHA_BIT = 1
Image stores alpha in a single bit.
AlphaMode ALPHA_BLEND = 2
Image uses alpha.
enum CompressMode:
CompressMode COMPRESS_S3TC = 0
Use S3TC compression.
CompressMode COMPRESS_ETC = 1
Use ETC compression.
CompressMode COMPRESS_ETC2 = 2
Use ETC2 compression.
CompressMode COMPRESS_BPTC = 3
Use BPTC compression.
enum UsedChannels:
UsedChannels USED_CHANNELS_L = 0
UsedChannels USED_CHANNELS_LA = 1
UsedChannels USED_CHANNELS_R = 2
UsedChannels USED_CHANNELS_RG = 3
UsedChannels USED_CHANNELS_RGB = 4
UsedChannels USED_CHANNELS_RGBA = 5
enum CompressSource:
CompressSource COMPRESS_SOURCE_GENERIC = 0
Source texture (before compression) is a regular texture. Default for all textures.
CompressSource COMPRESS_SOURCE_SRGB = 1
Source texture (before compression) is in sRGB space.
CompressSource COMPRESS_SOURCE_NORMAL = 2
Source texture (before compression) is a normal texture (e.g. it can be compressed into two channels).
enum ASTCFormat:
ASTCFormat ASTC_FORMAT_4x4 = 0
Hint to indicate that the high quality 4x4 ASTC compression format should be used.
ASTCFormat ASTC_FORMAT_8x8 = 1
Hint to indicate that the low quality 8x8 ASTC compression format should be used.
Constants¶
MAX_WIDTH = 16777216
The maximal width allowed for Image resources.
MAX_HEIGHT = 16777216
The maximal height allowed for Image resources.
Property Descriptions¶
Dictionary data = { "data": PackedByteArray(), "format": "Lum8", "height": 0, "mipmaps": false, "width": 0 }
Holds all the image's color data in a given format. See Format constants.
Method Descriptions¶
void adjust_bcs ( float brightness, float contrast, float saturation )
There is currently no description for this method. Please help us by contributing one!
void blend_rect ( Image src, Rect2i src_rect, Vector2i dst )
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_mask ( Image src, Image mask, Rect2i src_rect, Vector2i dst )
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 blit_rect ( Image src, Rect2i src_rect, Vector2i dst )
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.
void blit_rect_mask ( Image src, Image mask, Rect2i src_rect, Vector2i dst )
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 bump_map_to_normal_map ( float bump_scale=1.0 )
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 clear_mipmaps ( )
Removes the image's mipmaps.
Error compress ( CompressMode mode, CompressSource source=0, ASTCFormat astc_format=0 )
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 mode
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.
Error compress_from_channels ( CompressMode mode, UsedChannels channels, ASTCFormat astc_format=0 )
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 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.
Dictionary compute_image_metrics ( Image compared_image, bool use_luma )
Compute image metrics on the current image and the compared image.
The dictionary contains max
, mean
, mean_squared
, root_mean_squared
and peak_snr
.
void convert ( Format format )
Converts the image's format. See Format constants.
void copy_from ( Image src )
Copies src
image to this image.
Image create ( int width, int height, bool use_mipmaps, Format format ) 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 generate_mipmaps.
Image create_from_data ( int width, int height, bool use_mipmaps, Format format, PackedByteArray data ) 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 generate_mipmaps.
void crop ( int width, int height )
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.
Error 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.
AlphaMode detect_alpha ( ) const
Returns ALPHA_BLEND if the image has data for alpha values. Returns ALPHA_BIT if all the alpha values are stored in a single bit. Returns ALPHA_NONE if no data for alpha values is found.
UsedChannels detect_used_channels ( CompressSource source=0 ) const
There is cu