You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create an image, set pixel values, save it to a PNG.
import'dart:io';
import'package:image/image.dart'as img;
voidmain() async {
// Create a 256x256 8-bit (default) rgb (default) image.final image = img.Image(width:256, height:256);
// Iterate over its pixelsfor (var pixel in image) {
// Set the pixels red value to its x position value, creating a gradient.
pixel..r = pixel.x
// Set the pixels green value to its y position value.
..g = pixel.y;
}
// Encode the resulting image to the PNG image format.final png = img.encodePng(image);
// Write the PNG formatted data to a file.awaitFile('image.png').writeAsBytes(png);
}
To asynchronously load an image file, resize it, and save it as a thumbnail:
import'package:image/image.dart'as img;
voidmain(List<String> args) async {
final path = args.isNotEmpty ? args[0] :'test.png';
final cmd = img.Command()
// Decode the image file at the given path
..decodeImageFile(path)
// Resize the image to a width of 64 pixels and a height that maintains the aspect ratio of the original.
..copyResize(width:64)
// Write the image to a PNG file (determined by the suffix of the file path).
..writeToFile('thumbnail.png');
// On platforms that support Isolates, execute the image commands asynchronously on an isolate thread.// Otherwise, the commands will be executed synchronously.await cmd.executeThread();
}
About
Dart Image Library for opening, manipulating, and saving various different image file formats.