Skip to content

Commit

Permalink
bugfix and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
functorism committed Oct 9, 2023
1 parent 4cf96a2 commit 06b66ee
Show file tree
Hide file tree
Showing 29 changed files with 63 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "snapcrop"
description = "CLI for crop/resize of large amounts of images with configurable resolutions"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
license = "MIT"

Expand Down
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,50 @@ Options:
Print help (see a summary with '-h')
```

## Visual examples

```
snapcrop squares --res 256
```
![](./examples/squares/4de26fac0cc3162259bf785a64e836fb88e5550082ff2233d8f58de5263facf6.png)
![](./examples/squares/50a5a51eb1d57307579b86508196fbb44fb770e7cd170257f573b3260b0b8feb.png)
![](./examples/squares/653d96fb41779dda5ff6cc7a10745d33f4775e1267cd232403854b3656028678.png)
![](./examples/squares/6b6fe23717af8d18ca11788013f0e789d90e40f6f4debff0cd2fcfb725d2a7d4.png)

```
snapcrop landscape --res 256x128
```
![](./examples/landscape/4de26fac0cc3162259bf785a64e836fb88e5550082ff2233d8f58de5263facf6.png)
![](./examples/landscape/50a5a51eb1d57307579b86508196fbb44fb770e7cd170257f573b3260b0b8feb.png)
![](./examples/landscape/653d96fb41779dda5ff6cc7a10745d33f4775e1267cd232403854b3656028678.png)
![](./examples/landscape/6b6fe23717af8d18ca11788013f0e789d90e40f6f4debff0cd2fcfb725d2a7d4.png)


```
snapcrop portrait --res 128x256
```
![](./examples/portrait/4de26fac0cc3162259bf785a64e836fb88e5550082ff2233d8f58de5263facf6.png)
![](./examples/portrait/50a5a51eb1d57307579b86508196fbb44fb770e7cd170257f573b3260b0b8feb.png)
![](./examples/portrait/653d96fb41779dda5ff6cc7a10745d33f4775e1267cd232403854b3656028678.png)
![](./examples/portrait/6b6fe23717af8d18ca11788013f0e789d90e40f6f4debff0cd2fcfb725d2a7d4.png)


```
snapcrop mixed --res 256,[128x256]
```
![](./examples/mixed/4de26fac0cc3162259bf785a64e836fb88e5550082ff2233d8f58de5263facf6.png)
![](./examples/mixed/50a5a51eb1d57307579b86508196fbb44fb770e7cd170257f573b3260b0b8feb.png)
![](./examples/mixed/653d96fb41779dda5ff6cc7a10745d33f4775e1267cd232403854b3656028678.png)
![](./examples/mixed/6b6fe23717af8d18ca11788013f0e789d90e40f6f4debff0cd2fcfb725d2a7d4.png)

```
snapcrop steps --res [128:256:8x128:256:8]
```
![](./examples/steps/4de26fac0cc3162259bf785a64e836fb88e5550082ff2233d8f58de5263facf6.png)
![](./examples/steps/50a5a51eb1d57307579b86508196fbb44fb770e7cd170257f573b3260b0b8feb.png)
![](./examples/steps/653d96fb41779dda5ff6cc7a10745d33f4775e1267cd232403854b3656028678.png)
![](./examples/steps/6b6fe23717af8d18ca11788013f0e789d90e40f6f4debff0cd2fcfb725d2a7d4.png)

## Dependencies
The main libraries this CLI uses are:

Expand All @@ -90,4 +134,4 @@ The main libraries this CLI uses are:
- rayon: For parallel processing.
- clap: For command-line argument parsing.
- simplelog: For logging.
- blake3: For hashing the image content.
- blake3: For hashing the image content.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions examples/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#! /usr/bin/env bash

rm -f {squares,landscape,portrait,mixed,steps}/*

find source -type f | ../target/release/snapcrop squares --res 256
find source -type f | ../target/release/snapcrop landscape --res 256x128
find source -type f | ../target/release/snapcrop portrait --res 128x256
find source -type f | ../target/release/snapcrop mixed --res "256,[128x256]"
find source -type f | ../target/release/snapcrop steps --res "[128:256:8x128:256:8]"
Binary file added examples/source/fruit-bowl-square.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/source/mieming-panorama-landscape.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/source/stockholm-landscape.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/source/stockholm-portrait.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ fn resize_and_crop(src_view: fr::DynamicImageView, res: Vec<(u32, u32)>) -> Resu
})
.ok_or_else(|| anyhow!("Could not find a valid resolution target"))?;

println!("{}x{} -> {}x{}", img_w, img_h, valid_w, valid_h);

if img_w < valid_w || img_h < valid_h {
return Err(anyhow!(
"Image too small, skipping: {}x{} < {}x{}",
Expand All @@ -179,11 +181,12 @@ fn resize_and_crop(src_view: fr::DynamicImageView, res: Vec<(u32, u32)>) -> Resu
));
}

// Determine the resize dimensions
let (resize_w, resize_h) = if img_ratio > valid_w as f64 / valid_h as f64 {
(valid_w, (valid_w as f64 / img_ratio).round() as u32)
// If the image is more "landscape" than the target, match its height to the target height
((img_w as f64 * valid_h as f64 / img_h as f64).round() as u32, valid_h)
} else {
((valid_h as f64 * img_ratio).round() as u32, valid_h)
// If the image is more "portrait" or equal to the target, match its width to the target width
(valid_w, (valid_w as f64 * img_h as f64 / img_w as f64).round() as u32)
};

// Resize the image while maintaining its original aspect ratio
Expand Down Expand Up @@ -283,6 +286,7 @@ fn main() -> Result<()> {

res.sort();
res.dedup();
res.reverse();

// Ensure res is not empty and no dimension is 0
if res.is_empty() || res.iter().any(|&(w, h)| w == 0 || h == 0) {
Expand Down

0 comments on commit 06b66ee

Please sign in to comment.