Image Processing in Kotlin: Resize and Crop with ImageMagick Library

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Images in Kotlin: Resizing and Cropping (Resize & Crop)



In modern web development and mobile application creation with Kotlin, there is often a need to programmatically process images: reduce size for optimized loading, crop excess edges, or prepare previews. In this article, we will explore a powerful tool for working with graphics — the ImageMagick library (via the Java wrapper JMagick), which is ideal for resize and crop tasks in Kotlin.



What are ImageMagick and JMagick?

ImageMagick is a cross-platform open-source software suite for creating, editing, and converting raster images. It supports over 200 formats (JPEG, PNG, GIF, WebP, TIFF, and others). To work with ImageMagick from Java/Kotlin, the JMagick library is used, providing a Java Native Interface (JNI) to ImageMagick functions.



Advantages of using ImageMagick in Kotlin:

  • High performance (native C/C++ code)
  • Rich functionality: resize, crop, rotate, filters, format conversion
  • Support for streaming processing of large images
  • Precise quality control during compression


Installation and Setup

To work with JMagick, you need to install native ImageMagick on your system. Then add the dependency in build.gradle.kts (for Gradle):



dependencies {    implementation("org.im4java:im4java:1.4.0") // Alternative wrapper    // Or use JMagick:    implementation("org.jmagick:jmagick:6.6.9")}


Note: JMagick requires ImageMagick version 6.x (not 7) to be installed. For simplicity, in the examples we will use im4java — a more modern wrapper that does not require JNI.



Resizing an Image (Resize)

The resize operation changes the dimensions of an image while preserving proportions (or not). In im4java, this is done via the ConvertCmd class.



Example: reduce the image to a width of 300 pixels, preserving proportions:



import org.im4java.core.ConvertCmdimport org.im4java.core.IMOperation

fun resizeImage(inputPath: String, outputPath: String, targetWidth: Int) { val cmd = ConvertCmd() val op = IMOperation()

op.addImage(inputPath) op.resize(targetWidth, null) // null = auto-height based on proportion op.addImage(outputPath)

cmd.run(op)}

fun main() { resizeImage("input.jpg", "output_resized.jpg", 300) println("Image resized to width 300px")}


Resize parameters:

  • resize(width, height) — if one parameter is null, proportions are preserved
  • resize(width, height, "^") — minimum area fill
  • resize(width, height, "!") — forced resize without preserving proportions


Cropping an Image (Crop)

Crop cuts out a rectangular area from the image. The coordinates of the top-left corner and the dimensions of the area are specified.



Example: cut out a central square of size 200x200 pixels:



import org.im4java.core.ConvertCmdimport org.im4java.core.IMOperation

fun cropImage(inputPath: String, outputPath: String, x: Int, y: Int, width: Int, height: Int) { val cmd = ConvertCmd() val op = IMOperation()

op.addImage(inputPath) op.crop(width, height, x, y) op.addImage(outputPath)

cmd.run(op)}

fun main() { // Cut out area: top-left corner (100,50), size 400x300 cropImage("input.jpg", "output_cropped.jpg", 100, 50, 400, 300) println("Image cropped")}


For automatic centering, you can calculate the coordinates:



                
    
    

Recommendations