LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Image Magick - Resize pictures in all subdirectories (https://www.linuxquestions.org/questions/linux-newbie-8/image-magick-resize-pictures-in-all-subdirectories-804354/)

McDuck 04-26-2010 03:30 PM

Image Magick - Resize pictures in all subdirectories
 
Heey guys,

I'd like to resize a lot of pictures which are in subdirectories. all the pictures are stored in /home/username/Pictures

With Image Magick I can resize a single picture by:
Code:

convert -size 1024 somefile.jpg -resize 1024 ./newsize1024/somefile1024.jpg
But now I want to resize all the pictures in current and all subdirectories without doing it manually.

Can u guys help me out with a command, bash script, c code or some other code? (i prefer the easiest way)

thnx in advance!
McDuck

Additional:
can the solution contain the solution to resize the picture to the highest dimension?
currently:
convert (from the image magick package) has a -resize option. this option can only resize to the maximum width, eg:
Code:

convert -resize 1024 somefile.jpg
with picture size: 2048x1572 to 1024x786
but with picture size: 1572x2048 to 1024x1334

desired:
which would output: 1024x786 and 786x1024

business_kid 04-26-2010 03:47 PM

Let me mention a typo - 1024x768 is the usual size.

Unless you have a bundle of pictures, you might be quicker doing it by hand.

use find (man find) which descends subdirs [ find <options> |xargs do stuff

and go chase gimp scripts (script-fu) to see if they can automate some of the messing.

colucix 04-26-2010 04:00 PM

Quote:

Originally Posted by McDuck (Post 3948487)
can the solution contain the solution to resize the picture to the highest dimension?

Yes. Specify both dimensions for -resize:
Code:

convert -resize 1024x1024 input.jpg output.jpg
in this way aspect ratio is still preserved, but you will get the desired size:
Code:

2048x1572 --> 1024x786
1572x2048 --> 786x1024

Regarding the recursion, follow the suggestion by business_kid and try find out. A simple while loop in bash should do the trick.

McDuck 04-26-2010 04:10 PM

Thnx for the fast replies!

Quote:

Originally Posted by business_kid (Post 3948507)
Let me mention a typo - 1024x768 is the usual size.

geh3

Quote:

Originally Posted by business_kid (Post 3948507)
Unless you have a bundle of pictures, you might be quicker doing it by hand.

Thats exactly my problem ;-)

Quote:

Originally Posted by business_kid (Post 3948507)
use find (man find) which descends subdirs [ find <options> |xargs do stuff

I tried that option, but i wasnt being able convert working with find. i got stuck the point that convert does need 2 filename. 1 original, and 1 resized.

Quote:

Originally Posted by colucix (Post 3948526)
Yes. Specify both dimensions for -resize:
Code:

convert -resize 1024x1024 input.jpg output.jpg
in this way aspect ratio is still preserved, but you will get the desired size:
Code:

2048x1572 --> 1024x786
1572x2048 --> 786x1024


Exactly what i was looking for =) prob didnt understood the man right.

Quote:

Originally Posted by colucix (Post 3948526)
Regarding the recursion, follow the suggestion by business_kid and try find out. A simple while loop in bash should do the trick.

bash with recursion?

colucix 04-26-2010 04:29 PM

Quote:

Originally Posted by McDuck (Post 3948543)
bash with recursion?

I mean to descend recursively into a directory tree. Let me do an example:
Code:

while read file
do
  convert -resize 1024x1024 "$file" "${file/.jpg/1024.jpg}
done < <(find . -name \*.jpg)

This uses process substitution to feed the while loop (you can refine the find comman to match your search criteria) and substring replacement in parameter substitution to build the name of the output file.

This creates the new file in the same directory as the original file. If you want all the new files in one specific location (as in your example) first you have to extract the file name. Here is an example using substring removal:
Code:

while read file
do
  oufile="${file##*/}"
  convert -resize 1024x1024 "$file" "./newsize1024/${oufile/.jpg/1024.jpg}"
done < <(find command here)

Ah, just noticed this is your first post here... welcome to LinuxQuestions! :)


All times are GMT -5. The time now is 04:41 AM.