Imagemagick: Difference between revisions
Jump to navigation
Jump to search
(Created page with "== Howto == === Image resize === convert front.jpg -resize 500x500 folder.jpg") |
|||
(13 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== Reference == |
|||
* [http://www.imagemagick.org/script/command-line-options.php Imagemagick command-line options] |
|||
* [https://www.imagemagick.org/script/convert.php ImageMagick convert command documentation] |
|||
* [http://www.fmwconcepts.com/imagemagick/index.php '''Fred's ImageMagick Scripts'''] — An impressive set of scripts. |
|||
== Install == |
|||
;IMEI |
|||
* To install the latest version, easiest is to use [https://github.com/SoftCreatR/imei IMEI] (ImageMagick Easy Install), which enables also several options (AVIF support...). |
|||
* Missing dependency: |
|||
<source lang="bash"> |
|||
sudo apt install yasm |
|||
sudo ./imei.sh |
|||
</source> |
|||
== Howto == |
== Howto == |
||
=== Image resize === |
=== Image resize === |
||
<source lang=bash> |
|||
⚫ | |||
convert input.jpg -resize 500x500 output.jpg # Resize to fit in 500x500 max. Final image is not necessarily 500x500. |
|||
⚫ | |||
-gravity center -extent 500x500 output.jpg # Resize, and if necessary center image and pad with black background |
|||
convert input.jpg -filter cubic -resize 500x500 output.jpg # Set resize filter. 'convert -list-filter' to get a list |
|||
convert input.jpg -filter cubic -resize 500x500^ output.jpg # Resize to fix outside 500x500, preserving aspect ratio. |
|||
convert input.jpg -filter -resize 500x500 -quality 85 output.jpg # Setting JPG quality |
|||
</source> |
|||
=== Image rotate === |
|||
<source lang="bash"> |
|||
convert input.jpg -rotate 90 input-90.jpg # Lossy rotation (clockwise) |
|||
mogrify -rotate -90 *.jpg # in-place rotate (counterclockwise) |
|||
</source> |
|||
For lossless rotation, see: |
|||
* <code>jpegtran</code> from package {{deb|libjpeg-turbo-progs}} |
|||
<source lang="bash"> |
|||
jpegtran -rot 90 -perfect -copy all foo.jpg |
|||
</source> |
|||
* <code>exiftran</code> from package {{deb|exiftran}} |
|||
<source lang="bash"> |
|||
exiftran -i9 image.jpg # in-place, rotate 90 |
|||
</source> |
|||
=== Get image info === |
|||
<source lang=bash> |
|||
convert -format "%f %w %h" sand.jpg info: |
|||
identify -format "%f %w %h" sand.jpg # Same but shorter |
|||
identify sand.jpg -format "%f %w %h" # WRONG - format string must come before because params are processed sequentially |
|||
read -r file width height <<< $(identify -format "%f %w %h" sand.jpg) # Set values in variables |
|||
</source> |
|||
=== Crop an image === |
|||
<source lang=bash> |
|||
convert IMG_0001.jpg -gravity Center -crop 1000x1000+0+0 +repage IMG_0001-resized.jpg # Crop a 1000x1000 region around center. The '+0+0' are mandatory. |
|||
</source> |
|||
=== Convert to JPG === |
|||
<source lang=bash> |
|||
convert IMG_0001.png IMG_0001.jpg # Convert with default quality |
|||
convert IMG_0001.png -quality 96 IMG_0001.jpg # Specify JPG quality |
|||
</source> |
|||
=== Blur an image === |
|||
<source lang=bash> |
|||
convert IMG_0001.jpg -gaussian-blur 5x2 IMG_0001-blur.jpg # Similar to Gimp gaussian blur 5x5 px |
|||
convert IMG_0001.jpg -gaussian-blur 5x2 -filter cubic -resize 1000x1000^ -gravity Center -crop 1000x1000+0+0 +repage -quality 96 IMG_0001-resized.jpg |
|||
</source> |
|||
=== Get pixel color of an image === |
|||
Use <code>pixel:</code>: |
|||
<source lang="bash"> |
|||
convert image.png -format '%[pixel:p{545,276}]' info:- |
|||
</source> |
|||
Now say we want to pick the pixel color of a sequence of images, but only keep those information when the color changes in the sequence: |
|||
<source lang=bash> |
|||
last=""; for f in png/*.png; do color=$(convert $f -format '%[pixel:p{545,276}]' info:-); if ! [ "$last" = "$color" ]; then echo "$f; $color"; last=$color; fi; done > banner_bg_colors.txt |
|||
</source> |
|||
=== Diff two images === |
|||
Use <code>compare</code>: |
|||
<source lang="bash"> |
|||
compare image1 image2 -compose src diff.png |
|||
</source> |
|||
Or more advanced script, but only compare luminance [https://miki.immie.org/mediawiki/index.php?title=Imagemagick&action=edit§ion=2]: |
|||
<source lang="bash"> |
|||
convert '(' file1.png -flatten -grayscale Rec709Luminance ')' \ |
|||
'(' file2.png -flatten -grayscale Rec709Luminance ')' \ |
|||
'(' -clone 0-1 -compose darken -composite ')' \ |
|||
-channel RGB -combine diff.png |
|||
</source> |
|||
Works as follows: |
|||
* Convert both file1.png and file2.png to grayscale. |
|||
* Then treat the first as the red channel of the resulting image, the second as the green channel. |
|||
* The blue channel is formed from these two using the darken compose operator, which essentially means taking the minimum. |
Latest revision as of 16:48, 14 November 2023
Reference
- Imagemagick command-line options
- ImageMagick convert command documentation
- Fred's ImageMagick Scripts — An impressive set of scripts.
Install
- IMEI
- To install the latest version, easiest is to use IMEI (ImageMagick Easy Install), which enables also several options (AVIF support...).
- Missing dependency:
sudo apt install yasm
sudo ./imei.sh
Howto
Image resize
convert input.jpg -resize 500x500 output.jpg # Resize to fit in 500x500 max. Final image is not necessarily 500x500.
convert input.jpg -resize 500x500 -background black -compose Copy \
-gravity center -extent 500x500 output.jpg # Resize, and if necessary center image and pad with black background
convert input.jpg -filter cubic -resize 500x500 output.jpg # Set resize filter. 'convert -list-filter' to get a list
convert input.jpg -filter cubic -resize 500x500^ output.jpg # Resize to fix outside 500x500, preserving aspect ratio.
convert input.jpg -filter -resize 500x500 -quality 85 output.jpg # Setting JPG quality
Image rotate
convert input.jpg -rotate 90 input-90.jpg # Lossy rotation (clockwise)
mogrify -rotate -90 *.jpg # in-place rotate (counterclockwise)
For lossless rotation, see:
jpegtran
from package libjpeg-turbo-progs
jpegtran -rot 90 -perfect -copy all foo.jpg
exiftran
from package exiftran
exiftran -i9 image.jpg # in-place, rotate 90
Get image info
convert -format "%f %w %h" sand.jpg info:
identify -format "%f %w %h" sand.jpg # Same but shorter
identify sand.jpg -format "%f %w %h" # WRONG - format string must come before because params are processed sequentially
read -r file width height <<< $(identify -format "%f %w %h" sand.jpg) # Set values in variables
Crop an image
convert IMG_0001.jpg -gravity Center -crop 1000x1000+0+0 +repage IMG_0001-resized.jpg # Crop a 1000x1000 region around center. The '+0+0' are mandatory.
Convert to JPG
convert IMG_0001.png IMG_0001.jpg # Convert with default quality
convert IMG_0001.png -quality 96 IMG_0001.jpg # Specify JPG quality
Blur an image
convert IMG_0001.jpg -gaussian-blur 5x2 IMG_0001-blur.jpg # Similar to Gimp gaussian blur 5x5 px
convert IMG_0001.jpg -gaussian-blur 5x2 -filter cubic -resize 1000x1000^ -gravity Center -crop 1000x1000+0+0 +repage -quality 96 IMG_0001-resized.jpg
Get pixel color of an image
Use pixel:
:
convert image.png -format '%[pixel:p{545,276}]' info:-
Now say we want to pick the pixel color of a sequence of images, but only keep those information when the color changes in the sequence:
last=""; for f in png/*.png; do color=$(convert $f -format '%[pixel:p{545,276}]' info:-); if ! [ "$last" = "$color" ]; then echo "$f; $color"; last=$color; fi; done > banner_bg_colors.txt
Diff two images
Use compare
:
compare image1 image2 -compose src diff.png
Or more advanced script, but only compare luminance [1]:
convert '(' file1.png -flatten -grayscale Rec709Luminance ')' \
'(' file2.png -flatten -grayscale Rec709Luminance ')' \
'(' -clone 0-1 -compose darken -composite ')' \
-channel RGB -combine diff.png
Works as follows:
- Convert both file1.png and file2.png to grayscale.
- Then treat the first as the red channel of the resulting image, the second as the green channel.
- The blue channel is formed from these two using the darken compose operator, which essentially means taking the minimum.