Added mask display

This commit is contained in:
2025-05-15 03:21:51 -05:00
parent 5e3bc7a6d8
commit e0b86dbc39
4 changed files with 309 additions and 200 deletions

View File

@ -1,13 +1,13 @@
extern crate lastfm;
extern crate dotenvy;
extern crate lastfm;
use serenity::async_trait;
use serenity::builder::{CreateAttachment, CreateMessage};
use serenity::model::channel::Message;
use serenity::prelude::*;
use serenity::builder::{CreateAttachment, CreateMessage};
extern crate magick_rust;
use magick_rust::{magick_wand_genesis, MagickWand, PixelWand, CompositeOperator, ColorspaceType};
use magick_rust::{ColorspaceType, CompositeOperator, MagickWand, PixelWand, magick_wand_genesis};
use std::env;
use std::sync::Once;
@ -17,112 +17,165 @@ static START: Once = Once::new();
struct Handler;
enum Reply {
Image(Vec<u8>),
Text(String)
Image(Vec<u8>),
Text(String),
}
macro_rules! handle_magick_result {
($a: expr, $b: literal) => {
match $a {
Ok(_) => {},
Err(e) => return Reply::Text(format!("Error: {} {}", $b, e)),
}
}
($a: expr, $b: literal) => {
match $a {
Ok(_) => {}
Err(e) => return Reply::Text(format!("Error: {} {}", $b, e)),
}
};
}
macro_rules! handle_magick_option {
($a: expr, $b: literal) => {
match $a {
Some(res) => res,
None => return Reply::Text($b.to_string()),
}
}
($a: expr, $b: literal) => {
match $a {
Some(res) => res,
None => return Reply::Text($b.to_string()),
}
};
}
#[allow(unused)]
async fn fmi(_arg: &str) -> Reply {
let client = lastfm::Client::<String, &str>::from_env("seoxi");
let now_playing = match client.now_playing().await {
Ok(np) => np,
Err(e) => return Reply::Text(format!("Error: grabbing last.fm user data failed {e}")),
};
if let Some(track) = now_playing {
let image_uri = match track.image.extralarge {
Some(iu) => iu,
None => return Reply::Text("Error: getting image uri failed".to_string()),
};
let mut base_color = PixelWand::new();
#[allow(unused_mut)]
let mut main_wand = MagickWand::new();
#[allow(unused_mut)]
let mut art_wand = MagickWand::new();
//#[allow(unused_mut)]
let mut art_wand_cluster = MagickWand::new();
let client = lastfm::Client::<String, &str>::from_env("seoxi");
let now_playing = match client.now_playing().await {
Ok(np) => np,
Err(e) => return Reply::Text(format!("Error: grabbing last.fm user data failed {e}")),
};
let track = match now_playing {
Some(track) => track,
None => return Reply::Text("Nothing playing.".to_string()),
};
let image_uri = match track.image.extralarge {
Some(iu) => iu,
None => return Reply::Text("Error: getting image uri failed".to_string()),
};
let mut base_color = PixelWand::new();
let mut white = PixelWand::new();
let mut main_wand = MagickWand::new();
let mut art_wand = MagickWand::new();
let mut art_wand_cluster = MagickWand::new();
let mut mask_wand = MagickWand::new();
handle_magick_result!(base_color.set_color("#7f7f7f"), "Failed to set base color");
handle_magick_result!(art_wand.read_image(image_uri.as_str()), "Failed to read image from uri");
handle_magick_result!(art_wand.transform_image_colorspace(ColorspaceType::Lab), "Failed to set art colorspace");
handle_magick_result!(
base_color.set_color("#7f7f7f"),
"Failed to set init base color"
);
handle_magick_result!(
art_wand.read_image(image_uri.as_str()),
"Failed to read image from uri"
);
handle_magick_result!(
art_wand.transform_image_colorspace(ColorspaceType::Lab),
"Failed to set art colorspace"
);
let (art_width, art_height) = (art_wand.get_image_width(), art_wand.get_image_height());
handle_magick_result!(art_wand_cluster.new_image(art_width, art_height, &base_color), "Failed to init cluster image");
handle_magick_result!(art_wand_cluster.compose_images(&art_wand, CompositeOperator::SrcOver, true, 0, 0), "Failed to copy onto cluster image");
// least stupid way I could figure out to do this, since it seems that MagickWand::new_from_wand() doesn't work
handle_magick_result!(art_wand_cluster.set_image_colorspace(ColorspaceType::Lab), "Failed to set cluster colorspace");
handle_magick_result!(art_wand_cluster.kmeans(5, 200, 0.001), "Failed to run kmeans");
let mut colors = handle_magick_option!(art_wand_cluster.get_image_histogram(), "Failed to get color histogram");
println!("{}", colors.len());
colors.sort_by_cached_key(|color| -(color.get_color_count() as isize));
let (art_width, art_height) = (art_wand.get_image_width(), art_wand.get_image_height());
handle_magick_result!(
art_wand_cluster.new_image(art_width, art_height, &base_color),
"Failed to init cluster image"
);
handle_magick_result!(
art_wand_cluster.compose_images(&art_wand, CompositeOperator::SrcOver, true, 0, 0),
"Failed to copy onto cluster image"
);
// least stupid way I could figure out to do this, since it seems that MagickWand::new_from_wand() doesn't work
handle_magick_result!(
art_wand_cluster.set_image_colorspace(ColorspaceType::Lab),
"Failed to set cluster colorspace"
);
handle_magick_result!(
art_wand_cluster.kmeans(5, 200, 0.001),
"Failed to run kmeans"
);
let mut colors = handle_magick_option!(
art_wand_cluster.get_image_histogram(),
"Failed to get color histogram"
);
colors.sort_by_cached_key(|color| -(color.get_color_count() as isize));
let mut accent_color = match colors.len() {
1 => &colors[0],
_ => &colors[1],
};
handle_magick_result!(main_wand.new_image(548, 147, &colors[0]), "Failed to create wand");
handle_magick_result!(main_wand.transform_image_colorspace(ColorspaceType::Lab), "Failed to set main colorspace");
handle_magick_result!(art_wand.adaptive_resize_image(124, 124), "Failed to resize art");
handle_magick_result!(art_wand_cluster.adaptive_resize_image(124, 124), "Failed to resize art_cluster");
handle_magick_result!(main_wand.compose_images(&art_wand, CompositeOperator::SrcOver, false, 12, 12), "Failed to combine images");
handle_magick_result!(main_wand.compose_images(&art_wand_cluster, CompositeOperator::SrcOver, false, 160, 12), "Failed to combine images");
Reply::Image(main_wand.write_image_blob("png").unwrap())
} else {
Reply::Text("Nothing playing.".to_string())
}
handle_magick_result!(white.set_color("#ffffff"), "Failed to set init white");
handle_magick_result!(mask_wand.read_image("mask.png"), "Failed to load mask");
handle_magick_result!(
mask_wand.opaque_paint_image(&white, accent_color, 0.0, false),
"Failed to paint mask"
);
handle_magick_result!(
main_wand.new_image(548, 147, &colors[0]),
"Failed to create wand"
);
handle_magick_result!(
main_wand.transform_image_colorspace(ColorspaceType::Lab),
"Failed to set main colorspace"
);
handle_magick_result!(
art_wand.adaptive_resize_image(124, 124),
"Failed to resize art"
);
handle_magick_result!(
art_wand_cluster.adaptive_resize_image(124, 124),
"Failed to resize art_cluster"
);
handle_magick_result!(
main_wand.compose_images(&art_wand, CompositeOperator::SrcOver, false, 12, 12),
"Failed to combine images"
);
handle_magick_result!(main_wand.compose_images(&art_wand_cluster, CompositeOperator::SrcOver, false, 160, 12), "Failed to combine debug images");
handle_magick_result!(
main_wand.compose_images(&mask_wand, CompositeOperator::SrcOver, false, 401, 0),
"Failed to combine mask"
);
Reply::Image(main_wand.write_image_blob("png").unwrap())
}
async fn set(arg: &str) -> Reply {
Reply::Text(format!("set user {}", arg))
Reply::Text(format!("set user {}", arg))
}
#[async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
let mut cmd_iter = msg.content.split(" ");
let cmd = cmd_iter.next().unwrap_or("");
let arg = cmd_iter.next().unwrap_or("");
let resp = match cmd {
".fmi" => Some(fmi(arg).await),
".set" => Some(set(arg).await),
_ => None
};
if let Some(reply) = resp {
let m = match reply {
Reply::Image(img) => {
let file = CreateAttachment::bytes(img, "fmi.png");
CreateMessage::new().add_file(file)
},
Reply::Text(txt) => CreateMessage::new().content(txt)
};
let message = msg.channel_id.send_message(&ctx.http, m).await;
if let Err(why) = message {
println!("Error sending message: {why:?}");
}
}
let mut cmd_iter = msg.content.split(" ");
let cmd = cmd_iter.next().unwrap_or("");
let arg = cmd_iter.next().unwrap_or("");
let resp = match cmd {
".fmi" => Some(fmi(arg).await),
".set" => Some(set(arg).await),
_ => None,
};
if let Some(reply) = resp {
let m = match reply {
Reply::Image(img) => {
let file = CreateAttachment::bytes(img, "fmi.png");
CreateMessage::new().add_file(file)
}
Reply::Text(txt) => CreateMessage::new().content(txt),
};
let message = msg.channel_id.send_message(&ctx.http, m).await;
if let Err(why) = message {
println!("Error sending message: {why:?}");
}
}
}
}
#[tokio::main]
async fn main() {
START.call_once(|| {
dotenvy::dotenv().expect("Failed to load .env");
START.call_once(|| {
dotenvy::dotenv().expect("Failed to load .env");
magick_wand_genesis();
});
// Login with a bot token from the environment
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
// Set gateway intents, which decides what events the bot will be notified about
@ -131,11 +184,15 @@ async fn main() {
| GatewayIntents::MESSAGE_CONTENT;
// Create a new instance of the Client, logging in as a bot.
let mut client =
Client::builder(&token, intents).event_handler(Handler).await.expect("Err creating client");
let mut client = Client::builder(&token, intents)
.event_handler(Handler)
.await
.expect("Err creating client");
// Start listening for events by starting a single shard
if let Err(why) = client.start().await {
println!("Client error: {why:?}");
}
println!("watcat started");
}