Unleashing the Power of Images with the Dart Image Library
Published on by Flutter News Hub
The Dart Image Library empowers developers to effortlessly load, save, and manipulate images in a wide range of formats, including JPG, PNG, GIF, and many more. Compatible with both Dart:io and Dart:html, this versatile library can be harnessed across command-line, Flutter, and web applications.
Supported Image Formats
The Dart Image Library offers comprehensive support for a diverse range of image formats, catering to various use cases:
- Read/Write: JPG, PNG (including animated APNG), GIF (animated or not), BMP, TIFF, TGA, PVR, ICO
- Read Only: WebP (animated or not), PSD, EXR
- Write Only: CUR
Functionality
Beyond its core image loading and saving capabilities, the library offers an array of image manipulation filters that enable developers to:
- Resize and crop images
- Apply color adjustments
- Perform rotations and flips
- Enhance images with sharpening, blurring, and noise reduction
- Convert images to grayscale or sepia
Examples
Creating and Saving an Image:
import 'dart:io'; import 'package:image/image.dart' as img; void main() async { // Create a 256x256 RGB image final image = img.Image(width: 256, height: 256); // Set pixel values to create a gradient for (var pixel in image) { pixel.r = pixel.x; pixel.g = pixel.y; } // Encode and save the image as a PNG file final png = img.encodePng(image); await File('image.png').writeAsBytes(png); }
Loading, Resizing, and Saving an Image:
import 'package:image/image.dart' as img; void main(List args) async { final path = args.isNotEmpty ? args[0] : 'test.png'; final cmd = img.Command() ..decodeImageFile(path) ..copyResize(width: 64) ..writeToFile('thumbnail.png'); await cmd.executeThread(); // Execute asynchronously on platforms that support Isolates }
Conclusion
The Dart Image Library provides a comprehensive suite of tools for image manipulation, empowering developers to handle images effectively in their applications. With its extensive format support and powerful filters, the library simplifies image processing tasks, enhancing the visual capabilities of your projects.