add white parameter to mark as spoiler
This commit is contained in:
		
							parent
							
								
									dcf0623c4b
								
							
						
					
					
						commit
						61d7a09e01
					
				| @ -10,21 +10,29 @@ use image::{DynamicImage,GenericImageView}; | |||||||
| use std::io::Cursor; | use std::io::Cursor; | ||||||
| use tokio::io::BufStream; | use tokio::io::BufStream; | ||||||
| 
 | 
 | ||||||
| const TOLERANCE: i16 = 0; | const TOLERANCE: i16 = 10; | ||||||
| const PERCENT_ACEPTANCE: u8 = 5; | 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 = 0; | ||||||
|  |     let mut cont_wite = 0; | ||||||
|     for pixel in img.pixels() { |     for pixel in img.pixels() { | ||||||
|         let diference_rg:i16 = (pixel.2[0] as i16 - pixel.2[1] as i16).abs(); |         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_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_br:i16 = (pixel.2[0] as i16 - pixel.2[2] as i16).abs(); | ||||||
|         let diference_pixel = diference_rg + diference_gb + diference_br; |         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 { |         if diference_pixel > TOLERANCE { | ||||||
|             cont += 1; |             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> { | 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() { |     match msg.photo() { | ||||||
|         Some(s) => { |         Some(s) => { | ||||||
|             let mut percent = 0; |             let mut percent = 0; | ||||||
|  |             let mut white = 0; | ||||||
|             let mut id : Option<(String, u32)> = None; |             let mut id : Option<(String, u32)> = None; | ||||||
|             let mut failed = true; |             let mut failed = true; | ||||||
|             for p in s { |             for p in s { | ||||||
| @ -76,8 +85,9 @@ pub async fn check_image(msg: Message, bot: Bot) -> anyhow::Result<()> { | |||||||
|                     Ok(img) => { |                     Ok(img) => { | ||||||
|                         failed = false; |                         failed = false; | ||||||
|                         let img_percent = check_percent(img); |                         let img_percent = check_percent(img); | ||||||
|                         if img_percent >= percent { |                         if img_percent.0 >= percent { | ||||||
|                             percent = img_percent; |                             percent = img_percent.0; | ||||||
|  |                             white = img_percent.1; | ||||||
|                             id = match id { |                             id = match id { | ||||||
|                                 Some(i) => { |                                 Some(i) => { | ||||||
|                                     if i.1 > p.width { |                                     if i.1 > p.width { | ||||||
| @ -93,7 +103,7 @@ pub async fn check_image(msg: Message, bot: Bot) -> anyhow::Result<()> { | |||||||
|                     Err(_e) => continue, |                     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?; |                 bot.delete_message(msg.chat.id, msg.id).await?; | ||||||
|                 let response = match id { |                 let response = match id { | ||||||
|                     Some(i) => bot.send_photo(msg.chat.id, teloxide::types::InputFile::file_id(i.0)).has_spoiler(true), |                     Some(i) => bot.send_photo(msg.chat.id, teloxide::types::InputFile::file_id(i.0)).has_spoiler(true), | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user