converting a directory of TIF to JPG

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
flieckster
Posts: 2
Joined: 2017-04-26T06:01:35-07:00
Authentication code: 1151

converting a directory of TIF to JPG

Post by flieckster » 2017-04-26T07:23:54-07:00

Hi all, i'm super noob at IM, and i'm just trying to convert a directory of images.

Code: Select all

cd /Users/flieckb/Desktop/from 
list=`ls *.tif`
echo "$list"
for img in $list; do
convert "$img" jpg "/Users/flieckb/Desktop/to/$img"
done
The above code simply copys the TIFF's to the 2nd directory without actually converting to JPG. what am I doing wrong?

I tired this also.

Code: Select all

cd /Users/flieckb/Desktop/from 
list=`ls *.tif`
echo "$list"
for img in $list; do
mogrify -path /Users/flieckb/Desktop/to/ -format jpg
done

snibgo
Posts: 9404
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: converting a directory of TIF to JPG

Post by snibgo » 2017-04-26T07:35:24-07:00

Both of your IM commands are wrong. I suggest you experiment, with just one file, to understand the commands.

Code: Select all

convert "abc.tif" "/Users/flieckb/Desktop/to/abc.jpg"
This^ reads a tif file, and writes it as a jpg to the directory.

Code: Select all

mogrify -path /Users/flieckb/Desktop/to/ -format jpg "abc.tif"
This^ does the same.

Code: Select all

mogrify -path /Users/flieckb/Desktop/to/ -format jpg "*.tif"
This^ does the same, for all *.tif.
snibgo's IM pages: im.snibgo.com

flieckster
Posts: 2
Joined: 2017-04-26T06:01:35-07:00
Authentication code: 1151

Re: converting a directory of TIF to JPG

Post by flieckster » 2017-04-26T07:48:25-07:00

ahh thank you that makes sense. i could convert one file, but it it would change the name to "*.jpg" and stop. the last bit of code seems to work great. i'll have to pick that apart more and play with it. thank you very much.

snibgo
Posts: 9404
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: converting a directory of TIF to JPG

Post by snibgo » 2017-04-26T08:16:59-07:00

Yes, "convert" needs an input filename and output filename. The output filename would need to end with ".jpg".

"convert" usually operates on one file per invocation, so needs to be in a shell loop.

For this job, "mogrify" is the obvious tool. It needs an input filename (often with wildcard). IM will build each output filename from the path, the name of the input file, and the format.

"mogrify" can process one file per invocation, inside a shell loop. Or you can use wildcards, so there is no need for a shell loop.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 22105
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: converting a directory of TIF to JPG

Post by fmw42 » 2017-04-26T09:17:41-07:00


merkthere
Posts: 1
Joined: 2017-08-14T00:47:32-07:00
Authentication code: 1151

Re: converting a directory of TIF to JPG

Post by merkthere » 2017-08-14T00:49:05-07:00

Install imagemagick $ sudo apt-get install imagemagick

It's simplest usage is: $ convert File.tif File.jpg

It is smart and goes by your file extension.

Now, for doing batch conversions, we shall use a loop.

cd into the directory where your tif files are.

then: $ for f in *.tif; do echo "Converting $f"; convert "$f" "$(basename "$f" .tif).jpg"; done

Post Reply