Prenumerera till Hur-Till Geek

Important! This is an automatic machine translated page. If you can read english, you should Click Here to read the original English version of the article.

När du finner PNG avbildar större än x-PIXEL till och med Linuxen Shell

När du är pröva att fungera på att ändra designen av din website, måste du att angå med bredden av föreställer i din nöjda artikel. Jag har fått ökänt stora screenshots på mest av artiklarna som jag har skriftligt, så, om jag önskar till förhöjning sidofältet den är kritisk att figurera ut, som föreställer går att vara för bred till passformen i den nya designen.

Var lätt för att mig ska skriva en liten applikation för att göra detta, sedan I-förmiddagen en programmerare, det skulle, men gjorde den mig start tänkande… varför inte kan jag göra denna på Linuxen befaller fodrar?

Det första tinget utklurad I var att PNG avbildar skärm storleksanpassadatan, när du kör ”sparar” befaller på dem:

$ sparar image3.png
image3.png: PNG avbildar data, 613 x 657, 8 bet/färgar RGBA som är non-interlaced

Mycket användbart, sedan 99% av föreställa på denna plats är i PNG, formatera. Så nu att kasta det i en kretsa för alla sparar i mitt uploadarkiv:

$ för f i *.png; spara $f; gjort

image.png: PNG avbildar data, 631 x 185, 8 bet/färgar RGBA som är non-interlaced
image1.png: PNG avbildar data, 631 x 96, 8 bet/färgar RGBA som är non-interlaced
image10.png: PNG avbildar data, 375 x 395, 8 bet/färgar RGBA som är non-interlaced
image11.png: PNG avbildar data, 484 x 241, 8 bet/färgar RGBA som är non-interlaced
- klipp

Detta är mer användbar, men jag skulle måste handtag datan in i Excel eller en liknande applikation för att sortera datan, så jag avgjorde att använda ”den klippta” linuxen befaller till handtag ut precis breddkolonnen.

Du ska märker - parametern f5 berättar snittet att ta den femte kolonnen, och - Det \ med ett utrymme, efter det har berättat snittet att använda ett utrymme som delimiteren. Hugga \ teckenet är ett flykttecken som berättar beskjuta att använda utrymmet som ett tecken och inte som whitespace.

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

631
631
375
484
—snipped—

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.

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 [ ]

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

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

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:

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

image.png
image1.png
image17.png
image18.png
—snipped—

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.

| More
This article was originally written on 12/20/07 Tagged with: Linux

Daily Email Updates

You can get our how-to articles in your inbox each day for free. Just enter your name and email below:


Name:
Email:

Comments (8)

  1. Victor

    Damn those commands look complicated,

    And then people wonder why linux is so unpopular with the *normal* people…

    Just my 2 cents.

    (linux user btw)

  2. The Geek

    Victor,

    I certainly wouldn’t advocate this type of thing for “normal” people =)

  3. Phillip C Donald Sr

    Thanks I needed that. I am trying to learn linux.

  4. Dennis

    Any idea how to do this with jpg’s?

  5. The Geek

    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.

    Alternatively you could probably write a php, python or perl script and interface with the gd library.

  6. Jeff

    wow…
    You should put it into the convert command (imagemagick) to scale it if it’s bigger

  7. Daniel

    Nice. This is a perfect example of the power of the Linux shell. It may look comlicated, but after you get over the learning curve, you can do things that are incredibly powerful.

    Thanks for the example,

    Daniel

    daniel1992.wordpress.com

  8. Artem Russakovskii

    ImageMagick’s identify should be able to identify any files you have enabled while compiling it. For example,

    identify ceaec3da6ef432af59fbdad2c93c277e.jpg
    ceaec3da6ef432af59fbdad2c93c277e.jpg JPEG 160×90 160×90+0+0 DirectClass 8-bit 10.1797kb


Our Friends
Getting Started


About How-To Geek
What Is That Process?
svchost.exe
jusched.exe
dwm.exe
ctfmon.exe
wmpnetwk.exe
wmpnscfg.exe
rundll32.exe
wfcrun32.exe
Ipoint.exe
Itype.exe
Wfica32.exe
Mobsync.exe
conhost.exe
Dpupdchk.exe Adobe_Updater.exe

Copyright © 2006-2009 HowToGeek.com. All Rights Reserved.