LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How about a program to mass-resize jpegs/pngs? (https://www.linuxquestions.org/questions/linux-software-2/how-about-a-program-to-mass-resize-jpegs-pngs-796781/)

Raveolution 03-20-2010 09:01 PM

How about a program to mass-resize jpegs/pngs?
 
Is there a way to take a whole directory of pictures that are of various dimensions and scale them all down to conform to, say, an 800x600 (or 600x800) boundary?

Better yet is it possible to also ignore and not size-up files that are, say, 400x300?

I am command line savvy so if it can be done via some kind of script I'm cool with that. Thanks!

rikijpn 03-20-2010 09:26 PM

gm
 
how about a script using convert?
http://www.linuxquestions.org/questi...ut-how-655591/

raju.mopidevi 03-20-2010 10:10 PM

You can convert them by using GWENVIEW. The resize option does it easy for you !

Raveolution 03-20-2010 10:43 PM

Quote:

Originally Posted by rikijpn (Post 3906146)

I like gwenview!
But I would like to use convert, too - I don't see a way for convert to intelligently scale the pic like Gwenview does.

Has anyone here used irfanview's batch resizer? I'm trying to replace that.

evo2 03-21-2010 01:06 AM

Quote:

Originally Posted by Raveolution (Post 3906189)
But I would like to use convert, too - I don't see a way for convert to intelligently scale the pic like Gwenview does.

I think that's the job of an intelligently written script. What exactly do you want to do?

Evo2.

rikijpn 03-21-2010 01:47 AM

Quote:

Originally Posted by Raveolution (Post 3906189)
I like gwenview!
But I would like to use convert, too - I don't see a way for convert to intelligently scale the pic like Gwenview does.

I don't understand what you mean by intelligent here. It would help if you'd explain more what you like about gwenview or irfanview's batch resizer, for all of us who don't know them. You can easily check all convert/gm's features by reading its manual.
Again, I don't know gwenview, but if it can do the job (change scale of all pics in a directory, right?) then just use whatever is easier for you.

It does sound more like something a command/script rather than a GUI would do better though. I'd write a script using "convert -resize 800x600" (GraphicsMagick package) to resize the image, and "identify" to check its resolution just in case I wanted to skip some.
All the rest would maybe be a couple of sed or cut sentences.

GrapefruiTgirl 03-21-2010 03:08 AM

Code:

find ./ -maxdepth 1 -exec basename "{}" \; | while read picture; do
  if [ ! "$(identify "$picture" | awk '{print $3}')" = '400x300' ]; then
    xdim=$(identify "$picture" | awk '{print $3}' | awk -Fx '{print $1}')
    ydim=$(identify "$picture" | awk '{print $3}' | awk -Fx '{print $2}')
    [ $xdim -gt $ydim ] && resize='800x' || resize='x800'
    mogrify -scale "$resize" "$picture"
  fi
done

The above resizes a directory full of pictures. It ignores pictures whose size is already 400x300, and the rest of them it resizes so their largest dimension becomes 800, thereby leaving you with a load of pictures that are no larger than 800x??? or ???x800

This uses ImageMagick tools `mogrify` and `identify`, and there are other mogrify options besides "scale" (such as "resize") that you may want to use instead; depends how you want the pictures to turn out.

Your mileage might vary -- Back up your pictures first! Test this on a bunch of spare pictures!!

Sasha

Raveolution 03-21-2010 08:42 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 3906292)
Code:

etc.

Wicked sick. That works famously. I'm testing it on a few scenarios to see if it works in all cases.

GrapefruiTgirl 03-21-2010 08:44 PM

LOL ;) hopefully 'wicked sick' == 'good' and not 'argh! it formatted my HDD!' :p

Good luck,

Sasha

Raveolution 03-22-2010 01:03 AM

Quote:

Originally Posted by GrapefruiTgirl (Post 3907061)
LOL ;) hopefully 'wicked sick' == 'good' and not 'argh! it formatted my HDD!' :p

Good luck,

Sasha

Nope. Probably because of Murphy's Law - I tested it on a few pics on a memory stick.

Wicked Sick == it worked! :D

Raveolution 03-22-2010 01:45 AM

Okay here's a bonus question... how to modify that command to do the same thing across several subdirectories (assuming I ever need that function)?

evo2 03-22-2010 02:29 AM

Just omit the "-maxdepth 1" option or change the "1" to however deep you want to go in the directory structure.

Evo2.

GrapefruiTgirl 03-22-2010 02:43 AM

Actually, since I used `basename` you'll have to remove that as well. Remove all of:

Code:

-maxdepth 1 -exec basename "{}" \;
and try that. Note that this code as it is, assumes that all files in these subdirs are pictures (there's not really error handling to speak of), AND that the directories you want to search are all subdirs of the dir you are working in. If there are directories all over the machine, i.e. located in other paths besides the one in which you are running the script, you'll want to add some more code to search specific paths.

Raveolution 03-22-2010 06:38 AM

Quote:

Originally Posted by GrapefruiTgirl (Post 3907261)
Actually, since I used `basename` you'll have to remove that as well. Remove all of:

Code:

-maxdepth 1 -exec basename "{}" \;
and try that. Note that this code as it is, assumes that all files in these subdirs are pictures (there's not really error handling to speak of), AND that the directories you want to search are all subdirs of the dir you are working in. If there are directories all over the machine, i.e. located in other paths besides the one in which you are running the script, you'll want to add some more code to search specific paths.

Jeez. I dig the voodoo that you do so well!
Seriously though, where'd you learn to do this? I've done a mean script or two myself and this is pretty advanced stuff.

GrapefruiTgirl 03-22-2010 06:12 PM

Ahh, no voodoo involved, and honestly, this really isn't very high-tech, but I'm happy that it works for you.

Where to learn this stuff? Just practice and fiddle around. :) I enjoy scripting -- and lots of other folks do too (though you will also have lots of people tell you to not bother, and to learn another language, depending on what exactly you're trying to do), and there are definitely others around here who are far more advanced scripters than I am. Basically, when you want or need something done, and you don't know C or python or Perl or <blah> or <whatever> other language, and you're determined to do it with a shell script, then just try and try again. I never set out on a mission to "learn scripting inside and out" -- so I don't know the half of it -- but I know how to do what I've needed to learn how to do.

Eventually you end up knowing lots of neat little things, by playing around..

Cheers,

Sasha


All times are GMT -5. The time now is 07:30 AM.