Image¶
Inherits:Resource<Reference<Object
Image datatype.
Description¶
Native image datatype. Contains image data which can be converted to anImageTexture and provides commonly usedimage processing methods. The maximum width and height for anImage areMAX_WIDTH andMAX_HEIGHT.
AnImage cannot be assigned to atexture property of an object directly (such asSprite), and has to be converted manually to anImageTexture 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 | blend_rect(Image src,Rect2 src_rect,Vector2 dst) |
void | blend_rect_mask(Image src,Image mask,Rect2 src_rect,Vector2 dst) |
void | |
void | blit_rect_mask(Image src,Image mask,Rect2 src_rect,Vector2 dst) |
void | bumpmap_to_normalmap(float bump_scale=1.0) |
void | |
compress(CompressMode mode,CompressSource source,float lossy_quality) | |
void | |
void | |
void | |
void | create_from_data(int width,int height,bool use_mipmaps,Format format,PoolByteArray data) |
void | |
void | |
void | |
void | |
void | flip_x() |
void | flip_y() |
generate_mipmaps(bool renormalize=false) | |
get_mipmap_offset(int mipmap)const | |
get_pixelv(Vector2 src)const | |
load_bmp_from_buffer(PoolByteArray buffer) | |
load_jpg_from_buffer(PoolByteArray buffer) | |
load_png_from_buffer(PoolByteArray buffer) | |
load_tga_from_buffer(PoolByteArray buffer) | |
load_webp_from_buffer(PoolByteArray buffer) | |
void | lock() |
void | |
void | |
void | resize(int width,int height,Interpolation interpolation=1) |
void | resize_to_po2(bool square=false,Interpolation interpolation=1) |
void | |
void | set_pixelv(Vector2 dst,Color color) |
void | |
void | |
void | unlock() |
Enumerations¶
enumFormat:
FORMAT_L8 =0 --- Texture format with a single 8-bit depth representing luminance.
FORMAT_LA8 =1 --- OpenGL texture format with two values, luminance and alpha each stored with 8 bits.
FORMAT_R8 =2 --- OpenGL texture format
REDwith a single component and a bitdepth of 8.
Note: When using the GLES2 backend, this uses the alpha channel instead of the red channel for storage.
FORMAT_RG8 =3 --- OpenGL texture format
RGwith two components and a bitdepth of 8 for each.FORMAT_RGB8 =4 --- OpenGL texture format
RGBwith three components, each with a bitdepth of 8.
Note: When creating anImageTexture, an sRGB to linear color space conversion is performed.
FORMAT_RGBA8 =5 --- OpenGL texture format
RGBAwith four components, each with a bitdepth of 8.
Note: When creating anImageTexture, an sRGB to linear color space conversion is performed.
FORMAT_RGBA4444 =6 --- OpenGL texture format
RGBAwith four components, each with a bitdepth of 4.FORMAT_RGBA5551 =7 --- OpenGL texture format
GL_RGB5_A1where 5 bits of depth for each component of RGB and one bit for alpha.FORMAT_RF =8 --- OpenGL texture format
GL_R32Fwhere there's one component, a 32-bit floating-point value.FORMAT_RGF =9 --- OpenGL texture format
GL_RG32Fwhere there are two components, each a 32-bit floating-point values.FORMAT_RGBF =10 --- OpenGL texture format
GL_RGB32Fwhere there are three components, each a 32-bit floating-point values.FORMAT_RGBAF =11 --- OpenGL texture format
GL_RGBA32Fwhere there are four components, each a 32-bit floating-point values.FORMAT_RH =12 --- OpenGL texture format
GL_R32Fwhere there's one component, a 16-bit "half-precision" floating-point value.FORMAT_RGH =13 --- OpenGL texture format
GL_RG32Fwhere there are two components, each a 16-bit "half-precision" floating-point value.FORMAT_RGBH =14 --- OpenGL texture format
GL_RGB32Fwhere there are three components, each a 16-bit "half-precision" floating-point value.FORMAT_RGBAH =15 --- OpenGL texture format
GL_RGBA32Fwhere there are four components, each a 16-bit "half-precision" floating-point value.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_DXT1 =17 --- TheS3TC 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 anImageTexture, an sRGB to linear color space conversion is performed.
FORMAT_DXT3 =18 --- TheS3TC 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 anImageTexture, an sRGB to linear color space conversion is performed.
FORMAT_DXT5 =19 --- TheS3TC 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 anImageTexture, an sRGB to linear color space conversion is performed.
FORMAT_RGTC_R =20 --- Texture format that usesRed Green Texture Compression, normalizing the red channel data using the same compression algorithm that DXT5 uses for the alpha channel.
FORMAT_RGTC_RG =21 --- Texture format that usesRed Green Texture Compression, normalizing the red and green channel data using the same compression algorithm that DXT5 uses for the alpha channel.
FORMAT_BPTC_RGBA =22 --- Texture format that usesBPTC compression with unsigned normalized RGBA components.
Note: When creating anImageTexture, an sRGB to linear color space conversion is performed.
FORMAT_BPTC_RGBF =23 --- Texture format that usesBPTC compression with signed floating-point RGB components.
FORMAT_BPTC_RGBFU =24 --- Texture format that usesBPTC compression with unsigned floating-point RGB components.
FORMAT_PVRTC2 =25 --- Texture format used on PowerVR-supported mobile platforms, uses 2-bit color depth with no alpha. More information can be foundhere.
Note: When creating anImageTexture, an sRGB to linear color space conversion is performed.
FORMAT_PVRTC2A =26 --- Same asPVRTC2, but with an alpha component.
FORMAT_PVRTC4 =27 --- Similar toPVRTC2, but with 4-bit color depth and no alpha.
FORMAT_PVRTC4A =28 --- Same asPVRTC4, but with an alpha component.
FORMAT_ETC =29 ---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_ETC2_R11 =30 ---Ericsson Texture Compression format 2 (
R11_EACvariant), which provides one channel of unsigned data.FORMAT_ETC2_R11S =31 ---Ericsson Texture Compression format 2 (
SIGNED_R11_EACvariant), which provides one channel of signed data.FORMAT_ETC2_RG11 =32 ---Ericsson Texture Compression format 2 (
RG11_EACvariant), which provides two channels of unsigned data.FORMAT_ETC2_RG11S =33 ---Ericsson Texture Compression format 2 (
SIGNED_RG11_EACvariant), which provides two channels of signed data.FORMAT_ETC2_RGB8 =34 ---Ericsson Texture Compression format 2 (
RGB8variant), which is a follow-up of ETC1 and compresses RGB888 data.
Note: When creating anImageTexture, an sRGB to linear color space conversion is performed.
FORMAT_ETC2_RGBA8 =35 ---Ericsson Texture Compression format 2 (
RGBA8variant), which compresses RGBA8888 data with full alpha support.
Note: When creating anImageTexture, an sRGB to linear color space conversion is performed.
FORMAT_ETC2_RGB8A1 =36 ---Ericsson Texture Compression format 2 (
RGB8_PUNCHTHROUGH_ALPHA1variant), which compresses RGBA data to make alpha either fully transparent or fully opaque.
Note: When creating anImageTexture, an sRGB to linear color space conversion is performed.
FORMAT_MAX =37 --- Represents the size of theFormat enum.
enumInterpolation:
INTERPOLATE_NEAREST =0 --- Performs nearest-neighbor interpolation. If the image is resized, it will be pixelated.
INTERPOLATE_BILINEAR =1 --- Performs bilinear interpolation. If the image is resized, it will be blurry. This mode is faster thanINTERPOLATE_CUBIC, but it results in lower quality.
INTERPOLATE_CUBIC =2 --- Performs cubic interpolation. If the image is resized, it will be blurry. This mode often gives better results compared toINTERPOLATE_BILINEAR, at the cost of being slower.
INTERPOLATE_TRILINEAR =3 --- Performs bilinear separately on the two most-suited mipmap levels, then linearly interpolates between them.
It's slower thanINTERPOLATE_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 callgenerate_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.
INTERPOLATE_LANCZOS =4 --- Performs Lanczos interpolation. This is the slowest image resizing mode, but it typically gives the best results, especially when downscalng images.
enumAlphaMode:
ALPHA_NONE =0 --- Image does not have alpha.
ALPHA_BIT =1 --- Image stores alpha in a single bit.
ALPHA_BLEND =2 --- Image uses alpha.
enumCompressMode:
COMPRESS_S3TC =0 --- Use S3TC compression.
COMPRESS_PVRTC2 =1 --- Use PVRTC2 compression.
COMPRESS_PVRTC4 =2 --- Use PVRTC4 compression.
COMPRESS_ETC =3 --- Use ETC compression.
COMPRESS_ETC2 =4 --- Use ETC2 compression.
enumCompressSource:
COMPRESS_SOURCE_GENERIC =0 --- Source texture (before compression) is a regular texture. Default for all textures.
COMPRESS_SOURCE_SRGB =1 --- Source texture (before compression) is in sRGB space.
COMPRESS_SOURCE_NORMAL =2 --- Source texture (before compression) is a normal texture (e.g. it can be compressed into two channels).
COMPRESS_SOURCE_LAYERED =3 --- Source texture (before compression) is aTextureLayered.
Constants¶
MAX_WIDTH =16384 --- The maximal width allowed for
Imageresources.MAX_HEIGHT =16384 --- The maximal height allowed for
Imageresources.
Property Descriptions¶
Dictionarydata
Default |
|
Holds all the image's color data in a given format. SeeFormat constants.
Method Descriptions¶
Alpha-blendssrc_rect fromsrc image to this image at coordinatesdest.
Alpha-blendssrc_rect fromsrc image to this image usingmask image at coordinatesdst. Alpha channels are required for bothsrc andmask.dst pixels andsrc pixels will blend if the corresponding mask pixel's alpha value is not 0.src image andmask imagemust have the same size (width and height) but they can have different formats.
Copiessrc_rect fromsrc image to this image at coordinatesdst.
Blitssrc_rect area fromsrc image to this image at the coordinates given bydst.src pixel is copied ontodst if the correspondingmask pixel's alpha value is not 0.src image andmask imagemust have the same size (width and height) but they can have different formats.
voidbumpmap_to_normalmap(float bump_scale=1.0)
Converts a bumpmap to a normalmap. A bumpmap provides a height offset per-pixel, while a normalmap provides a normal direction per pixel.
voidclear_mipmaps()
Removes the image's mipmaps.
Errorcompress(CompressMode mode,CompressSource source,float lossy_quality)
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. SeeCompressMode andCompressSource constants.
voidconvert(Format format)
Converts the image's format. SeeFormat constants.
voidcopy_from(Image src)
Copiessrc image to this image.
Creates an empty image of given size and format. SeeFormat constants. Ifuse_mipmaps istrue then generate mipmaps for this image. See thegenerate_mipmaps.
voidcreate_from_data(int width,int height,bool use_mipmaps,Format format,PoolByteArray data)
Creates a new image of given size and format. SeeFormat constants. Fills the image with the given raw data. Ifuse_mipmaps istrue then loads mipmaps for this image fromdata. Seegenerate_mipmaps.
Crops the image to the givenwidth andheight. If the specified size is larger than the current size, the extra area is filled with black pixels.
Errordecompress()
Decompresses the image if it is compressed. Returns an error if decompress function is not available.
ReturnsALPHA_BLEND if the image has data for alpha values. ReturnsALPHA_BIT if all the alpha values are stored in a single bit. ReturnsALPHA_NONE if no data for alpha values is found.
voidexpand_x2_hq2x()
Stretches the image and enlarges it by a factor of 2. No interpolation is done.
voidfill(Color color)
Fills the image with a givenColor.
voidfix_alpha_edges()
Blends low-alpha pixels with nearby pixels.
voidflip_x()
Flips the image horizontally.
voidflip_y()
Flips the image vertically.
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 is0.
Note: Mipmap generation is done on the CPU, is single-threaded and isalways done on the main thread. This means generating mipmaps will result in noticeable stuttering during gameplay, even ifgenerate_mipmaps is called from aThread.
PoolByteArrayget_data()const
Returns a copy of the image's raw data.
Returns the image's format. SeeFormat constants.
Returns the image's height.
Returns the offset where the image's mipmap with indexmipmap is stored in thedata dictionary.
Returns the color of the pixel at(x,y) if the image is locked. If the image is unlocked, it always returns aColor with the value(0,0,0,1.0). This is the same asget_pixelv, but two integer arguments instead of a Vector2 argument.
Returns the color of the pixel atsrc if the image is locked. If the image is unlocked, it always returns aColor with the value(0,0,0,1.0). This is the same asget_pixel, but with a Vector2 argument instead of two integer arguments.
Returns a new image that is a copy of the image's area specified withrect.
Returns the image's size (width and height).
Returns aRect2 enclosing the visible portion of the image, considering each pixel with a non-zero alpha channel as visible.
Returns the image's width.
Returnstrue if the image has generated mipmaps.
Returnstrue if the image is compressed.
Returnstrue if the image has no data.
Returnstrue if all the image's pixels have an alpha value of 0. Returnsfalse if any pixel has an alpha value higher than 0.
Loads an image from filepath. SeeSupported 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 theuser:// directory, and may not work in exported projects.
See alsoImageTexture description for usage examples.
Errorload_bmp_from_buffer(PoolByteArray buffer)
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.
Errorload_jpg_from_buffer(PoolByteArray buffer)
Loads an image from the binary contents of a JPEG file.
Errorload_png_from_buffer(PoolByteArray buffer)
Loads an image from the binary contents of a PNG file.
Errorload_tga_from_buffer(PoolByteArray buffer)
Loads an image from the binary contents of a TGA file.
Errorload_webp_from_buffer(PoolByteArray buffer)
Loads an image from the binary contents of a WebP file.
voidlock()
Locks the data for reading and writing access. Sends an error to the console if the image is not locked when reading or writing a pixel.
voidnormalmap_to_xy()
Converts the image's data to represent coordinates on a 3D plane. This is used when the image represents a normalmap. A normalmap can add lots of detail to a 3D surface without increasing the polygon count.
voidpremultiply_alpha()
Multiplies color values with alpha values. Resulting color values for a pixel are(color*alpha)/256.
voidresize(int width,int height,Interpolation interpolation=1)
Resizes the image to the givenwidth andheight. New pixels are calculated using theinterpolation mode defined viaInterpolation constants.
voidresize_to_po2(bool square=false,Interpolation interpolation=1)
Resizes the image to the nearest power of 2 for the width and height. Ifsquare istrue then set width and height to be the same. New pixels are calculated using theinterpolation mode defined viaInterpolation constants.
Imagergbe_to_srgb()
Converts a standard RGBE (Red Green Blue Exponent) image to an sRGB image.
Saves the image as an EXR file topath. Ifgrayscale istrue 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 meanssave_exr will return@GlobalScope.ERR_UNAVAILABLE when it is called from an exported project.
Saves the image as a PNG file topath.
PoolByteArraysave_png_to_buffer()const
Sets theColor of the pixel at(x,y) if the image is locked. Example:
varimg=Image.new()img.create(img_width,img_height,false,Image.FORMAT_RGBA8)img.lock()img.set_pixel(x,y,color)# Worksimg.unlock()img.set_pixel(x,y,color)# Does not have an effect
Sets theColor of the pixel at(dst.x,dst.y) if the image is locked. Note that thedst values must be integers. Example:
varimg=Image.new()img.create(img_width,img_height,false,Image.FORMAT_RGBA8)img.lock()img.set_pixelv(Vector2(x,y),color)# Worksimg.unlock()img.set_pixelv(Vector2(x,y),color)# Does not have an effect
voidshrink_x2()
Shrinks the image by a factor of 2.
voidsrgb_to_linear()
Converts the raw data from the sRGB colorspace to a linear scale.
voidunlock()
Unlocks the data and prevents changes.