我正在尝试循环遍历图像目录,将不透明度编辑为 50%,然后保存它们。我有一组 jpg 和 png 图像。我正在使用图像箱。
可能我必须对图像的格式进行一些考虑,但我不知道是什么......
use clap::Parser;
use std::{process, thread};
#[derive(Parser,Debug)]
#[command(author,version,about,long_about=None)]
struct Args {
#[arg(long,value_hint=clap::ValueHint::DirPath, default_value="./")]
picture_dir: std::path::PathBuf,
#[arg(long, default_value="300")]
draw_time: u64,
#[arg(long, default_value="3")]
start_delay: u64,
#[arg(long)]
dry_run: bool,
}
fn main() {
let args = Args::parse();
println!("{}", args.picture_dir.as_os_str().to_str().unwrap());
let dir = args.picture_dir.as_os_str().to_str().unwrap();
let dir = format!("{}/*.*", dir);
let extensions_supported = vec!("png","jpg","bmp");
for path in glob::glob(&dir).expect("Failed to list directory") {
let path = path.unwrap();
let copy_from_path = path.clone();
let file_name = path.file_name().unwrap();
let mut cachepath = std::env::temp_dir();
cachepath.push(file_name.to_str().unwrap());
let str_path = cachepath.to_str().unwrap();
let path_extension = path.extension().unwrap().to_str().unwrap();
if !extensions_supported.contains(&path_extension) {
println!("We don't support that file type");
continue;
}
println!("{}", str_path);
let start = std::time::Instant::now();
// We should open the program
let cmd_str = format!("cmd.exe /C clipstudiopaint.exe {}", str_path);
let mut child: Option<process::Child> = None;
if args.dry_run {
println!("{}",cmd_str);
} else {
std::fs::copy(copy_from_path,cachepath.clone()).expect("Failed to create temporary file");
let cache_image_path = cachepath.clone();
let cache_image_path = cache_image_path.to_str().unwrap();
let save_image_path = cachepath.clone();
let mut image = image::open(cache_image_path)
.expect("Failed to open image for editing").to_rgba32f();
image.pixels_mut().for_each(|p| p[3] /= 2.0);
image.save(save_image_path.to_str().unwrap()).expect("Failed to save image");
//let mut image_target = image::ImageBuffer::new(image.width(), image.height());
child = Some(process::Command::new("cmd")
.arg("/C")
.arg("clipstudiopaint.exe")
.arg(cachepath.to_str().unwrap())
.spawn()
.expect("Failed to run clipstudiopaint"));
}
// We should wait a couple of seconds for it to load
loop {
if start.elapsed().as_secs() > args.start_delay {
break;
}
}
let start = std::time::Instant::now();
// we should wait the specified drawing time
println!("Starting timer");
loop {
if start.elapsed().as_secs() > args.draw_time {
break;
}
}
// we should kill the process
if !args.dry_run {
child.unwrap().kill().expect("Failed to kill childprocess");
std::fs::remove_file(cachepath).expect("Failed to remove temp file");
}
let start = std::time::Instant::now();
loop {
if start.elapsed().as_secs() > 2 {
break;
}
}
}
}
这会导致错误:Failed to save image: Encoding(EncodingError { format: Exact(Png), underlying: Some(ColorType(Rgba32F)) })
最终答案的相关部分最终如下所示:
std::fs::copy(copy_from_path,cachepath.clone()).expect("Failed to create temporary file");
let image_edit_path = image_edit_path.clone();
let image_edit_path = image_edit_path.to_str().unwrap();
let save_image_path = cachepath.clone();
println!("Opening: {} for editing", image_edit_path);
let mut image = image::open(image_edit_path)
.expect("Failed to open image for editing");
let mut target_image = image::DynamicImage::new_rgba8(image.width(), image.height());
let mut target_image = target_image.to_rgba8();
target_image.pixels_mut().for_each(|p| *p = image::Rgba{0: [246, 244, 237, 255]});
let mut image = image.to_rgba8();
image.pixels_mut().for_each(|p| p[3] = 100);
image::imageops::overlay(&mut target_image, &image, 0, 0);
let target_image = image::DynamicImage::ImageRgba8(target_image);
target_image.save(save_image_path.to_str().unwrap()).expect("Failed to save image");
并非所有编码器都支持所有颜色格式。
由于您使用显式转换它
to_rgba32f()
,因此您同样必须将其转换回您正在使用的编码器支持的格式。对于 PNG,请使用例如to_rgba8()
或into_rgba8()
。对于 JPEG,如果您想要透明度,您还必须将它们另存为(例如)PNG。
否则,当您尝试保存它时,您同样会收到编码错误,因为 JPEG 不支持 Alpha 通道。(至少
image
板条箱编码器没有。)说得完全迂腐一点,JPEG 格式本身就支持“alpha”通道。
image
但这没有被使用,也没有得到广泛支持,而且crate也不支持它。请参阅Q2639866了解更多信息。