DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Compressing JPG Images with Groovy

Compressing JPG Images with Groovy

Dustin Marx user avatar by
Dustin Marx
·
Dec. 01, 11 · Interview
Like (0)
Save
Tweet
Share
10.52K Views

Join the DZone community and get the full member experience.

Join For Free

I recently had need to reduce the size of a large number of JPG images. I did not want to do this one at a time in a graphics manipulation tool, so scripting seemed like the obvious choice. These days, Groovy is my generally my preferred tool for scripting jobs. I noted that Eric Wendelin had posted Crush images on the command-line with Groovy that demonstrates using Groovy in conjunction with ImageMagick to "[compress] images the Groovy way." This looks like an easy and attractive way to do it, but it requires ImageMagick and, if on Windows, Cygwin. I decided to write a similar script using straight Java and Groovy to see if I could reduce required dependencies. Note that this script would have been more concise had I used the Java Advanced Imaging API, but that would have meant another separate dependency.

rescaleJpgImage.groovy
import java.awt.Image
import java.awt.image.BufferedImage
import java.awt.image.RenderedImage
import javax.imageio.IIOImage
import javax.imageio.ImageIO
import javax.imageio.ImageWriteParam
import javax.imageio.ImageWriter
import javax.imageio.stream.FileImageOutputStream

if (args.length < 2)
{
   println "USAGE: groovy rescaleImage.groovy <sourceDirectory> <scalingFactor>\n"
   println "\twhere <sourceDirectory> is directory from which image files come"
   println "\t      <scalingFactor> is scaling factor for reduced image (0 to 1)"
   System.exit(-1)
}

def directoryPath = args[0]
def directory = new File(directoryPath)
if (!directory.isDirectory())
{
   println "${directoryPath} is NOT an existing directory!"
   System.exit(-1)
}

def scaleFactor = args[1] as float
def iter = ImageIO.getImageWritersByFormatName("jpeg")
def writer = (ImageWriter)iter.next()
def imageWriteParameters = writer.getDefaultWriteParam()
imageWriteParameters.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
imageWriteParameters.setCompressionQuality(scaleFactor);

def backupDirName = "backup_" + System.currentTimeMillis()
def targetDirectory = new File(backupDirName)
targetDirectory.mkdir()
def targetDirectoryPath = targetDirectory.canonicalPath
directory.eachFile
{ file ->
   def fullFileName = file.canonicalPath
   def fileName = file.name
   def sourceImage = ImageIO.read(new File(fullFileName))
   def targetName = targetDirectoryPath + File.separator + fileName
   println "Copying ${fullFileName} to ${targetName} with scale factor of ${scaleFactor} ..."
   def targetFile = new File(targetName);
   def output = new FileImageOutputStream(targetFile);
   writer.setOutput(output);
   def image = new IIOImage(sourceImage, null, null);
   writer.write(null, image, imageWriteParameters);
}
writer.dispose();

The Groovy script above does not change the input image files, but instead re-writes them with the same name to a new directory. The new directory is named by concatenating the milliseconds since epoch time after "backup_". The files are written as JPG files and are scaled by the provided scaling factor.

This script was sufficient for my needs, but there are many ways it could be improved. Groovy's built-in CLI-based command-line support could be used for nicer handling of command-line options. There could be better checks to verify that the provided scaling factor is a numeral between 0 and 1. This script also assumes that all images provided to be compressed are already JPEG format. Using libraries such as Java Advanced Imaging API, ImageMagick, or imgscalr might have made the script even more concise.

Compression Results

UPDATE: This section added to original post based on feedback from Eric Wendelin (see below). The following screen snapshot attempts to show compression results in terms of image file sizes. The leftmost files are the original files. The middle set of files were generated by running the above script against the original files with a scaling factor of 0.5. The rightmost set of files are the result of running the script against the original files with a scaling factor of 0.25.

As a final part of comparing the compression results, I include an image taken during the week in San Francisco for JavaOne 2011 in all three forms below. The first is the original image (3.62 MB), the second is the image generated with scale factor of 0.5 (482 KB) and the third is the image generated with scale factor of 0.25 (326 KB).

All three versions of the Fisherman's Wharf image above, including the original, are JPEG format. One of the downsides of the script above is that this format is assumed. The next two screen snapshots show what happens when the earlier PNG image comparing file sizes is run through this script.

Adding the ability to handle other image formats as input files is where a separate image manipulation library such as ImageMagick might be particularly useful.

Conclusion

This is another example of how Groovy combines the best of scripting with the feature-rich libraries of Java. The above script could have been written in Java, but Groovy seems a better fit for scripts like this one.

 

From http://marxsoftware.blogspot.com/2011/11/compressing-jpg-images-with-groovy.html

Groovy (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Distributed Tracing: A Full Guide
  • Data Stream Using Apache Kafka and Camel Application
  • Benefits and Challenges of Multi-Cloud Integration
  • Effective Jira Test Management

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: