按大小查找图像:find / file / awk

ste*_*ino 10 find xargs awk files images

我一直在尝试找到一定高度(超过 500 像素)的 png 图像文件。我知道这file将返回图像尺寸。例子:

$ file TestImg1a.png

TestImg1a.png: PNG image data, 764 x 200, 4-bit colormap, non-interlaced   
Run Code Online (Sandbox Code Playgroud)

但是我需要使用它来查找目录中高度超过 500px 的所有文件。我知道如何打印所有文件而不考虑高度:

find . -name '*.png' | xargs file | awk '{print $7 " " $1}'
Run Code Online (Sandbox Code Playgroud)

但是我如何将 7 美元限制为大于 500 的结果?

Sté*_*las 13

exiftool -q -r -ext png -if '$ImageHeight > 500' -p '$Directory/$FileName' .
Run Code Online (Sandbox Code Playgroud)


h3r*_*ler 7

我知道这有点矫枉过正,但是,无论文件如何显示信息,这每次都会起作用(即使您的文件名中有空格)。

find . -name '*.png' -exec file {} \; | sed 's/\(.*png\): .* \([0-9]* x [0-9]*\).*/\2 \1/' | awk 'int($1) > 500 {print}'
Run Code Online (Sandbox Code Playgroud)

并打印图片和文件的尺寸

说明:

  1. find.png 下所有名为 *.png 的文件 并为每个人做一个文件

  2. 用于sed仅打印文件名和尺寸,然后重新排序以先打印尺寸

  3. 用于awk测试第一个数字(图片的高度),确保它大于 500,如果它是打印尺寸和文件名,如果不是什么都不做。


Lri*_*Lri 6

您还可以identify从 ImageMagick使用:

find . -name \*.png -print0|xargs -0 identify -format '%h %f\n'|
awk '$1>500'|cut -d' ' -f2-
Run Code Online (Sandbox Code Playgroud)

或者在 OS X 中:

mdfind 'kMDItemFSName=*.png&&kMDItemPixelHeight>500' -onlyin .
Run Code Online (Sandbox Code Playgroud)


cjc*_*cjc 5

我觉得 shell 实用程序以外的东西会更合适,例如,Perl:

#!/usr/bin/perl

use File::Find;
use Image::Info qw(image_info dim);

find (\&check_height, './');

sub check_height {

  my $info = image_info( $_ );
  my ($width, $height) = dim( $info );
  print $_ . " has height $height\n" if ( $height > 500 );

}
Run Code Online (Sandbox Code Playgroud)

减少试图解析 $7 的麻烦;直接获取尺寸即可。是的,您将需要 Image::Info 模块,但是,在 CentOS/RHEL 上,它是一个标准包,因此您只需运行yum install perl-Image-Info.