Home > Blockchain >  command to print images with pixel size within directory
command to print images with pixel size within directory

Time:09-21

is it possible to list all images in a directory that have pixel size grater or smaller than x? I found this command on another thread

find . -name '*.png' -exec file {} \; | sed 's/\(.*png\): .* \([0-9]* x [0-9]*\).*/\2 \1/' | awk 'int($1) < 250 {print}'

this command works well for *.png, but the moment I change it to *.jpg the results get too crazy. see below. I would like to write the results to a log file so that I can later delete these images from my product catalog.

*.png output

50 x 50 ./data/catalog/agsquare.png
220 x 138 ./data/catalog/[email protected]
3 x 3 ./data/catalog/dots.png
98 x 98 ./data/catalog/grid-noise.png

*jpg output (the pixel size is ignored, something is clearly wrong with this command)

./data/background/fashion-shirt.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 1920x1440, frames 3
./data/background/grey-bag.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 1920x1280, frames 3

OS: Ubuntu 18.04.5 LTS

CodePudding user response:

file outputs 1 x 2 for PNG but 1x2 for JPEG. Your command expects the spaces.

Instead, use a tool like identify from ImageMagick that can do this in a format independent manner:

find . \( -name '*.jpg' -o -name '*.png' \) \
    -exec identify -format '%w %h %i\n' {}   |
    awk '$1 < 250'

CodePudding user response:

In order of simplest to more heavy-weight, you have several options:

  • feh
  • exiftool
  • ffprobe - little friend of ffmpeg
  • ImageMagick identify

According the Ubuntu package repository, you should have feh installed in Ubuntu 18 LTS, so you can use:

feh -L "%f:%w:%h"  *.jpg *.png

Sample Output

small.png:10:10
red.jpg:8:8
redhat.jpg:254:255

Note feh also accepts —min-dimension and —max-dimension as filters. So, for all images wider than 640 px, use:

feh --min-dimension 640x0 -L "%f:%w:%h"  *.jpg *.png

You can also use exiftool to find and print images with height or width less than 100 px like this:

exiftool -p '$filename:$imagewidth:$imageheight' -if '$imagesize and ($imagewidth<100 or $imageheight<100)' -q *.jpg *.png

Sample Output

black.jpg:8:8
red.jpg:8:8
small.png:10:10

If you are on any "operating system" originating from Redmond, replace the single quotes with double quotes.

Note that exiftool is a significantly smaller installation than ImageMagick.


Ubuntu 18 LTS should also include ffmpeg and its little friend ffprobe, so you can do:

ffprobe -v error -select_streams v -show_entries stream=width,height  -of csv=p=0:s=x IMAGEFILE

Or with ImageMagick, use this to find images with width<100:

magick identify -format '%f:%w:%h\n' *png *jpg | awk -F: '$2<100'

CodePudding user response:

thanks to the other guy I removed the spaces around the x and it worked. failed to mention that I did not want to use any tool, the other commands could not be found on my server.

find . -name '*.jpg' -exec file {} \; | sed 's/\(.*jpg\): .* \([0-9]*x[0-9]*\).*/\2 \1/' | awk 'int($1) < 250 {print}'
  • Related