Thursday, May 2, 2024
23
rated 0 times [  23] [ 0]  / answers: 1 / hits: 19532  / 2 Years ago, sun, may 8, 2022, 2:27:48

I am trying to find all JPG images within a folder with subfolders that have either width or height below 300px.


This way I want to detect old thumbnails and delete them.


For sure I can find all images using find:


find . -iname "*.jpg" -type f | ...

But what follows after the pipe? Which package can I use to detect picture attributes?


More From » command-line

 Answers
4

You can use identify from imagemagick, and you can use the following command:



find . -iname "*.jpg" -type f -exec identify -format '%w %h %i' '{}' ; | awk '$1<300 || $2<300'


the use of -exec <command> '{}' ; makes sure that your filename can have spaces in them, alternatively you can use



find . -iname "*.jpg" -type f | xargs -I{} identify -format '%w %h %i' {} | awk '$1<300 || $2<300'


where the -I{} takes care of the same thing.



What I like about identify is that you can specify the output format; in this case '%w %h %i' which gives the width, height and full pathname of the image. Then the awk expression only keeps those lines for which the image is smaller than the desired size.



Example of the output:



64 64 ./thumbsup.jpg
100 150 ./photomin.jpg


Edit: If you want the filenames only (for piping to rm for instance), simply change $line in the awk statement to $3, then it will only print the third column.


[#33290] Monday, May 9, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tudatchful

Total Points: 270
Total Questions: 109
Total Answers: 122

Location: Palau
Member since Tue, May 30, 2023
1 Year ago
;