Images
The first step with SkImage will be to load and save images. That's what we'll see in this section.
Loading or creating an image
In order to get an image, you can:
- load an image from a file (such as a
png
orjpg
file) - load an image from a URL
- create an image from scratch (with a specific size and type)
Info
You can specify the type of the image as an int (by adding with type X
at the end). The specified type will be used to create the encoding of the image:
0
forTYPE_INT_RGB
(such as ajpg
image, no transparency)1
forTYPE_INT_ARGB
(such as apng
image, with transparency)2
forTYPE_INT_ARGB_PRE
(such as apng
image, with transparency and pre-multiplied alpha)
And more are available at the JavaDoc.
Saving an image
The only way to save an image is to use the save
effect. It will save the image in the specified file path.
Info
- If the desired file path doesn't exist, it will be created.
- If the file already exists, it will be overwritten.
Manipulating images
SkImage2 provides several ways to manipulate images, including resizing, cropping, and applying effects like rounded corners.
Resizing images
You can resize an image to a new width and height using the resized
expression:
By default, this uses the default scaling algorithm. You can specify a different algorithm for better quality:
Available algorithms include:
default
- Basic scalingfast
- Optimized for speed but lower qualitysmooth
- Higher quality but slower processingreplicate
- Pixel replication (nearest neighbor)
Cropping images
You can crop an image to a specified width and height, starting from the top-left corner:
This will create a new image with only the top-left 100x100 pixels of the original image.
Rounded corners
You can create a rounded version of an image:
# Create a rounded image with default corner radius
set {_rounded} to rounded {_image}
# Create a rounded image with specific corner radius (e.g., 20 pixels)
set {_rounded} to rounded {_image} with size 20
Finding average color
You can determine the average (or main) color of an image:
This is useful for generating color schemes or determining the dominant color in an image.
Image properties
You can retrieve various properties of an image: