Last active
December 22, 2021 04:03
-
-
Save 19wintersp/32f50900a4971cae44a45e88e844c588 to your computer and use it in GitHub Desktop.
Code to generate an image which will show differently when expanded on Discord. (standard dark theme) Bad code, doesn't really deserve a repo of its own. Available in website form at https://discordimage.19wintersp.repl.co/
This file contains 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
// depends on https://crates.io/crates/image ^0.23 | |
use std::env::args; | |
use std::process::exit; | |
use image::{ GenericImageView, GrayImage, ImageResult, RgbaImage }; | |
use image::imageops::FilterType; | |
const BRIGHTNESS_THRESHOLD: u8 = 64; | |
const COLOUR_A: [u8; 4] = [ 8, 8, 9, 255 ]; | |
const COLOUR_B: [u8; 4] = [ 54, 57, 63, 255 ]; | |
const MAXIMUM_SIZE: u32 = 256; | |
fn main() -> ImageResult<()> { | |
let args: Vec<_> = args().collect(); | |
if args.len() < 3 { | |
eprintln!("Usage: {} <image_a> <image_b> [output]", args[0]); | |
exit(1); | |
} | |
let mut image_a = image::open(&args[1])?.grayscale(); | |
let mut image_b = image::open(&args[2])?.grayscale(); | |
if image_a.width() > MAXIMUM_SIZE || image_a.height() > MAXIMUM_SIZE { | |
image_a = image_a.resize(MAXIMUM_SIZE, MAXIMUM_SIZE, FilterType::Nearest); | |
} | |
if image_b.width() > MAXIMUM_SIZE || image_b.height() > MAXIMUM_SIZE { | |
image_b = image_b.resize(MAXIMUM_SIZE, MAXIMUM_SIZE, FilterType::Nearest); | |
} | |
let image_a = image_a.to_luma8(); | |
let image_b = image_b.to_luma8(); | |
let width = image_a.width().max(image_b.width()); | |
let height = image_a.height().max(image_b.height()); | |
let mut output = RgbaImage::new(width, height); | |
for x in 0..width { | |
for y in 0..height { | |
let i = x % 2 == y % 2; | |
let a = get_pixel(&image_a, x, y) < BRIGHTNESS_THRESHOLD && i; | |
let b = get_pixel(&image_b, x, y) < BRIGHTNESS_THRESHOLD && !i; | |
let pixel = if a { | |
COLOUR_A | |
} else if b { | |
COLOUR_B | |
} else { | |
[ 0, 0, 0, 0 ] | |
}; | |
output.put_pixel(x, y, pixel.into()); | |
} | |
} | |
output.save(if args.len() >= 4 { | |
&args[3] | |
} else { | |
"output.png" | |
})?; | |
Ok(()) | |
} | |
fn get_pixel(image: &GrayImage, x: u32, y: u32) -> u8 { | |
if x >= image.width() || y >= image.height() { | |
255 | |
} else { | |
image.get_pixel(x, y).0[0] | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment