You are not logged in.

#1 2010-01-16 14:55:49

xen0blade
Member
Registered: 2009-11-06
Posts: 8

Sorting images by resolution in Bash

I'm trying to develop a shell script to sort(?) images by resolution, and I'm stuck on something.  This is what I have:

#!/bin/bash
echo "What resolution would you like to select? ex. 1280x800"
read RES
echo "The resolution you have selected is $res"
echo "Removing Spaces from filenames"
rename -v 's/\ /\_/g' *
echo "Making list of files..."
ls *.jpg > filelist.txt
echo "Identifying images that match desired resolution..."
identify $(cat filelist.txt) > modlist.txt
echo "Grepping..."
grep $RES modlist.txt > final.txt

And the problem starts there.  In order to move the desired images, I need to remove the extraneous information from the lines, leaving only the file name.  I've done quite a bit of googling about this, and can't find a sed or awk or anything to fit my needs.  Is this possible?

Basically, I need to modify this:

autumn-sky.jpg[94] JPEG 1440x900 1440x900+0+0 8-bit DirectClass 1.212MB 0.010u 0:00.000

into this:

autumn-sky.jpg

Thanks in advance...


Looking for the author of Haxial KDX.
Email me:  xen0systems [at] gmail.com

Offline

#2 2010-01-16 15:26:55

daneel971
Member
Registered: 2008-03-28
Posts: 197

Re: Sorting images by resolution in Bash

edit - nevermind. I misread your post

Last edited by daneel971 (2010-01-16 15:37:14)

Offline

#3 2010-01-16 16:13:12

linkmaster03
Member
Registered: 2008-12-27
Posts: 269

Re: Sorting images by resolution in Bash

cut -d "[" -f1 works for me. Is that what you are looking for?

$ echo "autumn-sky.jpg[94] JPEG 1440x900 1440x900+0+0 8-bit DirectClass 1.212MB 0.010u 0:00.000" | cut -d "[" -f1
autumn-sky.jpg

Offline

#4 2010-01-16 17:08:38

Bralkein
Member
Registered: 2004-10-26
Posts: 354

Re: Sorting images by resolution in Bash

I guess you really mean filter by resolution, rather than sort, am I right? The cut example given by linkmaster03 is okay but it won't work if any of the files have a [ in the filename. You could try doing it by sed, this way works for your example:

sed -r 's/([^.]*\..{3,4})\[.*/\1/'

If this doesn't quite work for some of the lines then you should be able to modify it, the basic idea is there anyway.

Offline

#5 2010-01-16 17:13:43

xen0blade
Member
Registered: 2009-11-06
Posts: 8

Re: Sorting images by resolution in Bash

Thanks!  I don't know sed and awk yet sad   ...Yet....


Looking for the author of Haxial KDX.
Email me:  xen0systems [at] gmail.com

Offline

#6 2010-01-16 17:34:08

xen0blade
Member
Registered: 2009-11-06
Posts: 8

Re: Sorting images by resolution in Bash

Are there some magical spaces that aren't spaces in filenames that I should know about???  If there aren't, then why are some spaces remaning in filenames after I pass

rename -v 's/\ /\_/g' *

. It's never failed me before, or is that a fluke too....:?

oh, and the Code so far:

#!/bin/bash
echo "What resolution would you like to select? ex. 1280x800"
read RES
echo "The resolution you have selected is $res"
echo "Removing Spaces from filenames"
rename -v 's/\ /\_/g' *
echo "Making list of files..."
ls *.jpg > filelist.txt
echo "Identifying images that match desired resolution..."
identify $(cat filelist.txt) > modlist.txt
echo "Grepping..."
grep $RES modlist.txt > final.txt
cat final.txt | sed -r 's/([^.]*\..{3,4})\[.*/\1/' > final2.txt
mkdir RezzedImages
mv $(cat final2.txt) RezzedImages
rm -rf *.txt
echo "Done!"

Last edited by xen0blade (2010-01-16 17:36:19)


Looking for the author of Haxial KDX.
Email me:  xen0systems [at] gmail.com

Offline

#7 2010-01-16 18:12:56

Bralkein
Member
Registered: 2004-10-26
Posts: 354

Re: Sorting images by resolution in Bash

Is rename even supposed to take a regular expression like that? Also you seem to have the regex enclosed in backticks, which will cause bash to try and do command substitution.

Anyway, does this do what you need?

rename ' ' _ *

Edit: Also, if I remember correctly giving a command a very long list of arguments will sometimes fail to work if the list is too long. For situations like that, you ought to use xargs. For example, I think you should probably change

identify $(cat filelist.txt) > modlist.txt

to

xargs identify < filelist.txt > modlist.txt

So that it won't break if filelist.txt contains very many entries. I might be wrong though.

Edit 2: Oh no, according to Wikipedia you can pass arbitrarily long argument lists since kernel 2.6.23. We live and learn, don't we?

Edit 3: Regarding the rename problem, see this relevant forum topic: http://bbs.archlinux.org/viewtopic.php?id=85128

Last edited by Bralkein (2010-01-16 19:24:40)

Offline

#8 2010-01-17 00:48:21

scj
Member
From: Sweden
Registered: 2007-09-23
Posts: 158

Re: Sorting images by resolution in Bash

feh is a pretty nice cli image viewer that can be used for all kinds of stuff. Being able to specify the output format is pretty handy if you want to maintain filename spaces.

#!/bin/bash
echo "What resolution would you like to select? ex. 1280x800"
read RES

IFS='\n'
mkdir rezzed
for img in $(feh -L "%wx%h %n" *.jpg | grep $RES | cut -d ' ' -f 2-); do
  mv -v "$img" rezzed
done

Last edited by scj (2010-01-17 00:49:33)

Offline

#9 2010-01-17 12:04:40

xen0blade
Member
Registered: 2009-11-06
Posts: 8

Re: Sorting images by resolution in Bash

Awesome....Absolute geniuses...whatever would I do without you people? smile


Looking for the author of Haxial KDX.
Email me:  xen0systems [at] gmail.com

Offline

#10 2010-01-21 19:00:34

xen0blade
Member
Registered: 2009-11-06
Posts: 8

Re: Sorting images by resolution in Bash

Still debugging (A lack of time really impedes this project o' mine...)...


Looking for the author of Haxial KDX.
Email me:  xen0systems [at] gmail.com

Offline

Board footer

Powered by FluxBB