Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,000 hints and counting!

Drag-and-drop script to quickly resize any image System
My friend (and primary author of the two Unix chapters in the new Woof Book) Kirk McElhearn was looking for a simple way to resize images for his Kirkville website. Although there are probably 1,500 apps out there that resize images, none seemed to do exactly what he wanted: drag an image onto an app, have it scale it to a predefined size, then save the file with a new name. He didn't want to have to launch Photoshop or Graphic Converter, nor specify the parameters every time; he just wanted a fast and easy resizer to his chosen dimensions (120 pixels wide by the required scale length).

Being the AppleScript expert that I am (ha!), I quickly chose the "phone a real expert" option and sent an email to Sal Soghoian, who was kind enough to respond with some very elegant resizing code. I then took a shot at adding the requisite file handling bits, but again my abilities with AppleScript turned Sal's work into a muddled mess. So I again deferred to an expert. This time, it was Doug Adams, of Doug's AppleScripts for iTunes fame (300+ free iTunes AppleScripts). Between the two of them, the end result was a very effective AppleScript. Read the rest of the hint for the details...

Open Script Editor, and create the following new script:
-- save in Script Editor as Application
-- drag files to its icon in Finder

on open some_items
  repeat with this_item in some_items
    try
      rescale_and_save(this_item)
    end try
  end repeat
end open


to rescale_and_save(this_item)
  tell application "Image Events"
    launch
    set the target_width to 120
    -- open the image file
    set this_image to open this_item
    
    set typ to this_image's file type
    
    copy dimensions of this_image to {current_width, current_height}
    if current_width is greater than current_height then
      scale this_image to size target_width
    else
      -- figure out new height
      -- y2 = (y1 * x2) / x1
      set the new_height to (current_height * target_width) / current_width
      scale this_image to size new_height
    end if
    
    tell application "Finder" to set new_item to ¬
    (container of this_item as string) & "scaled." & (name of this_item)
    save this_image in new_item as typ
    
  end tell
end rescale_and_save
Save this as an application, and then place its icon somewhere easy to reach (dock, desktop, whatever). Drag and drop an image, or a number of images, onto the icon, and a few moments later, you'll have a file named scaled.Original_File_Name sitting in the same folder as the source image(s). It doesn't get much easier than that!

You can, of course, pick another width other than 120 pixels (just change the noted line in the source). With a bit more work, you could have this script prompt for the width, or set the scale based on height targets, etc. Thanks to Sal and Doug for this very useful script; I'll actually use it a fair bit when I just have a simple resize need!
    •    
  • Currently 5.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[78,448 views]  

Drag-and-drop script to quickly resize any image | 27 comments | Create New Account
Click here to return to the 'Drag-and-drop script to quickly resize any image' hint
The following comments are owned by whomever posted them. This site is not responsible for what they say.
Drag-and-drop script to quickly resize any image
Authored by: kirkmc on Tue, Sep 28 2004 at 11:04AM PDT
My first solution had been to use the sips command in Terminal: see this article: http://www.mcelhearn.com/article.php?story=20040924085258924

The droplet solution is even better, at least if I don't have Terminal open.

Kirk

---
Read my blog: Kirkville -- http://www.mcelhearn.com
Musings, Opinion and Miscellanea, on Macs, iPods and more

[ Reply to This | # ]
Star! Options Dialogs?
Authored by: willbank on Tue, Sep 28 2004 at 11:09AM PDT
Fabulously neat - and cd be used in conjunction with something like Quicksilver to really speed processing (as I have a huge library of full res images but I get asked to email snaps to people and have to resize etc etc).

As Rob mentioned - is there a chance that another script guru could write two dialog pop-ups asking for preferred width (perhaps with the 'Use Last' button highlighted so hitting the return key would kick the default settings into action) and then another to specify the location to be saved, or send it straight to Mail, or another option, again, 'Use Default' (which could be same location as original file as in the current script)?

Just what I have needed - great work. Thanks to all who helped with this.

---
No, really. This is a healthy obsession.

[ Reply to This | # ]

Star! Options Dialogs?
Authored by: selasley on Tue, Sep 28 2004 at 5:28PM PDT
Here's my attempt at a version that prompts for width and save location and keeps those values between runs.

-- save in Script Editor as Application
-- drag files to its icon in Finder

property target_width : 120
property save_folder : ""

on open some_items
    -- do some set up
    tell application "Finder"
        -- get the target width, the default answer is the property target_width
        set new_width to text returned of ¬
            (display dialog "Target width:" default answer target_width ¬
            buttons {"OK"} default button "OK")
        if new_width as integer > 0 then
            set target_width to new_width
        end if
        -- if the save_folder property has not been set, 
        -- set it to the folder containing the original image
        if save_folder is "" then
            set save_folder to ¬
            (container of file (item 1 of some_items) as string)
        end if
        -- get the folder to save the scaled images in, 
        -- default folder is the property save_folder
        set temp_folder to ¬
            choose folder with prompt "Save scaled images in:" ¬
            default location alias save_folder
        set save_folder to temp_folder as string
    end tell
    -- loop through the images, scale them and save them 
    repeat with this_item in some_items
        try
            rescale_and_save(this_item)
        end try
    end repeat
    
    tell application "Image Events" to quit
end open


on rescale_and_save(this_item)
    tell application "Finder"
        set new_item to save_folder & "scaled." & (name of this_item)
    end tell
    
    tell application "Image Events"
        launch
        -- open the image file
        set this_image to open this_item
        set typ to this_image's file type
        copy dimensions of this_image to {current_width, current_height}
        scale this_image by factor (target_width / current_width)
        save this_image in new_item as typ
    end tell
end rescale_and_save


[ Reply to This | # ]
Star! Options Dialogs?
Authored by: willbank on Wed, Sep 29 2004 at 12:20PM PDT
Brilliant, brilliant, brilliant selasley. Thanks so much.

---
No, really. This is a healthy obsession.

[ Reply to This | # ]

Star! Options Dialogs?
Authored by: Bepe on Fri, Oct 1 2004 at 8:25AM PDT
Hello, great job this script !! I love this site :-) I had a prompt for prefix (scaled.) and translate into french. Here it is :

-- Redimensionne les images
-- par drag & drop
----

-- propriétés par défaut
-- Largeur 120 pixels
-- pas de dossier d'enregistrement
-- prefix des images : redim.nom_image.jpg
property target_width : 120
property save_folder : ""
property prefix : "redim"

-- handler du drag & drop
-- appelle la boucle principale : rescale_and_save
-- pour le ou les items glissés
on open some_items
	-- Réglages
	tell application "Finder"
		-- Obtenir la largeur souhaitée, la réponse par défaut est la  propriété de  target_width
		set new_width to text returned of ¬
			(display dialog "Largeur souhaitée:" default answer target_width ¬
				buttons {"OK"} default button "OK")
		if new_width as integer > 0 then
			set target_width to new_width
		end if
		-- Réglage du préfixe
		set prefix to text returned of ¬
			(display dialog "Préfixe des images redim :" default answer prefix ¬
				buttons {"OK"} default button "OK")
		
		-- Si la propriété save_folder n'est pas renseignée 
		-- la fixer au dossier contenant l'image originale
		if save_folder is "" then
			set save_folder to ¬
				(container of file (item 1 of some_items) as string)
		end if
		-- obtenir le nom du dossier dans lequel enregistrer les images redimenssionnées, 
		-- par défaut la propriété est dans save_folder
		set temp_folder to ¬
			choose folder with prompt ¬
				"Enregistrer les images dans:" default location alias save_folder
		set save_folder to temp_folder as string
	end tell
	-- boucler pour toutes les images, les redimensionner et les enregistrer 
	repeat with this_item in some_items
		try
			rescale_and_save(this_item)
		end try
	end repeat
	
	tell application "Image Events" to quit
end open


on rescale_and_save(this_item)
	tell application "Finder"
		set new_item to save_folder & prefix & "." & (name of this_item)
	end tell
	
	tell application "Image Events"
		launch
		-- Ouvir le fichier image
		set this_image to open this_item
		set typ to this_image's file type
		copy dimensions of this_image to {current_width, current_height}
		scale this_image by factor (target_width / current_width)
		save this_image in new_item as typ
	end tell
end rescale_and_save



[ Reply to This | # ]
Drag-and-drop script to quickly resize any image
Authored by: Black on Tue, Sep 28 2004 at 11:19AM PDT
I'm not going to pretty this up so it fits in the script, but if you have ImageMagick installed (say via fink), and you wanted to do this to a file called tmp.jpg, this would be:
convert -resize 120 tmp.jpg tmp.jpg
This would save you from having to do your own aspect ratio calculations...

[ Reply to This | # ]
Drag-and-drop script to quickly resize any image
Authored by: mania on Tue, Sep 28 2004 at 11:19AM PDT
well - another fantastic easy way to do this is to install imagemagick from www.imagemagick.org. you can then write bash or php or perl scripts to run the commands - so for example it allows you to write an image upload script that automatically makes thumbnail sized pics for the links and leaves bigger pics for what you zoom in on. i have done this on both linux and os x for a shopping cart program i wrote and also a web-based real estate MLS search system. imagemagick does a lot more than resizing - it has photoshop like capabilities and its free. check it out.

[ Reply to This | # ]
Resize within Imagemajick?
Authored by: rhowell on Tue, Sep 28 2004 at 12:42PM PDT
Any idea how to resize within Imagemajick? I know about convert..., but what about opening your image graphically in Imagemajick and resizing? For the life of me, I can't find it...

[ Reply to This | # ]
Resize within Imagemajick?
Authored by: koncept on Tue, Sep 28 2004 at 3:39PM PDT
use "display pathToImage" with X11 running...

[ Reply to This | # ]
Resize within Imagemajick?
Authored by: rhowell on Tue, Sep 28 2004 at 3:42PM PDT
Right, sorry, I fully understand how to open a graphics file in Imagemajick. What I don't know how to do is resize that picture within Imagemajick. Where is it in the menus?

[ Reply to This | # ]
Resize within Imagemajick?
Authored by: rkchang on Tue, Sep 28 2004 at 11:30PM PDT
I accidentally responded to this at the level above, but here it is again for clarification.

Imagemagick, as far as I know, is a set of command line utilities. To do a batch resize, you can use the following command:

mogrify -resize

My digital camera regularly takes photos at 1280x960 resolution, and I often have to resize them to 800x600, so I do so with this command:

mogrify -resize 800x600 *.jpg

---
"I have seen the evils of procrastination, and I vow to change my ways tomorrow."

[ Reply to This | # ]

Resize within Imagemajick?
Authored by: Black on Wed, Sep 29 2004 at 1:33PM PDT
Resize is under the View menu.

[ Reply to This | # ]
Drag-and-drop script to quickly resize any image
Authored by: Fofer on Tue, Sep 28 2004 at 1:19PM PDT
The freeware Resize! works pretty well too:
http://kstudio.net/re.html

[ Reply to This | # ]
Drag-and-drop script to quickly resize any image
Authored by: PeterDie on Tue, Sep 28 2004 at 6:48PM PDT
Ahw, c'mon. ThumbsUp 4.0 is the way to go. Kudos for trying to reinvent a wheel, but the resize / scale / compress / pre-suffix-folder /antialiasing /sharpening / quit when done-options are a few clicks away if you just startup the app without dropping anything on it, while you never see a dialog when you drop a bunch of pics on ThumbsUp. It just works!

Granted, two gripes; resize quality could be better. Not as good as Resize!
You cannot set arbitrary height with a give with. You can percentually re-scale or set with x height.

Contrary to Resize!, ThumbsUp shows no dialog. You can drop individual files on ThumbsUp, while Resize! requires dropping a folder

Another option: the contextual menu extension PhotoTools. Does a great job, too, but requires filling in width every time. Has other very handy features, though.

[ Reply to This | # ]
Drag-and-drop script to quickly resize any image
Authored by: rkchang on Tue, Sep 28 2004 at 11:21PM PDT
My only use with Imagemagick has been as a command line utility. To do a batch resize, I use the following code:

mogrify -resize

For example, my digital camera takes pictures at a 1280x960 resolution by default, and I've been too lazy to figure out how to adjust the resolution from within the camera, so once I transfer the photos to my hard drive, I navigate to the folder and resize to 800x600 with the following command:

mogrify -resize 800x600 *.jpg

---
"I have seen the evils of procrastination, and I vow to change my ways tomorrow."

[ Reply to This | # ]

Drag-and-drop script to quickly resize any image
Authored by: orpy on Wed, Sep 29 2004 at 2:45AM PDT
If you want to do something similar, but:

1. Control the resize on an image-by-image basis
2. Save time by dumping the image to the clipboard

You can use my app, ResizeImage 1.2:

http://www.macupdate.com/info.php/id/15980

It's all AppleScript and it's free...

---
--
Martin

[ Reply to This | # ]
Yet another ImageMagick wrapper
Authored by: tinb on Wed, Sep 29 2004 at 6:11AM PDT
Please have a look at TurboTool for just another way to create a Drag&Drop solution from a command line program, e.g. ImageMagick. On the website you'll find a "Quick Example" to resize an image. Not very easy to use but verrrry powerful.

Cheers from Robert, developer of TurboTool

[ Reply to This | # ]
Size is the problem
Authored by: pampa on Wed, Sep 29 2004 at 6:18AM PDT
The script does resizing brilliantly but it doesn't 'optimise' the GIF's. Resizing the 8K image resulted in 60K image. JPEG's are fine in my test.

[ Reply to This | # ]
How to properly deal with landscape vs. portrait..
Authored by: jiclark on Wed, Sep 29 2004 at 12:55PM PDT
...in other words, how do you get this to create images that are no more than, say, 120 pixels in their longest dimension, regardless of whether they're landscape or portrait oriented??

Is it possible?

[ Reply to This | # ]
How to properly deal with landscape vs. portrait..
Authored by: ngb on Thu, Sep 30 2004 at 2:23PM PDT
easiest way is to use "sips" from the command line:
% sips -Z 120 imagefile --out newimagefile
or
% sips -Z 120 imagefiles --out newimagefiledirectory


[ Reply to This | # ]
Scale and send images directly to mail app
Authored by: nilswerk on Wed, Sep 29 2004 at 7:09PM PDT
Hi everyone,

i just read this hint and the according posts (especially the one from willbank)
and i thought you might be interested in another alternative script.
I wrote it a few weeks ago when I was irritated about mailing images wich I made with
my digital camera. It is very similar to the script posted here.
What it does:
Drag one or more images on the droplet and you will be prompted to enter a percentage value
for scaling. After that, Mail opens and the images are posted directly into a new message window. That´s it.
During the process a folder named "temp_files_" will appear on your desktop.
It will be moved to the trash once the process is over. So the original files will remain untouched.
If you are interested you can download it from my .mac account.
http://homepage.mac.com/nilswerk/FileSharing7.html

Greetings,
nilswerk

[ Reply to This | # ]
Scale and send images directly to mail app
Authored by: willbank on Thu, Sep 30 2004 at 12:16PM PDT
You guys! You kill me with your genius :-)

Nice work - that is perfect. Scripts are obviously like buses: you wait years for one and two come along at once. My thanks again to nilswerk and this unique community.

---
No, really. This is a healthy obsession.

[ Reply to This | # ]

Drag-and-drop script to quickly resize any image
Authored by: hoosker on Fri, Oct 1 2004 at 8:51PM PDT
I use freePhotoConverter 2 (ver. 3 is available now I think). Very simple and fast. Ajustable % or actual dimension. Only complaint is photos are not as sharp as you can get in photoshop.

http://www.raggers.net/pathos/freephotoconverter.php

[ Reply to This | # ]
Drag-and-drop script to quickly resize any image
Authored by: edwardsx on Mon, Oct 11 2004 at 3:36PM PDT
I set this up on my powerbook and it works great. I tried it on my cube and powermac and it does NOT work. My friend put it on his power mac and it does not work on his machine. Does any one have a idea as what the problem is. I have checked everything that I can think of.

[ Reply to This | # ]
Image Resize Widget
Authored by: Ste3v3 on Wed, Aug 23 2006 at 1:51PM PDT
Just download Image Shackle. It is a drag and drop widget that resizes images quick and easily. http://www.apple.com/downloads/dashboard/developer/imageshackle.html

[ Reply to This | # ]
Drag-and-drop script to quickly resize any image
Authored by: madsmekker on Sun, May 10 2009 at 11:46PM PDT
Great script that can become very usefull for me!

Is there any way to tell finder to do this (instead of storing the picture in the same folder):
<blockquote>
If there is not a folder named "NETTSIDEBILDER" on the desktop,
make it,
then move the scaled image to this folder.
</blockquote>
Thanks alot :)

[ Reply to This | # ]
Drag-and-drop script to quickly resize any image
Authored by: ludojad on Tue, Jul 14 2009 at 2:32AM PDT
Here's quick script in bash which resizes images to the same size no matter if they are horizontal or vertical.
Remeber to set SIZE variable to desired size.


#!/bin/sh

# Call this script resizer.sh and use it this way:
# for i in ./*.JPG ; do resizer.sh $i ; done

SIZE=640
GETWIDTH=`sips --getProperty pixelWidth $1 | cut -s -d : -f 2 -`
GETHEIGHT=`sips --getProperty pixelHeight $1 | cut -s -d : -f 2 -`

# If image is horizontal then
if [ $GETWIDTH -gt $GETHEIGHT ] ; then
sips --resampleWidth $SIZE $1 2>&1>/dev/null
else
# Image is vertical
sips --resampleHeight $SIZE $1 2>&1>/dev/null
fi

[ Reply to This | # ]