From bd0eb609e7e81a1130587a120fe413e6234c1044 Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Sun, 9 Jul 2023 14:49:46 +0200 Subject: [PATCH] chore: added a way to find visually similar images --- knowledge base/imagemagick.md | 58 +++++++++++++++++----- knowledge base/parallel.md | 6 +++ knowledge base/resize an image from cli.md | 6 +-- 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/knowledge base/imagemagick.md b/knowledge base/imagemagick.md index 8f6b49d..5d60c2f 100644 --- a/knowledge base/imagemagick.md +++ b/knowledge base/imagemagick.md @@ -2,36 +2,70 @@ Components: +- `compare`: diff tool - `convert`: image conversion tool +## Table of contents + +1. [TL;DR](#tldr) +1. [Further readings](#further-readings) +1. [Sources](#sources) + ## TL;DR ```sh -# scale an image to 50% its original size -convert IMG_20200117_135049.jpg -adaptive-resize 50% IMG_20200117_135049_resized.jpg +# Scale images to 50% its original size. +magick convert -adaptive-resize '50%' 'in.jpg' 'out.jpg' -# create a gif using images -magick *.jpg images.gif +# Create GIFs from single images. +magick *.jpg 'out.gif' -# convert n images to individual pdf pages -magick *.jpg +adjoin page-%d.pdf +# Convert N images into individual PDF documents. +magick *.jpg +adjoin 'page-%d.pdf' -# convert n images to a single pdf document -magick *.png out.pdf +# Convert N images into pages of a single PDF document. +magick *.png 'out.pdf' + +# Calculate the percentage of *similarity* between 2 images. +# Avoid saving the diff. +magick compare -metric 'NCC' 'in_1.jpg' 'in_2.jpg' NULL: + +# Output only the differences between 2 images. +magick compare -compose 'src' -fuzz '5%' 'in_1.jpg' 'in_2.jpg' 'diff.jpg' ``` ## Further readings - [Website] -- [cheat.sh/convert] - -[cheat.sh/convert]: https://cheat.sh/convert -[website]: https://imagemagick.org +- [image similarity comparison] ## Sources +All the references in the [further readings] section, plus the following: + +- [cheat.sh/convert] +- [cheat.sh/compare] - [Converting Multiple Images into a PDF File] - [How to Quickly Resize, Convert & Modify Images from the Linux Terminal] +- [Diff an image using ImageMagick] +- [ImageMagick compare without generating diff image] + + + +[image similarity comparison]: https://imagemagick.org/script/compare.php +[website]: https://imagemagick.org + + +[further readings]: #further-readings + + + +[cheat.sh/compare]: https://cheat.sh/compare +[cheat.sh/convert]: https://cheat.sh/convert [converting multiple images into a pdf file]: https://legacy.imagemagick.org/discourse-server/viewtopic.php?p=144157&sid=e7706233f81874af86ffbbf3e57b1e76#p144157 +[diff an image using imagemagick]: https://stackoverflow.com/questions/5132749/diff-an-image-using-imagemagick [how to quickly resize, convert & modify images from the linux terminal]: https://www.howtogeek.com/109369/how-to-quickly-resize-convert-modify-images-from-the-linux-terminal/ +[imagemagick compare without generating diff image]: https://unix.stackexchange.com/questions/612067/imagemagick-compare-without-generating-diff-image diff --git a/knowledge base/parallel.md b/knowledge base/parallel.md index 75b6b29..ff1da28 100644 --- a/knowledge base/parallel.md +++ b/knowledge base/parallel.md @@ -35,6 +35,12 @@ parallel --group --jobs 100% --tag \ | jq -r '.items[] | .metadata.name + \"\t\" + .spec.nodeName' -" \ ::: "${NAMESPACES}" \ | column -t + +# Given a collection of images in the same working directory, find all those +# that are at least 90% similar, but not equal, to another. +parallel --tag "magick compare -metric NCC -quiet {} NULL: 2>&1; echo" \ + ::: *.jpg *.jpeg *.png *.webp ::: *.jpg *.jpeg *.png *.webp \ +| grep '0\.9' ``` ## Further readings diff --git a/knowledge base/resize an image from cli.md b/knowledge base/resize an image from cli.md index 35f6e94..9aa8240 100644 --- a/knowledge base/resize an image from cli.md +++ b/knowledge base/resize an image from cli.md @@ -1,12 +1,12 @@ # Resize images using the CLI -Leverages `convert` from `imagemagick`. +Leverage `convert` from `imagemagick`: ```sh -convert input.jpg -adaptive-resize 50% output.jpg +magick convert -adaptive-resize '50%' 'in.jpg' 'out.jpg' # Scale down all images in a folder. -ls -1 | xargs -I{} convert {} -adaptive-resize 50% {}_scaled.jpg +ls -1 | xargs -I{} magick convert -adaptive-resize '50%' {} {}_scaled.jpg ``` Further readings