Skip to content

Quickly Reducing Image File Sizes

I recently had a folder full of jpg and png images that I wanted to reduce in size (the file sizes, not the image dimensions). There were about 850 images totaling about 56MB. I did a quick Google search and found OS X Preview could change the image dimensions pretty easily and maybe even save the files in a different format, but not if they were different formats to start with.

But silly me, this is the perfect thing for Xojo. And in fact, it only took 10 lines of code. I just created a new desktop project and dragged a button onto the Window. In its Action event I added this simple code:

  // Prompt for folder containing pictures
 Dim f As FolderItem = SelectFolder
 If f Is Nil Then Return
 // Create new folder to create resized pictures
 Dim newFolder As FolderItem = f.Parent.Child("ResizedPictures")
 // Go through each original picture, resize it
 // and save it to the new folder
 For i As Integer = 1 To f.Count
   Dim newPicFile As FolderItem = newFolder.Child(f.Item(i).Name.Replace(".png", ".jpg"))
   Dim p As Picture = Picture.Open(f.Item(i))
   p.Save(newPicFile, Picture.SaveAsJPEG, Picture.QualityHigh)
 MsgBox("Pictures converted to high-quality JPEG.")

If you don’t count the comments, that’s just 10 lines. This code simply loops through the folder and opens each file as a Picture and then re-saves it to a new folder as a high-quality jpg. There’s no error handling, so the original folder better only contain pictures, but in my case I only needed to do this one time and I knew the folder only had pictures.

Running this code on the folder took a couple seconds and the folder size dropped to 25MB with no noticeable loss in the quality of the images (they were all pretty small) and also removed a few duplicates as well.

Making this Xojo app took me less time than I spent than searching Google and the App Store!

Watch the 2-minute video to see how easy it is:


  1. \”There’s no error handling, so the original folder better only contain pictures, but in my case I only needed to do this one time and I knew the folder only had pictures.\”

    That is, of course, unless you opened the pictures folder in the OS X Finder and it created a hidden .DS_Store file in it, which it usually does.

    Not even adding a simple check for even that common case is not a good way to teach examples to new users. Any example coming from Xojo should make an effort to teach people proper exercises, because Xojo users are usually those with even less experience than your common Xcode user (who usually do this by trade or at least learned it in school, whereas Xojo claims to make it easy even for newbies).

    If you leave every such error checking as an \”exercise\” for the inexperienced user, you\’re not helping them, you\’re rather frustrating them. Just look at the forum, finding many questions related to the simplest cases of nil checking that users don\’t understand.

    For those who read this and wonder what I\’m talking about…

    1. Usually, when getting the FolderItem from a directory (folder) using \”f.Item(i)\”, that result should be checked for nil – which may indicate that the file is there but not accessible. This is a rare case, but it\’s safer to check for that if you\’re writing code you ever want to run as a built app and even give it away to others.

    2. In the above code example by Paul, the easiest way to catch all those possible errors is to add a try/catch block inside the loop, like this:

    Dim newPicFile As FolderItem = newFolder.Child(f.Item(i).Name.Replace(“.png”, “.jpg”))
    Dim p As Picture = Picture.Open(f.Item(i))
    p.Save(newPicFile, Picture.SaveAsJPEG, Picture.QualityHigh)
    Catch exc as RuntimeException
    ‘ ignore any errors
    End Try

    • Paul Lefebvre Paul Lefebvre

      Thanks, Thomas. Exception handling is a great way to easily check for any errors during this processing.

    • Thomas Thomas

      Unfortunately, my code did not come across well. All those quote chars came prependd with an \”\\\”, which need to be removed again if someone copies my example.. Also, I wrote RuntimeErrror but meant RuntimeException.

      • Paul Lefebvre Paul Lefebvre

        Code doesn’t seem to format quite right when in comments. I’ve corrected the formatting as best I can.

  2. Edwin van den Akker Edwin van den Akker

    Great idea. I had some \”Automator\” script, that did this for me. But through Xojo I have more control. I build one app myself, based on your code. But I added the error handling and a progress bar.
    Also, I added a label that shows the status message, including the amount of size reduction.

    Thanks Paul!