Finding PNG Images Larger Than x Pixels Through the Linux Shell Paghahanap ng PNG na imahe na mas malaki sa x pixels pamamagitan ng Linux Shell
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. Kapag kayo ay nagsisikap na magtrabaho sa mga pagbabago ng disenyo ng iyong website, ikaw ay nababahala sa ang lapad ng mga larawan sa iyong artikulo na nilalaman. 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. Mayroon akong malaking notoriously screenshot sa karamihan ng mga articles ko na nakasulat, kaya kung nais kong taasan ang sidebar ito ay kritikal para malaman kung aling mga larawan ay magiging masyadong malawak upang umangkop sa bagong disenyo.
Since I'ma 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? Since I'ma programista, magiging madali para sa akin upang makapagsulat ng isang maliit na application upang gawin ito, ngunit ito ginawa sa akin simulan ang pag-iisip ... kung bakit hindi ko na ito sa linya Linux command?
The first thing I figured out was that PNG images display the size data when you run the “file” command on them: Ang unang bagay na ako ang korte ay na PNG imahen ipakita ang data sa sukat kapag patakbuhin mo ang "file" utos sa kanila:
$ file image3.png $ File image3.png
image3.png: PNG image data, 613 x 657, 8-bit/color RGBA, non-interlaced image3.png: PNG data ng imahe, 613 x 657, 8-bit/color RGBA, non-interlaced
Very useful since 99% of the picture on this site are in PNG format. Very kapaki-pakinabang since 99% ng mga larawan sa site na ito ay sa PNG format. So now to throw it in a loop for all the files in my upload directory: Kaya ngayon upang ihagis ito sa isang loop para sa lahat ng mga file sa aking mag-upload ng direktoryo ng:
$ for f in *.png;do file $f;done $ Para sa f png sa *.; ang file na $ f; tapos na
image.png: PNG image data, 631 x 185, 8-bit/color RGBA, non-interlaced image.png: PNG data ng imahe, 631 x 185, 8-bit/color RGBA, non-interlaced
image1.png: PNG image data, 631 x 96, 8-bit/color RGBA, non-interlaced image1.png: PNG data ng imahe, 631 x 96, 8-bit/color RGBA, non-interlaced
image10.png: PNG image data, 375 x 395, 8-bit/color RGBA, non-interlaced image10.png: PNG data ng imahe, 375 x 395, 8-bit/color RGBA, non-interlaced
image11.png: PNG image data, 484 x 241, 8-bit/color RGBA, non-interlaced image11.png: PNG data ng imahe, 484 x 241, 8-bit/color RGBA, non-interlaced
—snipped— -Snipped -
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. Ito ay mas kapaki-pakinabang, ngunit gusto ko na hilahin ang data sa Excel o isang katulad na aplikasyon upang ayusin ang mga data, kaya nagpasya ako na gamitin ang linux "hiwa" utos sa pull out lang ang lapad ng haligi.
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. Mapapansin mo na ang-F5 parameter ay nagsasabi sa hiwa gawin ang ikalimang haligi, at ang-d \ may puwang ito pagkatapos na ito ay nagsasabi sa pinutol na gumamit ng isang space bilang Delimiter. The slash \ character is an escape character to tell the shell to use the space as a character, and not as whitespace. Ang slash \ karakter ay isang pagtakas ng character sabihin sa shell gamitin ang puwang na bilang isang karakter, at hindi bilang whitespace.
$ for f in *.png;do file $f|cut -f5 -d\ ;done $ Para sa f png sa *.; ang file na $ f | hiwa-F5-d \; tapos na
631 631
631 631
375 375
484 484
—snipped— -Snipped -
Not entirely useful output, is it? Hindi lubos na kapaki-pakinabang na output, ay ito? 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. Let's push na sa pamamagitan ng isang Bash kung ang pahayag, at pagkatapos lamang ipakita ang output ng mga command na file kapag ang width ay mas malaki kaysa sa 600 pixels.
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). Paunawa sa `(backtick) sa paligid ng mga marka ng" file $ f | hiwa ... "na seksyon, na nagpapahiwatig na ang mga utos sa loob ng` ay ipoproseso ng isang solong output at sawang sa kung ang pahayag, kung saan gumagamit kami ng isang-gt (mas malaki kaysa sa ). Also note that you need spaces around either side of the brackets [ ] Gayundin tandaan na kailangan mo ng puwang sa paligid ng magkabilang panig ng bracket []
for f in *.png;do if [ `file $f | cut -f5 -d\ ` -gt 600 ] ; then file $f;fi;done para sa f png sa *.; gawin kung [ `file $ f | hiwa-F5-d \`-gt 600]; pagkatapos ng file $ f; fi; tapos na
image.png: PNG image data, 631 x 185, 8-bit/color RGBA, non-interlaced image.png: PNG data ng imahe, 631 x 185, 8-bit/color RGBA, non-interlaced
image1.png: PNG image data, 631 x 96, 8-bit/color RGBA, non-interlaced image1.png: PNG data ng imahe, 631 x 96, 8-bit/color RGBA, non-interlaced
image17.png: PNG image data, 638 x 340, 8-bit/color RGBA, non-interlaced image17.png: PNG data ng imahe, 638 x 340, 8-bit/color RGBA, non-interlaced
image18.png: PNG image data, 608 x 448, 8-bit/color RGBA, non-interlaced image18.png: PNG data ng imahe, 608 x 448, 8-bit/color RGBA, non-interlaced
—snipped— -Snipped -
Now we have a list of all the files larger than 600 pixels wide. Ngayon kami ay may isang listahan ng lahat ng mga file na mas malaki kaysa sa 600 na lapad. 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: Maaari mong ayusin ang "file $ f" sa dulo sa makatarungan echo ang filename kung kailangan upang kopyahin o ilipat ang mga ito sa iba pang lugar:
for f in *.png;do if [ `file $f | cut -f5 -d\ ` -gt 600 ] ; then echo $f;fi;done para sa f png sa *.; gawin kung [ `file $ f | hiwa-F5-d \`-gt 600]; pagkatapos echo $ f; fi; tapos na
image.png image.png
image1.png image1.png
image17.png image17.png
image18.png image18.png
—snipped— -Snipped -
The Linux shell is incredibly powerful! Ang Linux shell ay incredibly malakas! 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. Solusyon na ito ay hindi tunay na praktikal na para sa lahat ng tao, ngunit ito ay mabuting malaman kung paano gumagana sa mga shell sa gayon maaari mong magawa ang ganitong uri ng gawain kapag kailangan mo na.

Daily Email Updates Araw-araw na Updates Email
You can get our how-to articles in your inbox each day for free. Maaari kang makakuha ng aming kung-paano na mga artikulo sa iyong inbox sa bawat araw para sa libre. Just enter your name and email below: Ilagay lamang ang inyong pangalan at email sa ibaba:



Damn those commands look complicated, Mapapahamak ang mga utos tumingin kumplikado,
And then people wonder why linux is so unpopular with the *normal* people… At pagkatapos ay ang mga tao-taka kung bakit ang linux ay kaya hindi popular sa mga * * * * * * normal na tao ...
Just my 2 cents. Basta ang aking 2 cents.
(linux user btw) (linux user btw)
Victor, Victor,
I certainly wouldn't advocate this type of thing for “normal” people =) Ako tiyak hindi tagapagtaguyod ng ganitong uri ng bagay para sa "normal" ang mga tao =)
Thanks I needed that. Salamat na kailangan ko na. I am trying to learn linux. Ako ay nagsisikap upang malaman ang linux.
Any idea how to do this with jpg's? Anumang mga ideya kung paano gawin ito sa jpg's?
Dennis, Dennis,
I'm not actually sure, but I think you'd have to find a utility that could check the dimensions of a jpg image, and you could substitute it on the command line. Hindi ako sigurado talaga, pero tingin ko gusto mong makahanap ng isang utility na maaaring i-tsek ang sukat ng isang jpg imahen, at maaari mong ipalit ito sa linya ng command.
Alternatively you could probably write a php, python or perl script and interface with the gd library. Bilang kahalili ka could marahil magsulat ng isang php, sawa o Perl script at interface sa gd library.
wow… wow ...
You should put it into the convert command (imagemagick) to scale it if it's bigger Ikaw ay dapat ilagay ito sa pag-convert ng utos (imagemagick) sa scale ito kung ito ay mas malaki
Nice. Ayos. This is a perfect example of the power of the Linux shell. Ito ay isang perpektong halimbawa ng kapangyarihan ng mga shell sa Linux. It may look comlicated, but after you get over the learning curve, you can do things that are incredibly powerful. Ito ay maaaring tingnan ang comlicated, ngunit matapos na makakuha ka sa pag-aaral ng curve, maaari mong gawin sa mga bagay na incredibly makapangyarihan.
Thanks for the example, Salamat para sa mga halimbawa,
Daniel Daniel
daniel1992.wordpress.com daniel1992.wordpress.com
ImageMagick's identify should be able to identify any files you have enabled while compiling it. ImageMagick's makilala ang dapat ma-tukuyin ang anumang mga file na napagana mo habang ipon ito. For example, Halimbawa, ang
identify ceaec3da6ef432af59fbdad2c93c277e.jpg kilalanin ceaec3da6ef432af59fbdad2c93c277e.jpg
ceaec3da6ef432af59fbdad2c93c277e.jpg JPEG 160×90 160×90+0+0 DirectClass 8-bit 10.1797kb ceaec3da6ef432af59fbdad2c93c277e.jpg JPEG 160 × 90 160 × 90 0 0 DirectClass 8-bit 10.1797kb