Skip to content

Instantly share code, notes, and snippets.

@Cerber-Ursi
Last active May 18, 2025 06:53
Show Gist options
  • Save Cerber-Ursi/3c07e2855b194946e2bb7ed4c890bca0 to your computer and use it in GitHub Desktop.
Save Cerber-Ursi/3c07e2855b194946e2bb7ed4c890bca0 to your computer and use it in GitHub Desktop.
Plotters image artifacts
[package]
name = "plotters_artifacts_test"
version = "0.1.0"
edition = "2021"
[dependencies]
image = "0.25.6"
plotters = "0.3.7"
plotters-bitmap = "0.3.7"
use image::{DynamicImage, RgbaImage};
use plotters::prelude::*;
use plotters_bitmap::BitMapBackend;
const WIDTH: u32 = 100;
const HEIGHT: u32 = 100;
fn main() {
// Create an RGBA buffer
let mut buffer = vec![0u8; (WIDTH * HEIGHT * 4) as usize];
// Use Plotters to draw something on the buffer
fill_buffer(&mut buffer);
// Write the resulting image, again, based on RGBA buffer
DynamicImage::ImageRgba8(RgbaImage::from_raw(WIDTH, HEIGHT, buffer).unwrap())
.save("test.png")
.unwrap();
}
fn fill_buffer(buffer: &mut Vec<u8>) {
// Create a backend based on RGB buffer
let backend = BitMapBackend::with_buffer(buffer, (WIDTH, HEIGHT));
let root = backend.into_drawing_area();
// Clear the background
root.fill(&WHITE).unwrap();
// Fill the left half of the area
root.split_evenly((1, 2))[0].fill(&BLACK).unwrap();
// Finish drawing
root.present().unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment