A Rust macro crate for converting text and images to various image formats, primarily for use with embedded graphics systems.
- Convert text to grayscale images with customizable options
- Convert color images to monochrome (1-bit) images
- Convert color images to 4-color (2-bit) images
- Convert images to grayscale with adjustable bit depth (1, 2, 4, or 8-bit)
Add this to your Cargo.toml
:
[dependencies]
text-image = "0.1.0"
Convert text to a grayscale image:
use text_image::text_image;
use embedded_graphics::{image::ImageRaw, pixelcolor::Gray8};
fn main() {
let (w, h, raw) = text_image!(
text = "Hello, world!哈哈这样也行",
font = "LXGWWenKaiScreen.ttf",
font_size = 48.0,
inverse,
Gray4,
);
let raw_image = ImageRaw::<Gray8>::new(raw, w);
}
Convert a color image to a 1-bit monochrome image:
use text_image::monochrome_image;
let (w, h, img_raw) = monochrome_image!("path/to/image.png", channel = 1);
Convert a color image to a 2-bit 4-color image:
use text_image::quadcolor_image;
let (w, h, img_raw) = quadcolor_image!("path/to/image.png");
Convert an image to grayscale with specified bit depth:
use text_image::gray_image;
let (w, h, img_raw) = gray_image!("path/to/image.png", Gray4);
text
: The text to convert (required fortext_image!
)font
: Path to the font file (required fortext_image!
)font_size
: Font size in pixels (default: 16.0)inverse
: Invert the colors (optional)line_spacing
: Additional space between lines (optional)Gray2
,Gray4
,Gray8
: Specify the bit depth for grayscale output
Contributions are welcome! Please feel free to submit a Pull Request.