40

I have a folder of images over 4MB - let's call this folder dsc_big/. I'd like to use convert -define jpeg:extent=2MB to convert them to under 2MB and copy dsc_big/* to a folder dsc_small/ that already exists.

I tried convert dsc_big/* -define jpeg:extent=2MB dsc_small/ but that produces images called -0, -1, and so on.

What do I do?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
tekknolagi
  • 10,663
  • 24
  • 75
  • 119

6 Answers6

52

convert is designed to handle a single input file as far as I can tell, although I have to admit I don't understand the output you're getting. mogrify is better suited for batch processing in the following style:

mogrify -path ../dsc_small -define jpeg:extent=2MB dsc_big/*

But honestly I consider it dangerous for general usage (it'll overwrite the original images if you forget that -path) so I always use convert coupled with a for loop for this:

for file in dsc_big/*; do convert $file -define jpeg:extent=2MB dsc_small/`basename $file`; done

The basename call isn't necessary if you're processing files in the current directory.

Eduardo Ivanec
  • 11,668
  • 2
  • 39
  • 42
  • Also how can I do this with `mogrify`? – tekknolagi Jan 03 '12 at 04:48
  • 1
    ImageMagick 6.8.0 can handle muliple files with convert: `convert *.jpg *.png`. However I don't know how to handle the output file names, because ImageMagick just set the name to a sequential number (i.e.:1.png, 2.png, 3.png, and so on) – Guillermo Gutiérrez Sep 12 '13 at 16:30
  • 1
    The "-path" in mogrify seems to have been deprecated. An updated example would be great. http://www.imagemagick.org/script/mogrify.php – ian Sep 01 '14 at 23:54
5

This was the command which helped me after a long try. I wanted to make same sized thumbnails from a big list of large images which have variable width and height . It was for creating a gallery page.

convert -define jpeg:size=250x200 *.jpg  -thumbnail 250x200^ -gravity center -extent 250x200  crop/thumbnail-%d.jpeg

I got re-sized thumbnails which all having same width and height. :) thanks to ImageMagick.

sajin tm
  • 323
  • 3
  • 9
3

Here's a solution without using for loops on the console

convert *.jpeg -define jpeg:extent=2MB -set filename:f '../dsc_small/%t_small.%e' +adjoin '%[filename:f]'
slm
  • 15,396
  • 12
  • 109
  • 124
Nacho
  • 89
  • 7
2

Although this is an old question, but I'm adding this response for the benefit of anyone else that stumbles upon this.

I had the same exact issue, and being discouraged by the use of mogrify, I wrote a small Python based utility called easymagick to make this process easier while internally using the convert command.

Please note, this is still a work in progress. I'll appreciate any kind of feedback I can get.

Danish
  • 3,708
  • 5
  • 29
  • 48
1

I found that cd-ing into the desired folder, and then using the bash global variable $PWD made my convert not throw any errors. I'm utilizing ImageMagick's recently implemented caption: http://www.imagemagick.org/Usage/text/#caption function to label my images with the base filename and place them in another directory within the first.

cd ~/person/photos
mkdir labeled
for f in $PWD/*.JPG; do
    width=$(identify -format %w $f)
    filename=$(basename $f .JPG)
convert -background '#0008' -colorspace transparent -fill white -gravity center -size ${width}x100  caption:"${filename}" ${f} +swap -gravity south -composite "$PWD/labeled/${filename}.jpg";
done
  • Consider using `"$f"` rather than `$f` in the lines starting `width=...` and `filename=...` in case there are filenames with spaces in them. – Mark Setchell Dec 16 '15 at 00:56
0

This works for me

convert -rotate 90 *.png rotate/image.jpg 

produces image-0.jpg, image-1.jpg, image-2.jpg ..... in the 'rotate' folder. Don't know of a way to preserve the original filenames though.

Khawar
  • 1
  • 1