add white parameter to mark as spoiler

This commit is contained in:
Guillermo Roche 2023-11-12 20:00:33 +01:00
parent dcf0623c4b
commit 61d7a09e01
Signed by: groche97
GPG Key ID: 041FB85BEEA4B9B0

View File

@ -10,21 +10,29 @@ use image::{DynamicImage,GenericImageView};
use std::io::Cursor;
use tokio::io::BufStream;
const TOLERANCE: i16 = 0;
const TOLERANCE: i16 = 10;
const PERCENT_ACEPTANCE: u8 = 5;
const WHITE : u8 = 160;
const WHITE_ACEPTANCE: u8 = 50;
fn check_percent(img: DynamicImage) -> u8 {
fn check_percent(img: DynamicImage) -> (u8,u8) {
let mut cont = 0;
let mut cont_wite = 0;
for pixel in img.pixels() {
let diference_rg:i16 = (pixel.2[0] as i16 - pixel.2[1] as i16).abs();
let diference_gb:i16 = (pixel.2[1] as i16 - pixel.2[2] as i16).abs();
let diference_br:i16 = (pixel.2[0] as i16 - pixel.2[2] as i16).abs();
let diference_pixel = diference_rg + diference_gb + diference_br;
if pixel.2[0] > WHITE && pixel.2[1] > WHITE && pixel.2[2] > WHITE {
cont_wite+=1
}
if diference_pixel > TOLERANCE {
cont += 1;
}
}
((cont as f64 /(img.width() * img.height()) as f64)*100.0) as u8
let percent = ((cont as f64 /(img.width() * img.height()) as f64)*100.0) as u8;
let white = ((cont_wite as f64 /(img.width() * img.height()) as f64)*100.0) as u8;
(percent,white)
}
pub fn append_text(new_msg: MultipartRequest<SendPhoto>, old_msg: Message) -> MultipartRequest<SendPhoto> {
@ -63,6 +71,7 @@ pub async fn check_image(msg: Message, bot: Bot) -> anyhow::Result<()> {
match msg.photo() {
Some(s) => {
let mut percent = 0;
let mut white = 0;
let mut id : Option<(String, u32)> = None;
let mut failed = true;
for p in s {
@ -76,8 +85,9 @@ pub async fn check_image(msg: Message, bot: Bot) -> anyhow::Result<()> {
Ok(img) => {
failed = false;
let img_percent = check_percent(img);
if img_percent >= percent {
percent = img_percent;
if img_percent.0 >= percent {
percent = img_percent.0;
white = img_percent.1;
id = match id {
Some(i) => {
if i.1 > p.width {
@ -93,7 +103,7 @@ pub async fn check_image(msg: Message, bot: Bot) -> anyhow::Result<()> {
Err(_e) => continue,
}
}
if !failed && percent < PERCENT_ACEPTANCE {
if !failed && percent < PERCENT_ACEPTANCE && white > WHITE_ACEPTANCE {
bot.delete_message(msg.chat.id, msg.id).await?;
let response = match id {
Some(i) => bot.send_photo(msg.chat.id, teloxide::types::InputFile::file_id(i.0)).has_spoiler(true),