You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/concurrency.md
+86
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,7 @@
4
4
|--------|--------|------------|
5
5
|[Mutate the elements of an array in parallel][ex-rayon-iter-mut]|[![rayon-badge]][rayon]|[![cat-concurrency-badge]][cat-concurrency]|
6
6
|[Sort a vector in parallel][ex-rayon-parallel-sort]|[![rayon-badge]][rayon][![rand-badge]][rand]|[![cat-concurrency-badge]][cat-concurrency]|
7
+
|[Generate jpg thumbnails in parallel][ex-rayon-thumbnails]|[![rayon-badge]][rayon][![glob-badge]][glob][![image-badge]][image]|[![cat-concurrency-badge]][cat-concurrency][![cat-filesystem-badge]][cat-filesystem]|
7
8
|[Spawn a short-lived thread][ex-crossbeam-spawn]|[![crossbeam-badge]][crossbeam]|[![cat-concurrency-badge]][cat-concurrency]|
8
9
|[Draw fractal dispatching work to a thread pool][ex-threadpool-fractal]|[![threadpool-badge]][threadpool][![num-badge]][num][![num_cpus-badge]][num_cpus][![image-badge]][image]|[![cat-concurrency-badge]][cat-concurrency][![cat-science-badge]][cat-science][![cat-rendering-badge]][cat-rendering]|
This example generates thumbnails for all .jpg in the current directory and saves them in a new folder called `thumbnails`.
81
+
82
+
Files are found using [`glob::glob_with`] to match case insensitively on both `.jpg` and `.JPG`. `rayon` is then used to resize images in parallel using [`par_iter`] along with the `make_thumbnail()` helper function which internally uses [`DynamicImage::resize`].
83
+
84
+
```rust,no_run
85
+
# #[macro_use]
86
+
# extern crate error_chain;
87
+
extern crate glob;
88
+
extern crate image;
89
+
extern crate rayon;
90
+
91
+
use std::path::Path;
92
+
use std::fs::{create_dir_all, File};
93
+
94
+
# use error_chain::ChainedError;
95
+
use glob::{glob_with, MatchOptions};
96
+
use image::{FilterType, ImageError};
97
+
use rayon::prelude::*;
98
+
99
+
# error_chain! {
100
+
# foreign_links {
101
+
# Image(ImageError);
102
+
# Io(std::io::Error);
103
+
# Glob(glob::PatternError);
104
+
# }
105
+
# }
106
+
107
+
fn run() -> Result<()> {
108
+
// find all files in current directory that have a .jpg extension
109
+
// use the default MatchOptions so the search is case insensitive
110
+
let options: MatchOptions = Default::default();
111
+
let files: Vec<_> = glob_with("*.jpg", &options)?
112
+
.filter_map(|x| x.ok())
113
+
.collect();
114
+
115
+
if files.len() == 0 {
116
+
bail!("No .jpg files found in current directory");
117
+
}
118
+
119
+
let thumb_dir = "thumbnails";
120
+
create_dir_all(thumb_dir)?;
121
+
122
+
println!("Saving {} thumbnails into '{}'...", files.len(), thumb_dir);
Copy file name to clipboardExpand all lines: src/intro.md
+2
Original file line number
Diff line number
Diff line change
@@ -61,6 +61,7 @@ community. It needs and welcomes help. For details see
61
61
|--------|--------|------------|
62
62
|[Mutate the elements of an array in parallel][ex-rayon-iter-mut]|[![rayon-badge]][rayon]|[![cat-concurrency-badge]][cat-concurrency]|
63
63
|[Sort a vector in parallel][ex-rayon-parallel-sort]|[![rayon-badge]][rayon][![rand-badge]][rand]|[![cat-concurrency-badge]][cat-concurrency]|
64
+
|[Generate jpg thumbnails in parallel][ex-rayon-thumbnails]|[![rayon-badge]][rayon][![glob-badge]][glob][![image-badge]][image]|[![cat-concurrency-badge]][cat-concurrency][![cat-filesystem-badge]][cat-filesystem]|
64
65
|[Spawn a short-lived thread][ex-crossbeam-spawn]|[![crossbeam-badge]][crossbeam]|[![cat-concurrency-badge]][cat-concurrency]|
65
66
|[Draw fractal dispatching work to a thread pool][ex-threadpool-fractal]|[![threadpool-badge]][threadpool][![num-badge]][num][![num_cpus-badge]][num_cpus][![image-badge]][image]|[![cat-concurrency-badge]][cat-concurrency][![cat-science-badge]][cat-science][![cat-rendering-badge]][cat-rendering]|
66
67
@@ -166,6 +167,7 @@ community. It needs and welcomes help. For details see
0 commit comments