Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to batch convert images between formats UNIX
To recursively batch convert images from one format to another using AppleScript Image Events, create the following shell script:
#!/bin/bash
# A simple bash script that uses AppleScript and ImageEvents to
# recursively transverse a directory structure and convert 
# between image formats

if [ ${#} -ne 3 ]
then
  echo "Image conversion script using Apple Image Events."
  echo ""
  echo "    Usage: ${0} [DIR] [file ext] [jpeg2|tiff|jpeg|pict|bmp|psd|png]"
  echo ""
  exit;
fi
if [ "${1}" != "-convert" ]
then
  echo ""
  echo "starting in: $1"
  echo "getting all the $2 files to convert to $3"
  find $1 -name "*.$2" -exec ./$0 -convert {} $3 ;
  exit 1
fi

jpg_file="$2"
pict_file="${2%%.*}.${3}"

if [ -e "${pict_file}" ]
then
  rm $pict_file
  echo "deleted old pict"
fi

echo "converting ${jpg_file} to ${pict_file}"
osascript
Remember to make the script executable.

[robg adds: I haven't tested this one, and as with anything that modifies a file, make sure you have a backup of whatever you're feeding to the script, just in case...]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[11,863 views]  

A script to batch convert images between formats | 13 comments | Create New Account
Click here to return to the 'A script to batch convert images between formats' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to batch convert images between formats
Authored by: baltwo on Oct 08, '04 04:00:15PM

Maybe I'm a bit brain dead this morning, but where are the calls to ImageEvents? This seems incomplete to me.



[ Reply to This | # ]
A script to batch convert images between formats
Authored by: mug1 on Oct 08, '04 04:09:51PM

or use GraphicConverter.... batch convert folders, filters etc who can live without gc?



[ Reply to This | # ]
A script to batch convert images between formats
Authored by: billbarstad on Oct 09, '04 01:59:50PM

Yeah, if I could figure out how to use the friggin program!



[ Reply to This | # ]
A script to batch convert images between formats
Authored by: worker201 on Oct 08, '04 05:05:11PM

It looks like this only changes the filename and extension. There's more of a difference between a jpeg and a tiff than just name.



[ Reply to This | # ]
A script to batch convert images between formats
Authored by: amdxp on Oct 09, '04 01:10:40AM

well if u see the code it say convert, my understanding is that it actually converts it and not simply change the file extenstions

cheers mate!



[ Reply to This | # ]
A script to batch convert images between formats
Authored by: sjmills on Oct 08, '04 05:24:25PM

Well, it just ends with "osascript". I bet there should've been more after it, but it got truncated when being sent/submitted/posted.



[ Reply to This | # ]
Try ImageMagick
Authored by: Chas on Oct 08, '04 07:33:57PM

ImageMagick works great for conversions. Command line utility, I installed it with Fink.

http://www.imagemagick.org/

Or maybe I used Mark Liyanage's installer, I forget.

http://www.entropy.ch/software/macosx/welcome.html#imagemagick

Very fast, flexible, and free.



[ Reply to This | # ]
A script to batch convert images between formats
Authored by: robspychala on Oct 09, '04 02:55:20PM
The Apple script calls got truncated from the posted file. Here is a repost.

#!/bin/bash
# A simple bash script that uses Applescript to convert between 
# image formats

if [ ${#} -ne 3 ]
then
    echo "Image conversion script using Apple Image Events."
    echo ""
    echo "    Usage: ${0} [DIR] [file ext] [jpeg2|tiff|jpeg|pict|bmp|psd|png]"
    echo ""
    exit;
fi
if [ "${1}" != "-convert" ]
then
    echo ""
    echo "starting in: $1"
    echo "getting all the $2 files to convert to $3"
    find $1 -name "*.$2" -exec ./$0 -convert {} $3 \;
    exit 1
fi

pwd=`pwd`
jpg_file="$2"
pict_file="${2%%.*}.${3}"

# deletes the previously converted file
if [ -e "${pict_file}" ]
then
    rm $pict_file
    echo "deleted old ${3}"
fi

echo "converting ${jpg_file} to ${pict_file}"
osascript<<EOF
tell application "Image Events"
	activate
	-- open the image file
	set this_image to open file "$jpg_file" as alias
	-- save in new file
	save this_image as ${3} in file "$pict_file"
	-- purge the open image data
	close this_image
end tell
EOF


[ Reply to This | # ]
A script to batch convert images between formats
Authored by: Chiwo on Oct 09, '04 07:34:18PM

Looks to me like this script will only work if it's in the current directory, and won't work on any files with spaces or other unusual characters in their names, or in directories with spaces in their names. May I suggest a structure more like this sketch:


find "$1" -type f -name "*.$2" -print | while read file
do
  dest="`dirname "$file"`/`basename "$file" ".$2"`"
  rm -f "$dest"
  osascript etc. etc.
done
Still not bombproof but more likely to work.



[ Reply to This | # ]
Use the 'sips' shell command
Authored by: kenahoo on Oct 09, '04 05:59:35PM
There's a /usr/bin/sips program in Panther that provides an interface to the "Image Events" suite but avoids the need to work with apple events. Unfortunately there's no manual page for it, but you can get a decent help message like so:

% sips --help
sips 1.0 - scriptable image processing system.
This tool is used to query or modify raster image files and ColorSync ICC profiles.
Its functionality can also be used through the "Image Events" AppleScript suite.

  ... more usage info follows ...
-Ken

[ Reply to This | # ]
Use the 'sips' shell command
Authored by: fxt on Oct 09, '04 10:09:33PM

to convert a pdf to a png file

% sips -s format png somefile.pdf --out somefile.png

wrap as need be (e.g., a foreach loop, or some other input/output format).

fxt



[ Reply to This | # ]
Use the 'sips' shell command
Authored by: tarjinderk on Oct 29, '05 02:26:01AM

Hi
which version of MAC support sip command
MAC-OS-10.3,10.2,10.1


Regard
Tarjinder



[ Reply to This | # ]
Just use folder actions
Authored by: NrTal on Oct 12, '04 10:01:45AM

Among standard Folder actions are a couple of scripts along the lines of "Duplicate as JPEG", "Duplicate as TIFF", etc. Tie these to an image folder and images put in will automatically be converted and put in a folder "JPEG images" or whatever as they are added to the folder.

About the only caveat is that for some reason they left off converting from PDF, so they don't work on screenshots. This can be fixed by adding "PDF" to the property type_list and adding "pdf" to the property extension_list. Further, if you want the original immediately removed change about 1/3 in from:

tell application "Finder"
my resolve_conflicts(this_item, originals_folder, "")
set the new_name to my resolve_conflicts(this_item, results_folder, newimage_extension)
set the source_file to (move this_item to the originals_folder with replacing) as alias
end tell
to:
tell application "Finder"
my resolve_conflicts(this_item, originals_folder, "")
set the new_name to my resolve_conflicts(this_item, results_folder, newimage_extension)
set the source_file to (move this_item to the originals_folder with replacing) as alias
move source_file to trash -- new delete command!
end tell

While the PDF changes seem a no brainer, I created a new script "Image - Move as JPEG" once I added the trashing line, so if it ever came up I would still have the original non-destructive script.

(If that's hard to follow, let me know and I'll try to get it properly formatted when I have more time.)



[ Reply to This | # ]