How to Reduce Image Size in Java

How to Reduce Image Size in Java

This image size reducer library offers enterprise-level compression techniques to compress images. In fact, you can customize your API calls per your business requirements using various compression types and levels provided by this rich-featured library. Therefore, let’s set up this library in our Java project and build an online image compressor. This tutorial will teach us how to reduce image size in Java programmatically.

The following points will be covered:

Image size reducer library installation

The installation of any library matters when it comes to the rapid application development. Anyway, you can download the JAR file or install this library using the following Maven configurations.

    <name>Aspose Java API</name>

Reduce PNG image size programmatically

There is a huge stack of methods offered by this library that you may find here. In addition, you may set the color, compression type and many other attributes of images programmatically.

Please follow the following steps and the code snippet to compress PNG images:

  1. Load the source PNG image file by calling the Image.load method.
  2. Loop over possible CompressionLevel range.
  3. Create an instance of PngOptions class for each resultant PNG to access the PNG options.
  4. Set CompressionLevel. Whereas, The png image compression level in the 0-9 range, where 9 is maximum compression and 0 is store mode.
  5. Save results on disk by calling the save method.

Copy & paste the following code into your main file:

Compress TIFF images in Java

Likewise, this library lets you build an online image compressor where you can reduce size of TIFF image files using a few lines of Java code.

You may follow the steps mentioned below:

  1. Invoke the image.load method to load an image through file path location or stream.
  2. Create an instance of TiffOptions for the resultant image.
  3. Set BitsPerSample, Photometric mode & Compression mode by calling the setBitsPerSample method.
  4. Call the setCompression method to set the compression.
  5. Invoke the setPhotometric method to set the photometric.
  6. Set grayscale palette by calling the setPalette method.
  7. Save the output.

You can see the output in the image below:

Compress TIFF images

Get a Free License

You can avail a free temporary license to try the API without evaluation limitations.

Summing up

This brings us to the end of this tutorial. You have learned how to reduce image size in Java along with some other important topics. Moreover, you have gone through how to compress TIFF images and reduce PNG image size programmatically. This blog post will really help you build an online image compressor using a few lines of source code. Finally, do not skip the documentation in case you are to build an image size reducer for your business application.

Moreover, we suggest you follow our Getting Started guide.

Finally, is writing new blog posts. So, please stay in touch for regular updates.

Ask a question

You can let us know about your questions or queries on our forum.


How do I resize an image in coding?

Install this Java image processing library to resize images programmatically. It offers methods to manipulate images using a few lines of source code in Java.

See Also