Working with images in Linux CLI
5 December 2022
# Intro
Hey, you probably don’t need to use shady online tools or resource-heavy GUI programs for working with images!
Well, at least if you want to:
- Create thumbnail
- Add watermark
- Remove Exif data
- Compress image
In this short post, I will show you simple examples of Linux CLI tools usage that can help you with various tasks regarding images. All examples below were tested on Fedora Linux.
Tools that will be used:
Prerequisites:
# Create thumbnail
Create a thumbnail from the given image. Restrict only width value for the resize.
1
2
3
4
5
6
7
8
IMAGE_IN="cat.jpg"
IMAGE_OUT="cat-thumbnail.jpg"
THUMBNAIL_WIDTH="200"
convert -thumbnail \
"$THUMBNAIL_WIDTH" \
"$IMAGE_IN" \
"$IMAGE_OUT"
# Add watermark
Add dissolved (15%) watermark to the center of the image. Output quality is set to max.
1
2
3
4
5
6
7
8
9
10
11
IMAGE_IN="cat.jpg"
IMAGE_OUT="cat-watermarked.jpg"
WATERMARK="watermark.jpg
composite \
-dissolve 15% \
-gravity Center \
-quality 100 \
"$WATERMARK" \
"$IMAGE_IN" \
"$IMAGE_OUT"
# Remove exif data
Remove all Exif data from the given image but preserve ICC profile. Deleting ICC profile may affect image colours. No copy will be created - the original file will be overwritten.
1
2
3
4
5
6
7
8
IMAGE_IN="cat.jpg"
IMAGE_OUT="cat-no-exif.jpg"
exiftool \
-overwrite_original \
-All= \
--icc_profile:all \
"$IMAGE_IN" \
# Compress image
Set quality level to 30%. You can play around with different values and observe output quality. Don’t forget to check file size before and after tests :)
1
2
3
4
5
6
7
8
IMAGE_IN="cat.jpg"
IMAGE_OUT="cat-compressed.jpg"
QUALITY="30%"
convert \
"$IMAGE_IN" \
-quality "$QUALITY" \
"$IMAGE_OUT"
# Summary
I am using all mentioned commands during my blog development. Mentioned examples are quite simple - both presented tools can be much more powerful.