通过Linux Shell查找大于x像素的PNG图像

When you are trying to work on changing the design of your website, you have to be concerned with the width of the pictures in your article content. I’ve got notoriously large screenshots on most of the articles I’ve written, so if I want to increase the sidebar it’s critical to figure out which pictures are going to be too wide to fit in the new design.

当您尝试更改网站的设计时,必须考虑文章内容中图片的宽度。 众所周知,我撰写的大多数文章都有很大的屏幕截图,因此,如果要增加侧边栏,至关重要的是弄清楚哪些图片的宽度太大而无法适应新设计。

Since I’m a programmer, it would be easy for me to write a small application to do this, but it made me start thinking… why can’t I do this on the Linux command line?

由于我是一名程序员,因此编写一个小应用程序很容易做到这一点,但是这让我开始思考……为什么我不能在Linux命令行上做到这一点?

The first thing I figured out was that PNG images display the size data when you run the “file” command on them:

我首先想到的是,当您在PNG图片上运行“文件”命令时,它们会显示尺寸数据:

$ file image3.pngimage3.png: PNG image data, 613 x 657, 8-bit/color RGBA, non-interlaced

$ file image3.pngimage3.png:PNG图像数据,613 x 657,8位/彩色RGBA,非隔行

Very useful since 99% of the picture on this site are in PNG format. So now to throw it in a loop for all the files in my upload directory:

非常有用,因为此站点上99%的图片均为PNG格式。 现在,将其放入我的上传目录中的所有文件的循环中:

$ for f in *.png;do file $f;done

$ for * .png中的f;执行文件$ f;完成

image.png: PNG image data, 631 x 185, 8-bit/color RGBA, non-interlacedimage1.png: PNG image data, 631 x 96, 8-bit/color RGBA, non-interlacedimage10.png: PNG image data, 375 x 395, 8-bit/color RGBA, non-interlacedimage11.png: PNG image data, 484 x 241, 8-bit/color RGBA, non-interlaced—snipped—

image.png:PNG图像数据,631 x 185,8位/彩色RGBA,非隔行image1.png:PNG图像数据,631 x 96,8位/彩色RGBA,non-interlacedimage10.png:PNG图像数据, 375 x 395,8位/彩色RGBA,非隔行扫描image11.png:PNG图像数据,484 x 241,8位/彩色RGBA,非隔行扫描—

This is more useful, but I’d have to pull the data into Excel or a similar application in order to sort the data, so I decided to use the linux “cut” command to pull out just the width column.

这更有用,但是我必须将数据提取到Excel或类似的应用程序中以便对数据进行排序,因此我决定使用linux“ cut”命令仅提取width列。

You’ll notice the -f5 parameter tells cut to take the fifth column, and the -d\ with a space after it tells cut to use a space as the delimiter. The slash \ character is an escape character to tell the shell to use the space as a character, and not as whitespace.

您会注意到-f5参数告诉cut占据第五列,-d \后面带有空格,告诉cut使用空格作为定界符。 斜杠\字符是一个转义字符,告诉外壳程序将空格用作字符,而不用作空格。

$ for f in *.png;do file $f|cut -f5 -d\ ;done

$ for * .png中的f;执行文件$ f | cut -f5 -d \;完成

631631375484—snipped—

631631375484-已抓取-

Not entirely useful output, is it? Let’s push that through a bash if statement, and then only show the output of the file command when the width is larger than 600 pixels.

不是完全有用的输出,是吗? 让我们通过bash if语句来推动它,然后仅在宽度大于600像素时显示file命令的输出。

Notice the ` (backtick) marks around the “file $f | cut…” section, which indicate that the commands inside the ` will be processed as a single output and fed into the if statement, where we use a -gt (greater than). Also note that you need spaces around either side of the brackets [ ]

请注意,“ $ f |文件”周围的`(反引号)标记。 cut…”部分,它表示`内部的命令将作为单个输出处理,并馈送到if语句中,在这里我们使用-gt(大于)。 另请注意,在括号[]的两侧都需要有空格

for f in *.png;do if [ `file $f | cut -f5 -d\ ` -gt 600 ] ; then file $f;fi;done

* .png中的f;如果[`file $ f | 切-f5 -d \`-gt 600]; 然后提交$ f; fi; done

image.png: PNG image data, 631 x 185, 8-bit/color RGBA, non-interlacedimage1.png: PNG image data, 631 x 96, 8-bit/color RGBA, non-interlacedimage17.png: PNG image data, 638 x 340, 8-bit/color RGBA, non-interlacedimage18.png: PNG image data, 608 x 448, 8-bit/color RGBA, non-interlaced—snipped—

image.png:PNG图像数据,631 x 185,8位/彩色RGBA,非隔行image1.png:PNG图像数据,631 x 96,8位/彩色RGBA,non-interlacedimage17.png:PNG图像数据, 638 x 340,8位/彩色RGBA,非隔行扫描image18.png:PNG图像数据,608 x 448,8位/彩色RGBA,非隔行扫描—

Now we have a list of all the files larger than 600 pixels wide. You could adjust the “file $f” at the end to just echo out the filenames if you needed to copy or move them somewhere else:

现在,我们有了所有大于600像素宽的文件的列表。 如果您需要将文件名复制或移动到其他位置,则可以在最后调整“ file $ f”以回显文件名:

for f in *.png;do if [ `file $f | cut -f5 -d\ ` -gt 600 ] ; then echo $f;fi;done

* .png中的f;如果[`file $ f | 切-f5 -d \`-gt 600]; 然后回显$ f; fi; done

image.pngimage1.pngimage17.pngimage18.png—snipped—

image.pngimage1.pngimage17.pngimage18.png(已抓取)

The Linux shell is incredibly powerful! This solution isn’t really practical for everybody, but it’s good to know how to work with the shell so you can accomplish this type of task when you need to.

Linux Shell功能强大! 该解决方案并非对每个人都切实可行,但是最好知道如何使用Shell,以便您可以在需要时完成此类任务。

翻译自: https://www.howtogeek.com/howto/linux/finding-png-images-larger-than-x-pixels-through-the-linux-shell/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值