Image
- class Image
Imagedescribes a two dimensional array of pixels to draw.The pixels may be decoded in a raster bitmap, encoded in a
Pictureor compressed data stream, or located in GPU memory as a GPU texture.Imagecannot be modified after it is created.Imagemay allocate additional storage as needed; for instance, an encodedImagemay decode when drawn.Imagewidth and height are greater than zero. Creating anImagewith zero width or height returnsImageequal to nullptr.Imagemay be created fromBitmap,Pixmap,Surface,Picture, encoded streams, GPU texture, YUV_ColorSpace data, or hardware buffer. Encoded streams supported include BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP. Supported encoding details vary with platform.skia-pythonsupports a few high-level methods in addition to C++ API:image = skia.Image.open('/path/to/image.png') image = image.resize(120, 120) image = image.convert(alphaType=skia.kUnpremul_AlphaType) image.save('/path/to/output.jpg', skia.kJPEG)
NumPy arrays can be directly imported or exported:
image = skia.Image.fromarray(array) array = image.toarray()
General pixel buffers can be exchanged in the following approach:
image = skia.Image.frombytes(pixels, (100, 100)) pixels = image.tobytes()
Classes
Members:
Members:
Members:
Members:
Methods
MakeBackendTextureFromImage(context: skia.GrDirectContext, image: skia.Image, backendTexture: skia.GrBackendTexture) -> bool
MakeCrossContextFromPixmap(context: skia.GrDirectContext, pixmap: SkPixmap, buildMips: bool, limitToMaxTextureSize: bool = False) -> skia.Image
MakeFromAdoptedTexture(context: skia.GrRecordingContext, backendTexture: skia.GrBackendTexture, origin: skia.GrSurfaceOrigin, colorType: skia.ColorType, alphaType: skia.AlphaType = skia.AlphaType.kPremul_AlphaType, colorSpace: skia.ColorSpace = None) -> skia.Image
MakeFromBitmap(bitmap: skia.Bitmap) -> skia.Image
MakeFromCompressedTexture(context: skia.GrRecordingContext, texture: skia.GrBackendTexture, origin: skia.GrSurfaceOrigin, alphaType: skia.AlphaType, colorSpace: skia.ColorSpace = None) -> skia.Image
MakeFromEncoded(encoded: skia.Data, alphaType: Optional[skia.AlphaType] = None) -> skia.Image
MakeFromPicture(picture: SkPicture, dimensions: skia.ISize, matrix: skia.Matrix = None, paint: SkPaint = None, bitDepth: skia.Image.BitDepth = skia.Image.BitDepth.kU8, colorSpace: skia.ColorSpace = None) -> skia.Image
MakeFromRaster(pixmap: SkPixmap) -> skia.Image
MakeFromTexture(context: skia.GrRecordingContext, texture: skia.GrBackendTexture, origin: skia.GrSurfaceOrigin, colorType: skia.ColorType, alphaType: skia.AlphaType, colorSpace: skia.ColorSpace = None) -> skia.Image
MakeFromYUVAPixmaps(context: skia.GrRecordingContext, pixmaps: SkYUVAPixmaps, buildMips: skia.GrMipmapped = skia.GrMipmapped.kNo, limitToMaxTextureSize: bool = False, imageColorSpace: skia.ColorSpace = None) -> skia.Image
MakeRasterCopy(pixmap: SkPixmap) -> skia.Image
MakeRasterData(info: skia.ImageInfo, pixels: Buffer, rowBytes: int) -> skia.Image
MakeRasterFromCompressed(data: skia.Data, width: int, height: int, type: skia.Image.CompressionType) -> skia.Image
MakeTextureFromCompressed(context: skia.GrDirectContext, data: skia.Data, width: int, height: int, type: skia.Image.CompressionType, mipMapped: skia.GrMipmapped = skia.GrMipmapped.kNo, isProtected: skia.GrProtected = skia.GrProtected.kNo) -> skia.Image
alphaType(self: skia.Image) -> skia.AlphaType
asLegacyBitmap(self: skia.Image, bitmap: skia.Bitmap, legacyBitmapMode: skia.Image.LegacyBitmapMode = skia.Image.kRO_LegacyBitmapMode) -> bool
bitmap(self: skia.Image, colorType: skia.ColorType = skia.ColorType.kUnknown_ColorType, alphaType: skia.AlphaType = skia.AlphaType.kUnknown_AlphaType, colorSpace: skia.ColorSpace = None) -> skia.Bitmap
bounds(self: skia.Image) -> skia.IRect
colorSpace(self: skia.Image) -> skia.ColorSpace
colorType(self: skia.Image) -> skia.ColorType
convert(self: skia.Image, colorType: skia.ColorType = skia.ColorType.kUnknown_ColorType, alphaType: skia.AlphaType = skia.AlphaType.kUnknown_AlphaType, colorSpace: skia.ColorSpace = None) -> skia.Image
dimensions(self: skia.Image) -> skia.ISize
flushAndSubmit(self: skia.Image, context: skia.GrDirectContext) -> None
fromarray(array: numpy.ndarray, colorType: skia.ColorType = skia.ColorType.kN32_ColorType, alphaType: skia.AlphaType = skia.AlphaType.kUnpremul_AlphaType, colorSpace: skia.ColorSpace = None, copy: bool = True) -> skia.Image
frombytes(array: Buffer, dimensions: skia.ISize, colorType: skia.ColorType = skia.ColorType.kN32_ColorType, alphaType: skia.AlphaType = skia.AlphaType.kUnpremul_AlphaType, colorSpace: skia.ColorSpace = None, copy: bool = True) -> skia.Image
getBackendTexture(self: skia.Image, flushPendingGrContextIO: bool, origin: skia.GrSurfaceOrigin = None) -> bool
hasMipmaps(self: skia.Image) -> bool
height(self: skia.Image) -> int
imageInfo(self: skia.Image) -> skia.ImageInfo
isAlphaOnly(self: skia.Image) -> bool
isLazyGenerated(self: skia.Image) -> bool
isOpaque(self: skia.Image) -> bool
isTextureBacked(self: skia.Image) -> bool
isValid(self: skia.Image, context: skia.GrRecordingContext = None) -> bool
makeColorSpace(self: skia.Image, target: skia.ColorSpace, direct: skia.GrDirectContext = None) -> skia.Image
makeColorTypeAndColorSpace(self: skia.Image, targetColorType: skia.ColorType, targetColorSpace: skia.ColorSpace = None, direct: skia.GrDirectContext = None) -> skia.Image
makeNonTextureImage(self: skia.Image, context: skia.GrDirectContext = None) -> skia.Image
makeRasterImage(self: skia.Image, cachingHint: skia.Image.CachingHint = skia.Image.CachingHint.kAllow_CachingHint) -> skia.Image
makeSubset(self: skia.Image, subset: skia.IRect, direct: skia.GrDirectContext = None) -> skia.Image
makeTextureImage(self: skia.Image, context: skia.GrDirectContext, mipMapped: skia.GrMipmapped = skia.GrMipmapped.kNo, budgeted: skia.Budgeted = skia.Budgeted.kYes) -> skia.Image
makeWithFilter(self: skia.Image, context: skia.GrRecordingContext, filter: SkImageFilter, subset: skia.IRect, clipBounds: skia.IRect, outSubset: skia.IRect, offset: skia.IPoint) -> skia.Image
open(fp: object) -> skia.Image
peekPixels(self: skia.Image, pixmap: SkPixmap) -> bool
ref(self: skia.RefCntBase) -> None
refColorSpace(self: skia.Image) -> skia.ColorSpace
refEncodedData(self: skia.Image) -> skia.Data
reinterpretColorSpace(self: skia.Image, newColorSpace: skia.ColorSpace = None) -> skia.Image
resize(self: skia.Image, width: int, height: int, options: skia.SamplingOptions = skia.SamplingOptions(), cachingHint: skia.Image.CachingHint = skia.Image.CachingHint.kAllow_CachingHint) -> skia.Image
save(self: skia.Image, fp: object, encodedImageFormat: skia.EncodedImageFormat = skia.EncodedImageFormat.kPNG, quality: int = 100) -> None
scalePixels(self: skia.Image, dst: SkPixmap, samplingOptions: skia.SamplingOptions = skia.SamplingOptions(), cachingHint: skia.Image.CachingHint = skia.Image.CachingHint.kAllow_CachingHint) -> bool
toarray(self: skia.Image, srcX: int = 0, srcY: int = 0, colorType: skia.ColorType = skia.ColorType.kUnknown_ColorType, alphaType: skia.AlphaType = skia.AlphaType.kUnpremul_AlphaType, colorSpace: skia.ColorSpace = None) -> numpy.ndarray
tobytes(self: skia.Image) -> object
unique(self: skia.RefCntBase) -> bool
uniqueID(self: skia.Image) -> int
unref(self: skia.RefCntBase) -> None
width(self: skia.Image) -> int
withDefaultMipmaps(self: skia.Image) -> skia.Image
Attributes
Methods
- static Image.MakeBackendTextureFromImage(context: skia.GrDirectContext, image: skia.Image, backendTexture: skia.GrBackendTexture) bool
Creates a
GrBackendTexturefrom the providedImage.Returns true and stores result in backendTexture if texture is created; otherwise, returns false and leaves backendTexture unmodified.
If
Imageis both texture backed and singly referenced, image is returned in backendTexture without conversion or making a copy.Imageis singly referenced if it was transferred solely using std::move().If
Imageis not texture backed, returns texture withImagecontents.- Parameters:
context (skia.GrContext) – GPU context
image (skia.Image) –
Imageused for texturebackendTexture (skia.GrBackendTexture) – storage for back-end texture
- Returns:
true if back-end texture was created
- static Image.MakeCrossContextFromPixmap(context: skia.GrDirectContext, pixmap: SkPixmap, buildMips: bool, limitToMaxTextureSize: bool = False) skia.Image
Creates
Imagefrom pixmap.Imageis uploaded to GPU back-end using context.Created
Imageis available to other GPU contexts, and is available across thread boundaries. All contexts must be in the same GPU share group, or otherwise share resources.When
Imageis no longer referenced, context releases texture memory asynchronously.GrBackendTexturecreated from pixmap is uploaded to matchSurfacecreated with dstColorSpace.ColorSpaceofImageis determined by pixmap.colorSpace().Imageis returned referring to GPU back-end if context is not nullptr, format of data is recognized and supported, and if context supports moving resources between contexts. Otherwise, pixmap pixel data is copied andImageas returned in raster format if possible; nullptr may be returned. Recognized GPU formats vary by platform and GPU back-end.- Parameters:
context (skia.GrDirectContext) – GPU context
pixmap (skia.Pixmap) –
ImageInfo, pixel address, and row bytesbuildMips (bool) – create
Imageas mip map if truelimitToMaxTextureSize (bool) – downscale image to GPU maximum texture size, if necessary
- Returns:
created
Image, or nullptr
- static Image.MakeFromAdoptedTexture(context: skia.GrRecordingContext, backendTexture: skia.GrBackendTexture, origin: skia.GrSurfaceOrigin, colorType: skia.ColorType, alphaType: skia.AlphaType = skia.AlphaType.kPremul_AlphaType, colorSpace: skia.ColorSpace = None) skia.Image
Creates
Imagefrom backendTexture associated with context.backendTexture and returned
Imageare managed internally, and are released when no longer needed.Imageis returned if format of backendTexture is recognized and supported. Recognized formats vary by GPU back-end.- Parameters:
context (skia.GrContext) – GPU context
backendTexture (skia.GrBackendTexture) – texture residing on GPU
colorSpace (skia.ColorSpace) – range of colors; may be nullptr
- Returns:
created
Image, or nullptr
- static Image.MakeFromBitmap(bitmap: skia.Bitmap) skia.Image
Creates
Imagefrom bitmap, sharing or copying bitmap pixels.If the bitmap is marked immutable, and its pixel memory is shareable, it may be shared instead of copied.
Imageis returned if bitmap is valid. ValidBitmapparameters include: dimensions are greater than zero; each dimension fits in 29 bits;ColorTypeandAlphaTypeare valid, andColorTypeis notkUnknown_ColorType; row bytes are large enough to hold one row of pixels; pixel address is not nullptr.- Parameters:
bitmap (skia.Bitmap) –
ImageInfo, row bytes, and pixels- Returns:
created
Image, or nullptr
- static Image.MakeFromCompressedTexture(context: skia.GrRecordingContext, texture: skia.GrBackendTexture, origin: skia.GrSurfaceOrigin, alphaType: skia.AlphaType, colorSpace: skia.ColorSpace = None) skia.Image
Creates an
Imagefrom a GPU backend texture.An
Imageis returned if the format of backendTexture is recognized and supported. Recognized formats vary by GPU back-end.- Parameters:
context (skia.GrRecordingContext) – the GPU context
backendTexture (skia.GrBackendTexture) – a texture already allocated by the GPU
alphaType (skia.AlphaType) – This characterizes the nature of the alpha values in the backend texture. For opaque compressed formats (e.g., ETC1) this should usually be set to
kOpaque_AlphaType.colorSpace (skia.ColorSpace) – This describes the color space of this image’s contents, as seen after sampling. In general, if the format of the backend texture is SRGB, some linear colorSpace should be supplied (e.g.,
ColorSpace.MakeSRGBLinear()). If the format of the backend texture is linear, then the colorSpace should include a description of the transfer function as well (e.g.,ColorSpace.MakeSRGB()).
- Returns:
created
Image, or nullptr
- static Image.MakeFromEncoded(encoded: skia.Data, alphaType: skia.AlphaType | None = None) skia.Image
Return an image backed by the encoded data, but attempt to defer decoding until the image is actually used/drawn.
This deferral allows the system to cache the result, either on the CPU or on the GPU, depending on where the image is drawn. If memory is low, the cache may be purged, causing the next draw of the image to have to re-decode.
If the encoded format is not supported, nullptr is returned.
- Parameters:
encoded – the encoded data
- Returns:
created
Image, or nullptr
- static Image.MakeFromPicture(picture: SkPicture, dimensions: skia.ISize, matrix: skia.Matrix = None, paint: SkPaint = None, bitDepth: skia.Image.BitDepth = skia.Image.BitDepth.kU8, colorSpace: skia.ColorSpace = None) skia.Image
Creates
Imagefrom picture.Returned
Imagewidth and height are set by dimensions.Imagedraws picture with matrix and paint, set to bitDepth and colorSpace.If matrix is nullptr, draws with identity
Matrix. If paint is nullptr, draws with defaultPaint. colorSpace may be nullptr.- Parameters:
picture (skia.Picture) – stream of drawing commands
dimensions (skia.ISize) – width and height
matrix (skia.Matrix) –
Matrixto rotate, scale, translate, and so on; may be nullptrpaint (skia.Paint) –
Paintto apply transparency, filtering, and so on; may be nullptrbitDepth (skia.Image.BitDepth) – 8-bit integer or 16-bit float: per component
colorSpace (skia.ColorSpace) – range of colors; may be nullptr
- Returns:
created
Image, or nullptr
- static Image.MakeFromRaster(pixmap: SkPixmap) skia.Image
Creates
Imagefrom pixmap, sharingPixmappixels.Imageis returned if pixmap is valid. ValidPixmapparameters include: dimensions are greater than zero; each dimension fits in 29 bits;ColorTypeandAlphaTypeare valid, andColorTypeis notkUnknown_ColorType; row bytes are large enough to hold one row of pixels; pixel address is not nullptr.- Parameters:
pixmap (skia.Pixmap) –
ImageInfo, pixel address, and row bytes- Returns:
Imagesharing pixmap
- static Image.MakeFromTexture(context: skia.GrRecordingContext, texture: skia.GrBackendTexture, origin: skia.GrSurfaceOrigin, colorType: skia.ColorType, alphaType: skia.AlphaType, colorSpace: skia.ColorSpace = None) skia.Image
Creates
Imagefrom GPU texture associated with context.Caller is responsible for managing the lifetime of GPU texture.
Imageis returned if format of backendTexture is recognized and supported. Recognized formats vary by GPU back-end.- Parameters:
context (skia.GrRecordingContext) – GPU context
backendTexture (skia.GrBackendTexture) – texture residing on GPU
colorSpace (skia.ColorSpace) – range of colors; may be nullptr
- Returns:
created
Image, or nullptr
- static Image.MakeFromYUVAPixmaps(context: skia.GrRecordingContext, pixmaps: SkYUVAPixmaps, buildMips: skia.GrMipmapped = skia.GrMipmapped.kNo, limitToMaxTextureSize: bool = False, imageColorSpace: skia.ColorSpace = None) skia.Image
Creates
ImagefromYUVAPixmaps.The image will remain planar with each plane converted to a texture using the passed
GrRecordingContext.YUVAPixmapshas aYUVAInfowhich specifies the transformation from YUV to RGB. TheColorSpaceof the resulting RGB values is specified by imageColorSpace. This will be theColorSpacereported by the image and when drawn the RGB values will be converted from this space into the destination space (if the destination is tagged).Currently, this is only supported using the GPU backend and will fail if context is nullptr.
YUVAPixmapsdoes not need to remain valid after this returns.- Parameters:
context – GPU context
pixmaps – The planes as pixmaps with supported
YUVAInfothat specifies conversion to RGB.buildMips – create internal YUVA textures as mip map if kYes. This is silently ignored if the context does not support mip maps.
limitToMaxTextureSize – downscale image to GPU maximum texture size, if necessary
imageColorSpace – range of colors of the resulting image; may be nullptr
- Returns:
created
Image, or nullptr
- static Image.MakeRasterCopy(pixmap: SkPixmap) skia.Image
Creates
ImagefromPixmapand copy of pixels.Since pixels are copied,
Pixmappixels may be modified or deleted without affectingImage.Imageis returned ifPixmapis valid. ValidPixmapparameters include: dimensions are greater than zero; each dimension fits in 29 bits;ColorTypeandAlphaTypeare valid, andColorTypeis notkUnknown_ColorType; row bytes are large enough to hold one row of pixels; pixel address is not nullptr.- Parameters:
pixmap (skia.Pixmap) –
ImageInfo, pixel address, and row bytes- Returns:
copy of
Pixmappixels, or nullptr
- static Image.MakeRasterData(info: skia.ImageInfo, pixels: Buffer, rowBytes: int) skia.Image
Creates
ImagefromImageInfo, sharing pixels.Imageis returned ifImageInfois valid. ValidImageInfoparameters include: dimensions are greater than zero; each dimension fits in 29 bits;ColorTypeandAlphaTypeare valid, andColorTypeis notkUnknown_ColorType; rowBytes are large enough to hold one row of pixels; pixels is not nullptr, and contains enough data forImage.- Parameters:
info (skia.ImageInfo) – contains width, height,
AlphaType,ColorType,ColorSpacepixels (Union[bytes,bytearray,memoryview]) – pixel storage
rowBytes (int) – size of pixel row or larger
- Returns:
Imagesharing pixels, or nullptr
- static Image.MakeRasterFromCompressed(data: skia.Data, width: int, height: int, type: skia.Image.CompressionType) skia.Image
Creates a CPU-backed
Imagefrom compressed data.This method will decompress the compressed data and create an image wrapping it. Any mipmap levels present in the compressed data are discarded.
- Parameters:
data (skia.Data) – compressed data to store in SkImage
width (int) – width of full SkImage
height (int) – height of full SkImage
type (skia.Image.CompressionType) – type of compression used
- Returns:
created
Image, or nullptr
- static Image.MakeTextureFromCompressed(context: skia.GrDirectContext, data: skia.Data, width: int, height: int, type: skia.Image.CompressionType, mipMapped: skia.GrMipmapped = skia.GrMipmapped.kNo, isProtected: skia.GrProtected = skia.GrProtected.kNo) skia.Image
Creates a GPU-backed
Imagefrom compressed data.This method will return an
Imagerepresenting the compressed data. If the GPU doesn’t support the specified compression method, the data will be decompressed and then wrapped in a GPU-backed image.Note: one can query the supported compression formats via
GrContext.compressedBackendFormat().- Parameters:
context (skia.GrDirectContext) – GPU context
width (int) – width of full
Imageheight (int) – height of full
Imagetype (skia.CompressionType) – type of compression used
mipMapped (skia.skgpu::Mipmapped) – does ‘data’ contain data for all the mipmap levels?
isProtected (skia.GrProtected) – do the contents of ‘data’ require DRM protection (on Vulkan)?
- Returns:
created
Image, or nullptr
- Image.__init__(*args, **kwargs)
- Image.alphaType(self: skia.Image) skia.AlphaType
Returns
AlphaType.AlphaTypereturned was a parameter to anImageconstructor, or was parsed from encoded data.
- Image.asLegacyBitmap(self: skia.Image, bitmap: skia.Bitmap, legacyBitmapMode: skia.Image.LegacyBitmapMode = skia.Image.kRO_LegacyBitmapMode) bool
Deprecated.
Creates raster
Bitmapwith same pixels asImage. If legacyBitmapMode iskRO_LegacyBitmapMode, returned bitmap is read-only and immutable. Returns true ifBitmapis stored in bitmap. Returns false and resets bitmap ifBitmapwrite did not succeed.
- Image.bitmap(self: skia.Image, colorType: skia.ColorType = skia.ColorType.kUnknown_ColorType, alphaType: skia.AlphaType = skia.AlphaType.kUnknown_AlphaType, colorSpace: skia.ColorSpace = None) skia.Bitmap
-
Pixels are always allocated and copied.
- Image.bounds(self: skia.Image) skia.IRect
- Image.colorSpace(self: skia.Image) skia.ColorSpace
Returns
ColorSpace, the range of colors, associated withImage.The reference count of
ColorSpaceis unchanged. The returnedColorSpaceis immutable.ColorSpacereturned was passed to anImageconstructor, or was parsed from encoded data.ColorSpacereturned may be ignored whenImageis drawn, depending on the capabilities of theSurfacereceiving the drawing.- Returns:
ColorSpaceinImage, or nullptr
- Image.colorType(self: skia.Image) skia.ColorType
Returns
ColorTypeif known; otherwise, returnskUnknown_ColorType.
- Image.convert(self: skia.Image, colorType: skia.ColorType = skia.ColorType.kUnknown_ColorType, alphaType: skia.AlphaType = skia.AlphaType.kUnknown_AlphaType, colorSpace: skia.ColorSpace = None) skia.Image
Creates
Imagein targetColorType,AlphatType, andColorSpace. Raises ifImagecould not be created.Pixels are converted only if pixel conversion is possible. If
ImageColorTypeiskGray_8_ColorType, orkAlpha_8_ColorType; colorType must be the same. IfImageColorTypeiskGray_8_ColorType, colorSpace must be the same. IfImageAlphaTypeiskOpaque_AlphaType, alphaType must be the same. IfImageColorSpaceis nullptr, colorSpace must be the same. Raises if pixel conversion is not possible.
- Image.dimensions(self: skia.Image) skia.ISize
- Image.encodeToData(*args, **kwargs)
Overloaded function.
encodeToData(self: skia.Image, encodedImageFormat: skia.EncodedImageFormat, quality: int) -> skia.Data
Encodes
Imagepixels, returning result asData.Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Imageencoding in a format requires both building with one or more of: SK_ENCODE_JPEG, SK_ENCODE_PNG, SK_ENCODE_WEBP; and platform support for the encoded format.If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can additionally be one of:
kICO,kBMP,kGIF.quality is a platform and format specific metric trading off size and encoding error. When used, quality equaling 100 encodes with the least error. quality may be ignored by the encoder.
encodeToData(self: skia.Image) -> skia.Data
Encodes
Imagepixels, returning result asData.Returns existing encoded data if present; otherwise,
Imageis encoded withkPNG. Skia must be built with SK_ENCODE_PNG to encodeImage.Returns nullptr if existing encoded data is missing or invalid, and encoding fails.
- return:
encoded
Image, or nullptr
- Image.flush(*args, **kwargs)
Overloaded function.
flush(self: skia.Image, arg0: skia.GrDirectContext, arg1: skia.GrFlushInfo) -> skia.GrSemaphoresSubmitted
Flushes any pending uses of texture-backed images in the GPU backend. If the image is not texture-backed (including promise texture images) or if the GrDirectContext does not have the same context ID as the context backing the image then this is a no-op.
If the image was not used in any non-culled draws in the current queue of work for the passed GrDirectContext then this is a no-op unless the GrFlushInfo contains semaphores or a finish proc. Those are respected even when the image has not been used.
- param context:
the context on which to flush pending usages of the image.
- param info:
flush options
flush(self: skia.Image, context: skia.GrDirectContext) -> None
- Image.flushAndSubmit(self: skia.Image, context: skia.GrDirectContext) None
Version of
flush()that uses a default GrFlushInfo.Also submits the flushed work to the GPU.
- static Image.fromarray(array: numpy.ndarray, colorType: skia.ColorType = skia.ColorType.kN32_ColorType, alphaType: skia.AlphaType = skia.AlphaType.kUnpremul_AlphaType, colorSpace: skia.ColorSpace = None, copy: bool = True) skia.Image
Creates a new
Imagefrom numpy array.- Parameters:
array (numpy.ndarray) – numpy ndarray of shape=(height, width, channels) and appropriate dtype. Must have non-zero width and height, and the valid number of channels for the specified color type.
colorType (skia.ColorType) – color type of the array
alphaType (skia.AlphaType) – alpha type of the array
colorSpace (skia.ColorSpace) – range of colors; may be nullptr
copy (bool) – Whether to copy pixels.
- static Image.frombytes(array: Buffer, dimensions: skia.ISize, colorType: skia.ColorType = skia.ColorType.kN32_ColorType, alphaType: skia.AlphaType = skia.AlphaType.kUnpremul_AlphaType, colorSpace: skia.ColorSpace = None, copy: bool = True) skia.Image
Creates a new
Imagefrom bytes.- Parameters:
pixels – raw bytes of pixels
dimensions (skia.ISize) – (width, height) tuple
colorType (skia.ColorType) – color type of the array
alphaType (skia.AlphaType) – alpha type of the array
colorSpace (skia.ColorSpace) – range of colors; may be nullptr
copy (bool) – Whether to copy pixels. When false is specified,
Imageshares the pixel buffer without copy.
- Image.getBackendTexture(self: skia.Image, flushPendingGrContextIO: bool, origin: skia.GrSurfaceOrigin = None) bool
Retrieves the back-end texture. If
Imagehas no back-end texture, an invalid object is returned. CallGrBackendTexture.isValid()to determine if the result is valid.If flushPendingGrContextIO is true, completes deferred I/O operations.
If origin in not nullptr, copies location of content drawn into
Image.- Parameters:
flushPendingGrContextIO – flag to flush outstanding requests
- Returns:
back-end API texture handle; invalid on failure
- Image.hasMipmaps(self: skia.Image) bool
Returns true if the image has mipmap levels.
- Image.height(self: skia.Image) int
Returns pixel row count.
- Returns:
pixel height in
Image
- Image.imageInfo(self: skia.Image) skia.ImageInfo
Returns a
ImageInfodescribing the width, height, color type, alpha type, and color space of theImage.- Returns:
image info of
Image.
- Image.isAlphaOnly(self: skia.Image) bool
Returns true if
Imagepixels represent transparency only.If true, each pixel is packed in 8 bits as defined by
kAlpha_8_ColorType.- Returns:
true if pixels represent a transparency mask
- Image.isLazyGenerated(self: skia.Image) bool
Returns true if
Imageis backed by an image-generator or other service that creates and caches its pixels or texture on-demand.- Returns:
true if
Imageis created as needed
- Image.isOpaque(self: skia.Image) bool
Returns true if pixels ignore their alpha value and are treated as fully opaque.
- Returns:
true if
AlphaTypeiskOpaque_AlphaType
- Image.isTextureBacked(self: skia.Image) bool
Returns true the contents of
Imagewas created on or uploaded to GPU memory, and is available as a GPU texture.- Returns:
true if
Imageis a GPU texture
- Image.isValid(self: skia.Image, context: skia.GrRecordingContext = None) bool
Returns true if
Imagecan be drawn on either raster surface or GPU surface.If context is nullptr, tests if
Imagedraws on raster surface; otherwise, tests ifImagedraws on GPU surface associated with context.Imagebacked by GPU texture may become invalid if associated GrRecordingContext is invalid. lazy image may be invalid and may not draw to raster surface or GPU surface or both.- Parameters:
context (skia.GrRecordingContext) – GPU context
- Returns:
true if
Imagecan be drawn
- Image.makeColorSpace(self: skia.Image, target: skia.ColorSpace, direct: skia.GrDirectContext = None) skia.Image
Creates
Imagein targetColorSpace.Returns nullptr if
Imagecould not be created.Returns original
Imageif it is in targetColorSpace. Otherwise, converts pixels fromImageColorSpaceto targetColorSpace. IfImagecolorSpace() returns nullptr,ImageColorSpaceis assumed to be sRGB.- Parameters:
target (skia.ColorSpace) –
ColorSpacedescribing color range of returnedImage- Returns:
created
Imagein targetColorSpace
- Image.makeColorTypeAndColorSpace(self: skia.Image, targetColorType: skia.ColorType, targetColorSpace: skia.ColorSpace = None, direct: skia.GrDirectContext = None) skia.Image
Experimental.
Creates
Imagein targetColorTypeandColorSpace. Returns nullptr ifImagecould not be created.Returns original
Imageif it is in targetColorTypeandColorSpace.- Parameters:
targetColorType (skia.ColorType) –
ColorTypeof returnedImagetargetColorSpace (skia.ColorSpace) –
ColorSpaceof returnedImage
- Returns:
created
Imagein targetColorTypeandColorSpace
- Image.makeNonTextureImage(self: skia.Image, context: skia.GrDirectContext = None) skia.Image
Returns raster image or lazy image.
Copies
Imagebacked by GPU texture into CPU memory if needed. Returns originalImageif decoded in raster bitmap, or if encoded in a stream.Returns nullptr if backed by GPU texture and copy fails.
- Returns:
raster image, lazy image, or nullptr
- Image.makeRasterImage(self: skia.Image, cachingHint: skia.Image.CachingHint = skia.Image.CachingHint.kAllow_CachingHint) skia.Image
Returns raster image.
Copies
Imagebacked by GPU texture into CPU memory, or decodesImagefrom lazy image. Returns originalImageif decoded in raster bitmap.Returns nullptr if copy, decode, or pixel read fails.
If cachingHint is
Image.CachingHint.kAllow_CachingHint, pixels may be retained locally. If cachingHint isImage.CachingHint.kDisallow_CachingHint, pixels are not added to the local cache.- Parameters:
cachingHint (skia.Image.CachingHint) – Caching hint
- Returns:
raster image, or nullptr
- Image.makeRawShader(*args, **kwargs)
Overloaded function.
makeRawShader(self: skia.Image, tmx: skia.TileMode = skia.TileMode.kClamp, tmy: skia.TileMode = skia.TileMode.kClamp, samplingOptions: skia.SamplingOptions = skia.SamplingOptions(), localMatrix: skia.Matrix = None) -> SkShader
makeRawShader functions like makeShader, but for images that contain non-color data. This includes images encoding things like normals, material properties (eg, roughness), heightmaps, or any other purely mathematical data that happens to be stored in an image. These types of images are useful with some programmable shaders (see: SkRuntimeEffect).
Shaderdimensions are taken fromImage.ShaderusesTileModerules to fill drawn area outsideImage. localMatrix permits transformingImagebeforeCanvasmatrix is applied.makeRawShader(self: skia.Image, tmx: skia.TileMode = skia.TileMode.kClamp, tmy: skia.TileMode = skia.TileMode.kClamp, samplingOptions: skia.SamplingOptions = skia.SamplingOptions(), localMatrix: skia.Matrix) -> SkShader
makeRawShader functions like makeShader, but for images that contain non-color data. This includes images encoding things like normals, material properties (eg, roughness), heightmaps, or any other purely mathematical data that happens to be stored in an image. These types of images are useful with some programmable shaders (see: SkRuntimeEffect).
Shaderdimensions are taken fromImage.ShaderusesTileModerules to fill drawn area outsideImage. localMatrix permits transformingImagebeforeCanvasmatrix is applied.makeRawShader(self: skia.Image, samplingOptions: skia.SamplingOptions = skia.SamplingOptions(), localMatrix: skia.Matrix) -> SkShader
makeRawShader functions like makeShader, but for images that contain non-color data. This includes images encoding things like normals, material properties (eg, roughness), heightmaps, or any other purely mathematical data that happens to be stored in an image. These types of images are useful with some programmable shaders (see: SkRuntimeEffect).
Shaderdimensions are taken fromImage. localMatrix permits transformingImagebeforeCanvasmatrix is applied.makeRawShader(self: skia.Image, samplingOptions: skia.SamplingOptions = skia.SamplingOptions(), localMatrix: skia.Matrix = None) -> SkShader
makeRawShader functions like makeShader, but for images that contain non-color data. This includes images encoding things like normals, material properties (eg, roughness), heightmaps, or any other purely mathematical data that happens to be stored in an image. These types of images are useful with some programmable shaders (see: SkRuntimeEffect).
Shaderdimensions are taken fromImage. localMatrix permits transformingImagebeforeCanvasmatrix is applied.
- Image.makeShader(*args, **kwargs)
Overloaded function.
makeShader(self: skia.Image, tmx: skia.TileMode = skia.TileMode.kClamp, tmy: skia.TileMode = skia.TileMode.kClamp, samplingOptions: skia.SamplingOptions = skia.SamplingOptions(), localMatrix: skia.Matrix = None) -> SkShader
makeShader(self: skia.Image, tmx: skia.TileMode = skia.TileMode.kClamp, tmy: skia.TileMode = skia.TileMode.kClamp, samplingOptions: skia.SamplingOptions = skia.SamplingOptions(), localMatrix: skia.Matrix) -> SkShader
makeShader(self: skia.Image, samplingOptions: skia.SamplingOptions = skia.SamplingOptions(), localMatrix: skia.Matrix) -> SkShader
makeShader(self: skia.Image, samplingOptions: skia.SamplingOptions = skia.SamplingOptions(), localMatrix: skia.Matrix = None) -> SkShader
- Image.makeSubset(self: skia.Image, subset: skia.IRect, direct: skia.GrDirectContext = None) skia.Image
Returns subset of
Image.subset must be fully contained by
Imagedimensions(). The implementation may share pixels, or may copy them.Returns nullptr if subset is empty, or subset is not contained by bounds, or pixels in
Imagecould not be read or copied.- Parameters:
subset (skia.IRect) – bounds of returned
Image- Returns:
partial or full
Image, or nullptr
- Image.makeTextureImage(self: skia.Image, context: skia.GrDirectContext, mipMapped: skia.GrMipmapped = skia.GrMipmapped.kNo, budgeted: skia.Budgeted = skia.Budgeted.kYes) skia.Image
Returns
Imagebacked by GPU texture associated with context.Returned
Imageis compatible withSurfacecreated with dstColorSpace. The returnedImagerespects mipMapped setting; if mipMapped equalsskgpu::Mipmapped.kYes, the backing texture allocates mip map levels.The mipMapped parameter is effectively treated as kNo if MIP maps are not supported by the GPU.
Returns original
Imageif the image is already texture-backed, the context matches, and mipMapped is compatible with the backing GPU texture.Budgetedis ignored in this case.Returns nullptr if context is nullptr, or if
Imagewas created with another GrDirectContext.- Parameters:
context (GrDirectContext) – the GrDirectContext in play, if it exists
mipMapped (skgpu::Mipmapped) – whether created
Imagetexture must allocate mip map levelsbudgeted (skia.Budgeted) – whether to count a newly created texture for the returned image counts against the GrDirectContext’s budget.
- Returns:
created
Image, or nullptr
- Image.makeWithFilter(self: skia.Image, context: skia.GrRecordingContext, filter: SkImageFilter, subset: skia.IRect, clipBounds: skia.IRect, outSubset: skia.IRect, offset: skia.IPoint) skia.Image
Creates filtered
Image.filter processes original
Image, potentially changing color, position, and size. subset is the bounds of originalImageprocessed by filter. clipBounds is the expected bounds of the filteredImage. outSubset is required storage for the actual bounds of the filteredImage. offset is required storage for translation of returnedImage.Returns nullptr if
Imagecould not be created. If nullptr is returned, outSubset and offset are undefined.Useful for animation of
ImageFilterthat varies size from frame to frame. ReturnedImageis created larger than required by filter so that GPU texture can be reused with different sized effects. outSubset describes the valid bounds of GPU texture returned. offset translates the returnedImageto keep subsequent animation frames aligned with respect to each other.- Parameters:
context (skia.GrRecordingContext) – the GrRecordingContext in play - if it exists
filter (skia.ImageFilter) – how
Imageis sampled when transformedsubset (skia.IRect) – bounds of
Imageprocessed by filterclipBounds (skia.IRect) – expected bounds of filtered
ImageoutSubset (skia.IRect) – output for returned
Imageboundsoffset (skia.IPoint) – output for returned
Imagetranslation
- Returns:
filtered
Image, or nullptr
- static Image.open(fp: object) skia.Image
Creates
Imagefrom file path or file-like object.Shortcut for the following:
if hasattr(fp, 'read') and hasattr(fp, 'seek'): fp.seek(0) data = skia.Data.MakeWithCopy(fp.read()) else: data = skia.Data.MakeFromFileName(fp) image = skia.Image.MakeFromEncoded(data)
- Parameters:
fp – file path or file-like object that has seek and read method. file must be opened in binary mode.
- Image.peekPixels(self: skia.Image, pixmap: SkPixmap) bool
Copies
Imagepixel address, row bytes, andImageInfoto pixmap, if address is available, and returns true.If pixel address is not available, return false and leave pixmap unchanged.
- Parameters:
pixmap (skia.Pixmap) – storage for pixel state if pixels are readable; otherwise, ignored
- Returns:
true if
Imagehas direct access to pixels
- Image.readPixels(*args, **kwargs)
Overloaded function.
readPixels(self: skia.Image, context: skia.GrDirectContext, dstInfo: skia.ImageInfo, dstPixels: Buffer, dstRowBytes: int, srcX: int = 0, srcY: int = 0, cachingHint: skia.Image.CachingHint = skia.Image.CachingHint.kAllow_CachingHint) -> bool
Copies a
Rectof pixels fromImageto dst. Copy starts at (srcX, srcY), and does not exceedImage(width(), height()).dstInfo specifies width, height,
ColorType,AlphaType, andColorSpaceof destination. dstRowBytes specifics the gap from one destination row to the next. Returns true if pixels are copied. Returns false if:dstInfo.addr() equals nullptr
dstRowBytes is less than
dstInfo.minRowBytes()PixelRefis nullptr
Pixels are copied only if pixel conversion is possible. If
ImageColorTypeiskGray_8_ColorType, orkAlpha_8_ColorType; dst.colorType() must match. IfImageColorTypeiskGray_8_ColorType, dst.colorSpace() must match. IfImageAlphaTypeiskOpaque_AlphaType, dst.alphaType() must match. IfImageColorSpaceis nullptr, dst.colorSpace() must match. Returns false if pixel conversion is not possible.srcX and srcY may be negative to copy only top or left of source. Returns false if width() or height() is zero or negative. Returns false if abs(srcX) >= Image width(), or if abs(srcY) >= Image height().
If cachingHint is kAllow_CachingHint, pixels may be retained locally. If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
- param context:
the GrDirectContext in play, if it exists
- param dstInfo:
destination width, height, pixels,
ColorType,AlphaType,ColorSpace- param dstPixels:
destination pixel storage
- param dstRowBytes:
destination row length
- param srcX:
column index whose absolute value is less than width()
- param srcY:
row index whose absolute value is less than height()
- param cachingHint:
whether the pixels should be cached locally
- return:
true if pixels are copied to dstPixels
readPixels(self: skia.Image, context: skia.GrDirectContext, dst: SkPixmap, srcX: int, srcY: int, cachingHint: skia.Image.CachingHint = skia.Image.CachingHint.kAllow_CachingHint) -> bool
Copies a
Rectof pixels fromImageto dst. Copy starts at (srcX, srcY), and does not exceedImage(width(), height()).dst specifies width, height,
ColorType,AlphaType,ColorSpace, pixel storage, and row bytes of destination. dst.rowBytes() specifics the gap from one destination row to the next. Returns true if pixels are copied. Returns false if:dst pixel storage equals nullptr
dst.rowBytes is less than
ImageInfo.minRowBytes()PixelRefis nullptr
Pixels are copied only if pixel conversion is possible. If
ImageColorTypeiskGray_8_ColorType, orkAlpha_8_ColorType; dst.colorType() must match. IfImageColorTypeiskGray_8_ColorType, dst.colorSpace() must match. IfImageAlphaTypeiskOpaque_AlphaType, dst.alphaType() must match. IfImageColorSpaceis nullptr, dst.colorSpace() must match. Returns false if pixel conversion is not possible.srcX and srcY may be negative to copy only top or left of source. Returns false if width() or height() is zero or negative. Returns false if abs(srcX) >= Image width(), or if abs(srcY) >= Image height().
If cachingHint is kAllow_CachingHint, pixels may be retained locally. If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
- param context:
the GrDirectContext in play, if it exists
- param dst:
- param srcX:
column index whose absolute value is less than width()
- param srcY:
row index whose absolute value is less than height()
- param cachingHint:
whether the pixels should be cached locally
- return:
true if pixels are copied to dst
readPixels(self: skia.Image, dstInfo: skia.ImageInfo, dstPixels: Buffer, dstRowBytes: int, srcX: int = 0, srcY: int = 0, cachingHint: skia.Image.CachingHint = skia.Image.CachingHint.kAllow_CachingHint) -> bool
Deprecated. Use the variants that accept a GrDirectContext.
readPixels(self: skia.Image, dst: SkPixmap, srcX: int, srcY: int, cachingHint: skia.Image.CachingHint = skia.Image.CachingHint.kAllow_CachingHint) -> bool
Deprecated. Use the variants that accept a GrDirectContext.
- Image.ref(self: skia.RefCntBase) None
Increment the reference count.
Must be balanced by a call to
unref().
- Image.refColorSpace(self: skia.Image) skia.ColorSpace
Returns a smart pointer to
ColorSpace, the range of colors, associated withImage.The smart pointer tracks the number of objects sharing this
ColorSpacereference so the memory is released when the owners destruct.The returned
ColorSpaceis immutable.ColorSpacereturned was passed to anImageconstructor, or was parsed from encoded data.ColorSpacereturned may be ignored whenImageis drawn, depending on the capabilities of theSurfacereceiving the drawing.- Returns:
ColorSpaceinImage, or nullptr, wrapped in a smart pointer
- Image.refEncodedData(self: skia.Image) skia.Data
Returns encoded
Imagepixels asData, ifImagewas created from supported encoded stream format.Platform support for formats vary and may require building with one or more of: SK_ENCODE_JPEG, SK_ENCODE_PNG, SK_ENCODE_WEBP.
Returns nullptr if
Imagecontents are not encoded.- Returns:
encoded
Image, or nullptr
- Image.reinterpretColorSpace(self: skia.Image, newColorSpace: skia.ColorSpace = None) skia.Image
Creates a new
Imageidentical to this one, but with a differentColorSpace.This does not convert the underlying pixel data, so the resulting image will draw differently.
- Image.resize(self: skia.Image, width: int, height: int, options: skia.SamplingOptions = skia.SamplingOptions(), cachingHint: skia.Image.CachingHint = skia.Image.CachingHint.kAllow_CachingHint) skia.Image
Creates
Imageby scaling pixels to fit width and height. Raises ifImagecould not be scaled.Scales the image, with filterQuality, to match width and height. filterQuality
None_FilterQualityis fastest, typically implemented with nearest neighbor filter.kLow_FilterQualityis typically implemented with bilerp filter.kMedium_FilterQualityis typically implemented with bilerp filter, and mip-map filter when size is reduced.kHigh_FilterQualityis slowest, typically implemented with bicubic filter.If cachingHint is
kAllow_CachingHint, pixels may be retained locally. If cachingHint iskDisallow_CachingHint, pixels are not added to the local cache.- Parameters:
width (int) – target width
height (int) – target height
options (skia.SamplingOptions) – sampling options
cachingHint (skia.Image.CachingHint) – Caching hint
- Returns:
- Image.save(self: skia.Image, fp: object, encodedImageFormat: skia.EncodedImageFormat = skia.EncodedImageFormat.kPNG, quality: int = 100) None
Saves
Imageto file path or file-like object.Shortcut for the following:
data = image.encodeToData(encodedImageFormat, quality) if hasattr(fp, 'write'): fp.write(data) else: with open(fp, 'wb') as f: f.write(data)
- Parameters:
fp – file path or file-like object that has write method. file must be opened in writable binary mode.
encodedImageFormat (skia.EncodedImageFormat) – one of:
kJPEG,kPNG,kWEBPquality (int) – encoder specific metric with 100 equaling best
- Image.scalePixels(self: skia.Image, dst: SkPixmap, samplingOptions: skia.SamplingOptions = skia.SamplingOptions(), cachingHint: skia.Image.CachingHint = skia.Image.CachingHint.kAllow_CachingHint) bool
Copies
Imageto dst, scaling pixels to fitdst.width()anddst.height(), and converting pixels to matchdst.colorType()anddst.alphaType().Returns true if pixels are copied. Returns false if
dst.addr()is nullptr, ordst.rowBytes()is less than dstImageInfo.minRowBytes().Pixels are copied only if pixel conversion is possible. If
ImageColorTypeiskGray_8_ColorType, orkAlpha_8_ColorType;dst.colorType()must match. IfImageColorTypeiskGray_8_ColorType,dst.colorSpace()must match. IfImageAlphaTypeiskOpaque_AlphaType,dst.alphaType()must match. IfImageColorSpaceis nullptr,dst.colorSpace()must match. Returns false if pixel conversion is not possible.Scales the image, with filterQuality, to match
dst.width()anddst.height(). filterQualityNone_FilterQualityis fastest, typically implemented with nearest neighbor filter.kLow_FilterQualityis typically implemented with bilerp filter.kMedium_FilterQualityis typically implemented with bilerp filter, and mip-map filter when size is reduced.kHigh_FilterQualityis slowest, typically implemented with bicubic filter.If cachingHint is
kAllow_CachingHint, pixels may be retained locally. If cachingHint iskDisallow_CachingHint, pixels are not added to the local cache.- Parameters:
dst (skia.Pixmap) – destination
Pixmap:ImageInfo, pixels, row bytesfilterQuality (skia.FilterQuality) – Filter quality
cachingHint (skia.Image.CachingHint) – Caching hint
- Returns:
true if pixels are scaled to fit dst
- Image.toarray(self: skia.Image, srcX: int = 0, srcY: int = 0, colorType: skia.ColorType = skia.ColorType.kUnknown_ColorType, alphaType: skia.AlphaType = skia.AlphaType.kUnpremul_AlphaType, colorSpace: skia.ColorSpace = None) numpy.ndarray
Exports a
numpy.ndarray.- Parameters:
srcX – offset into readable pixels on x-axis; may be negative
srcY – offset into readable pixels on y-axis; may be negative
colorType – target
ColorTypealphaType – target
AlphaTypecolorSpace – target
ColorSpace
- Returns:
numpy.ndarray
- Image.tobytes(self: skia.Image) object
Creates python bytes object from internal pixels.
When the image is raster, the returned bytes share the internal buffer. Otherwise, pixels are copied to a newly allocated python bytes.
- Image.unique(self: skia.RefCntBase) bool
May return true if the caller is the only owner.
Ensures that all previous owner’s actions are complete.
- Image.uniqueID(self: skia.Image) int
Returns value unique to image.
Imagecontents cannot change afterImageis created. Any operation to create a newImagewill receive generate a new unique number.- Returns:
unique identifier
- Image.unref(self: skia.RefCntBase) None
Decrement the reference count.
If the reference count is 1 before the decrement, then delete the object. Note that if this is the case, then the object needs to have been allocated via new, and not on the stack.
- Image.width(self: skia.Image) int
Returns pixel count in each row.
- Returns:
pixel width in
Image
- Image.withDefaultMipmaps(self: skia.Image) skia.Image
Returns an image with the same “base” pixels as the this image, but with mipmap levels automatically generated and attached.
Attributes
- Image.kAllow_CachingHint = <CachingHint.kAllow_CachingHint: 0>
- Image.kBC1_RGB8_UNORM = <CompressionType.kBC1_RGB8_UNORM: 2>
- Image.kBC1_RGBA8_UNORM = <CompressionType.kBC1_RGBA8_UNORM: 3>
- Image.kDisallow_CachingHint = <CachingHint.kDisallow_CachingHint: 1>
- Image.kETC2_RGB8_UNORM = <CompressionType.kETC2_RGB8_UNORM: 1>
- Image.kF16 = <BitDepth.kF16: 1>
- Image.kLast = <CompressionType.kBC1_RGBA8_UNORM: 3>
- Image.kNone = <CompressionType.kNone: 0>
- Image.kRO_LegacyBitmapMode = <LegacyBitmapMode.kRO_LegacyBitmapMode: 0>
- Image.kU8 = <BitDepth.kU8: 0>