LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-01-2012, 02:32 PM   #16
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled

ok... Tell me if Im understanding whats going on.

Code:
maxsize="3000000"
you are declaring the variable maxsize with a value of 2M

Code:
while read -r width height fname
Im lost here. I understand its a simple do while loop but Im lost with the read -r width height fname. read is read but whats -r doing exactly??? I thought -r was read, too. Im stuck with what fname is doing are, are you running the whole line and setting it as a variable fname?

Code:
((width*height>=maxsize)) && echo "$fname"
you are doing "this line" and the width*height>=maxsize AND echoing the variable fname. it will do...... How are you declaring the variable fname??

Code:
 < <(identify -format "%w %h %f\n" *jpg)
im stuck with whats going on with < <. Im understanding that the identity format is what is used with ImageMagick and %w and %h are pulling the width and height. Whats %f doing? \n prints a new line and *jpg is all images.

Code:
>> results.csv
prints to CSV file with the name results


EDIT:
how would you also extend *jpg to also check for other file types like .png and .jpeg

Last edited by graphicsmanx1; 11-01-2012 at 03:08 PM.
 
Old 11-01-2012, 05:04 PM   #17
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
FYI.. if anyone reads this to be able to do all types of image formats you dont need a comparision operator it just needs to look like this:

Code:
(identify -format "%w %h %f\n"  *png *jpg *jpeg *tif *gif *bmp)>> results.csv
 
Old 11-01-2012, 06:20 PM   #18
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Code:
#!/bin/bash

maxsize="2000000"
while read -r width height fname; do 
    ((width*height>maxsize)) && echo "$fname";
done < <(identify -format "%w %h %f\n" *jpg)
Ok. The variable declaration is clear. To explain the rest, I would, if I may, start from the end:

Code:
identify -format "%w %h %f\n" *jpg
This is the command to get the size of the image, as proposed by druuna. I took the liberty of slightly rearranging the output format. I put the width and height to the front because they will never contain spaces. So I know that the first word is width, the second one is height and the rest of the line is filename.
Now, the <( ) is bash process substitution. It makes the output of the command inside look like a file to the shell script. The < in front then redirects this "file" to the while loop.

Code:
read -r width height fname
this part in the loop will read the lines one at a time, so that first word of each line will be stored in variable width, the second one in height and the rest of the line (the file name) will go to fname. -r means that read will not interpret backslashes as metacharacters.

Code:
((width*height>=maxsize)) && echo "$fname"
Here we have the bash arithmetic (( )). Whatever is inside will be evaluated as a mathematical expression. So, it computes width * height and does the comparison. The result of the operation is either true or false.
&& is the bash 'and' operator. It runs the second command only if the first command was successful (that is, if width * height >= maxsize is true. Alternatively, you could write
Code:
if ((width*height>=maxsize)); then
    echo "$fname"
fi
To achieve the same effect.
That's it. If you have any questions, feel free to ask.

Last edited by millgates; 11-02-2012 at 10:05 AM.
 
1 members found this post helpful.
Old 11-02-2012, 09:42 AM   #19
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
for some reason the process substitution is throwing me off and Im trying to look at it from a simplified manner.
 
Old 11-02-2012, 10:06 AM   #20
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by graphicsmanx1 View Post
for some reason the process substitution is throwing me off and Im trying to look at it from a simplified manner.
Sorry, a typo in the link. It should work now
 
Old 11-06-2012, 03:41 PM   #21
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
im sorry can you explain how this would be done with a simple loop?? Im still lost in understanding how the read and process substitution is working. That is throwing me off. So the statement is starting with

Code:
(identify -format "%w %h %f\n" *jpg)
from what I am reading.
 
Old 11-07-2012, 02:12 AM   #22
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Well, that was a simple loop. But if I did this without read and process substitution, it could look like this:

Code:
#!/bin/bash

# define the max size of the image
maxsize="2000000"

# iterate over all jpg images in the working dir
for image in *.jpg; do

    # use identify to get the size in format "width height"
    size="$(identify -format "%w %h" "$image")"

    # extract width from the string by 'cutting off' everything
    # from the first space on (or up to the first space(see string manipulations)
    width="${size%% *}"

    # extract height
    height="${size##* }" 

    # use bash math to compare the numbers
    if ((width*height>maxsize)); then
        echo "$fname";
    fi
done

Last edited by millgates; 11-07-2012 at 02:14 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with my Dell Dimensions 2400 pc jberry46 Linux - Newbie 3 03-19-2008 09:12 PM
need video dimensions shanenin Programming 6 11-21-2005 06:54 PM
Coldfusion - getting image dimensions Locura Programming 2 02-15-2005 04:52 PM
Java arrays and dimensions pycoucou Programming 5 04-07-2004 01:59 PM
terminal dimensions and lilo nautilus_1987 Linux - General 1 10-22-2003 01:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration