Last active
May 2, 2023 09:00
-
-
Save affanshahid/a88c781d6edc47c2dcc9cb89b1901488 to your computer and use it in GitHub Desktop.
Overlay indices on a spritesheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::{env, path::Path}; | |
| use image::{open, Rgb}; | |
| use imageproc::drawing::draw_text_mut; | |
| use rusttype::{Font, Scale}; | |
| fn main() { | |
| let arg = env::args().nth(1).expect("Please provide a valid path"); | |
| let sprite_width: i32 = env::args() | |
| .nth(2) | |
| .map(|s| s.parse().ok()) | |
| .flatten() | |
| .expect("Please provide a valid sprite width"); | |
| let sprite_height: i32 = env::args() | |
| .nth(3) | |
| .map(|s| s.parse().ok()) | |
| .flatten() | |
| .expect("Please provide a valid sprite height"); | |
| let path = Path::new(&arg); | |
| let font = Vec::from(include_bytes!("../../assets/CascadiaMono.ttf") as &[u8]); | |
| let font = Font::try_from_vec(font).unwrap(); | |
| let mut image = open(path) | |
| .expect(&format!("Could not load image at {:?}", path)) | |
| .to_rgb8(); | |
| let (width, height) = image.dimensions(); | |
| let mut x: i32; | |
| let mut y: i32 = 0; | |
| let mut index = 0; | |
| while y < height as i32 { | |
| x = 0; | |
| while x < width as i32 { | |
| draw_text_mut( | |
| &mut image, | |
| Rgb([255u8, 255u8, 255u8]), | |
| x, | |
| y, | |
| Scale { | |
| x: (sprite_width as f32 / 1.8), | |
| y: (sprite_height as f32 / 1.8), | |
| }, | |
| &font, | |
| &index.to_string(), | |
| ); | |
| x += sprite_width; | |
| index += 1; | |
| } | |
| y += sprite_height; | |
| } | |
| let output_path = path | |
| .parent() | |
| .unwrap() | |
| .parent() | |
| .unwrap() | |
| .join("indices") | |
| .join(format!( | |
| "{}-index.{}", | |
| path.file_stem().unwrap().to_str().unwrap(), | |
| path.extension().unwrap().to_str().unwrap() | |
| )); | |
| image.save(output_path).unwrap(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment