diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-03-06 22:44:29 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-03-06 22:44:29 +0100 |
| commit | 1daea33eec09c376b09739e204bee480f7bf4140 (patch) | |
| tree | 77e6cfa2902c675465407750f48a7c5af4ac711b /src | |
| parent | 5b4f601fc74a1ed607fb93fea7cabc4cc3a1f5ba (diff) | |
| download | sebastiano.tronto.net-1daea33eec09c376b09739e204bee480f7bf4140.tar.gz sebastiano.tronto.net-1daea33eec09c376b09739e204bee480f7bf4140.zip | |
Finished blog post
Diffstat (limited to 'src')
| -rw-r--r-- | src/blog/2023-03-06-resize-pictures/resize-pictures.md | 79 |
1 files changed, 59 insertions, 20 deletions
diff --git a/src/blog/2023-03-06-resize-pictures/resize-pictures.md b/src/blog/2023-03-06-resize-pictures/resize-pictures.md index c28340f..e7e7f18 100644 --- a/src/blog/2023-03-06-resize-pictures/resize-pictures.md +++ b/src/blog/2023-03-06-resize-pictures/resize-pictures.md | |||
| @@ -12,39 +12,39 @@ $ du -h -d 1 sebastiano.tronto.net | |||
| 12 | 225M sebastiano.tronto.net | 12 | 225M sebastiano.tronto.net |
| 13 | ``` | 13 | ``` |
| 14 | 14 | ||
| 15 | Stupid modern phones and their multi-megapixel cameras! | 15 | Stupid modern phones and their multi-megapixel cameras! I definitely |
| 16 | I definitely do not want my minimalist blog to waste your bandwidth, | 16 | do not want my minimalist blog to waste your bandwidth, I have to fix |
| 17 | I have to fix this. I could just go through all my pictures and resize | 17 | this. I could just go through all my pictures and resize them one by |
| 18 | them with something like [gimp](https://www.gimp.org/) - there are like | 18 | one with something like [gimp](https://www.gimp.org/) - there are like |
| 19 | 10 of them. But that's boring. Let's do it with the command line instead. | 19 | 10 of them. But that's boring. Let's do it with the command line instead! |
| 20 | 20 | ||
| 21 | ## ImageMagick | 21 | ## ImageMagick |
| 22 | 22 | ||
| 23 | The obvious question is: how do we even edit an image file | 23 | The obvious question is: how do we even edit a picture |
| 24 | with the command line? Luckily, there is a tool for that: | 24 | with the command line? Luckily, there is a tool for that: |
| 25 | [ImageMagick](https://imagemagick.org/). This piece of software | 25 | [ImageMagick](https://imagemagick.org/). This piece of software can do a |
| 26 | does a ton of things, and I do not use it very often. So I always | 26 | ton of things, and I do not use it very often, so I always need to look |
| 27 | need to look up what I want to do. | 27 | up what I want to do. |
| 28 | 28 | ||
| 29 | One way to invoke ImageMagick is by calling the `convert` command, | 29 | One way to invoke ImageMagick is by |
| 30 | which can take a | 30 | calling the `convert` command, which can take a |
| 31 | [`-resize`](https://imagemagick.org/script/command-line-options.php#resize) | 31 | [`-resize`](https://imagemagick.org/script/command-line-options.php#resize) |
| 32 | option, followed by a | 32 | option, followed by a |
| 33 | [`geometry`](https://imagemagick.org/script/command-line-processing.php#geometry) | 33 | [`geometry`](https://imagemagick.org/script/command-line-processing.php#geometry) |
| 34 | argument. After checking the online manual, I understood that the | 34 | argument. After checking the online manual, I understood that the command |
| 35 | command I was looking for was: | 35 | I was looking for was: |
| 36 | 36 | ||
| 37 | ``` | 37 | ``` |
| 38 | $ convert picture.jpg -resize "750>" picture.jpg | 38 | $ convert picture.jpg -resize "750>" picture.jpg |
| 39 | ``` | 39 | ``` |
| 40 | 40 | ||
| 41 | which resizes `picture.jpg` by scaling it down to at most 750px width - | 41 | which resizes `picture.jpg` by scaling it down to at most 750px width - |
| 42 | keeping the ratio between width and height, and leaving it untouched if it | 42 | preserving the ratio between width and height, and leaving the picture |
| 43 | is already smaller. The value 750 was chosen after a couple of attempts, | 43 | untouched if it is already smaller. The value 750 was chosen after a |
| 44 | it seems a good compromise between quality and size. | 44 | couple of attempts, it seems a good compromise between quality and size. |
| 45 | 45 | ||
| 46 | And now I just have to do this for all the pictures. Of course, running | 46 | And now I just have to do this for all pictures. Of course, running the |
| 47 | the same command 10 times with a different argument is out of question. | 47 | same command 10 times with a different argument is out of question. |
| 48 | 48 | ||
| 49 | ## find(1) | 49 | ## find(1) |
| 50 | 50 | ||
| @@ -58,7 +58,7 @@ running a command). The OpenBSD and GNU versions of find have some | |||
| 58 | differences, check your local manual page. The commands I use | 58 | differences, check your local manual page. The commands I use |
| 59 | here have been tested on the GNU version, but should be standard. | 59 | here have been tested on the GNU version, but should be standard. |
| 60 | 60 | ||
| 61 | To look for file and simply print their name, we can use `-name`: | 61 | To look for files and simply print their name, we can use `-name`: |
| 62 | 62 | ||
| 63 | ``` | 63 | ``` |
| 64 | $ find src -name \*.jpg -or -name \*.png | 64 | $ find src -name \*.jpg -or -name \*.png |
| @@ -75,12 +75,51 @@ src/blog/2023-02-25-job-control/jobs-diagram.png | |||
| 75 | src/blog/2022-12-30-blog-ready/pc-planner.jpg | 75 | src/blog/2022-12-30-blog-ready/pc-planner.jpg |
| 76 | ``` | 76 | ``` |
| 77 | 77 | ||
| 78 | Notice how I had to escape the `*`: the strings `\*.jpg` and `\*.png` | ||
| 79 | are part of the find command, we don't want the shell to expand them. | ||
| 80 | |||
| 78 | Now we can use `-exec` to run a command on each of these files, | 81 | Now we can use `-exec` to run a command on each of these files, |
| 79 | using `{}` to refer to the file's name: | 82 | using `{}` to refer to the file's name: |
| 80 | 83 | ||
| 81 | ``` | 84 | ``` |
| 82 | find src \( -name \*.jpg -or -name \*.png \) -exec convert {} -resize "750>" {} \; | 85 | $ find src \( -name \*.jpg -or -name \*.png \) -exec convert {} -resize "750>" {} \; |
| 83 | ``` | 86 | ``` |
| 84 | 87 | ||
| 85 | We get a couple of warnings about grayscale images, but whatever. | 88 | We get a couple of warnings about grayscale images, but whatever. |
| 86 | This seems to have worked. | 89 | This seems to have worked. |
| 90 | |||
| 91 | ## The weight of history | ||
| 92 | |||
| 93 | Let's see if things have improved: | ||
| 94 | |||
| 95 | ``` | ||
| 96 | $ du -h -d 1 sebastiano.tronto.net | ||
| 97 | 19M sebastiano.tronto.net/gemini | ||
| 98 | 19M sebastiano.tronto.net/http | ||
| 99 | 19M sebastiano.tronto.net/src | ||
| 100 | 102M sebastiano.tronto.net/.git | ||
| 101 | 158M sebastiano.tronto.net | ||
| 102 | ``` | ||
| 103 | |||
| 104 | That's... a bit disappointing. It looks like large pictures were not | ||
| 105 | the only culprit - all the [slides for my math talks](../../research) | ||
| 106 | are also quite heavy. Too bad, I am not going to change them now. | ||
| 107 | Maybe in a follow-up post :-) | ||
| 108 | |||
| 109 | Just like me with my old slides, git also wants to keep a memory | ||
| 110 | of the past. After all, this is what version control system are | ||
| 111 | supposed to do. If you are wondering why my website is a git | ||
| 112 | repository, check out | ||
| 113 | [this old blog post of mine](../2022-08-14-website). In the end, | ||
| 114 | I don't really need the ability to revert my website to an older | ||
| 115 | version, so I could just reset the repo from all the history | ||
| 116 | at some point. | ||
| 117 | |||
| 118 | ## Conclusion | ||
| 119 | |||
| 120 | ImageMagick is and find are both powerful tools. I am happy with looking | ||
| 121 | up ImageMagick's syntax everytime, but I definitely want to become more | ||
| 122 | proficient with find. | ||
| 123 | |||
| 124 | This was not a hard task, but I have learned something new. I hope you | ||
| 125 | did too :-) | ||
