Export image with image crate

This commit is contained in:
Piyush मिश्रः 2022-02-28 19:38:46 +05:30
parent ad1aaae2a0
commit 7f67d5ad29
3 changed files with 10 additions and 57 deletions

51
Cargo.lock generated
View File

@ -29,12 +29,6 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "anyhow"
version = "1.0.55"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "159bb86af3a200e19a068f4224eae4c8bb2d0fa054c7e5d1cacd5cef95e684cd"
[[package]] [[package]]
name = "atty" name = "atty"
version = "0.2.14" version = "0.2.14"
@ -632,7 +626,7 @@ dependencies = [
[[package]] [[package]]
name = "post_maker" name = "post_maker"
version = "0.2.5" version = "0.2.6"
dependencies = [ dependencies = [
"clap", "clap",
"dirs", "dirs",
@ -647,7 +641,6 @@ dependencies = [
"serde_json", "serde_json",
"simplelog", "simplelog",
"textwrap", "textwrap",
"turbojpeg",
"webbrowser", "webbrowser",
] ]
@ -939,26 +932,6 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "thiserror"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "tiff" name = "tiff"
version = "0.6.1" version = "0.6.1"
@ -986,28 +959,6 @@ version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e5d7cd7ab3e47dda6e56542f4bbf3824c15234958c6e1bd6aaa347e93499fdc" checksum = "3e5d7cd7ab3e47dda6e56542f4bbf3824c15234958c6e1bd6aaa347e93499fdc"
[[package]]
name = "turbojpeg"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "111671fdf720903270c8c19fd641048793a4f8f69abc184e4e7ea9228619c65e"
dependencies = [
"image",
"libc",
"thiserror",
"turbojpeg-sys",
]
[[package]]
name = "turbojpeg-sys"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03ee42589a950ef4f0f03752c3878676ffe6ab5a468132784ba483b0efc257e6"
dependencies = [
"anyhow",
"libc",
]
[[package]] [[package]]
name = "unicode-linebreak" name = "unicode-linebreak"
version = "0.1.2" version = "0.1.2"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "post_maker" name = "post_maker"
version = "0.2.5" version = "0.2.6"
edition = "2021" edition = "2021"
description = "Post Maker helps you to make post for Instagram and other Social Media apps easily." description = "Post Maker helps you to make post for Instagram and other Social Media apps easily."
authors = ["PiyushXCoder <https://piyushxcoder.in>"] authors = ["PiyushXCoder <https://piyushxcoder.in>"]
@ -17,7 +17,6 @@ simplelog = "0.11"
fltk = "1.2" fltk = "1.2"
fltk-theme = "0.4" fltk-theme = "0.4"
image = "0.23" image = "0.23"
turbojpeg = { version = "^0.2", features = ["image"] }
imageproc = "0.22" imageproc = "0.22"
rusttype = "0.9" rusttype = "0.9"
serde_json = "1.0" serde_json = "1.0"

View File

@ -13,7 +13,7 @@
*/ */
use std::{ use std::{
fs, fs::{self, File},
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::{Arc, RwLock}, sync::{Arc, RwLock},
}; };
@ -236,16 +236,19 @@ impl ImageContainer {
prop.original_dimension.1, prop.original_dimension.1,
); );
let comp = match turbojpeg::compress_image(&img.into_rgb8(), 95, turbojpeg::Subsamp::None) { let mut output = match File::create(&export) {
Ok(a) => a, Ok(a) => a,
Err(e) => { Err(e) => {
dialog::alert_default("Failed to compress jpeg!"); dialog::alert_default("Failed to write to disk!");
warn!("Failed to compress jpeg!\n{:?}", e); warn!("Failed to write to disk!\n{:?}", e);
return; return;
} }
}; };
if let Err(e) = std::fs::write(&export, comp) { let mut encoder = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut output, 100);
encoder.set_pixel_density(image::codecs::jpeg::PixelDensity::dpi(300));
if let Err(e) = encoder.encode_image(&img) {
dialog::alert_default("Failed to export Image!"); dialog::alert_default("Failed to export Image!");
warn!("Failed to export Image!\n{:?}", e); warn!("Failed to export Image!\n{:?}", e);
} }