Working with Images in Kotlin: Libraries and Tools

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: Library Overview and Practical Examples



Kotlin, being a modern programming language fully compatible with Java, provides developers with access to the entire ecosystem of libraries for working with images. In this article, we will review the main tools available in Kotlin for loading, processing, transforming, and saving images. You will learn how to use standard Java capabilities (AWT, Swing) and specialized libraries to solve common tasks.



Standard Java AWT and Swing Tools



The basic functionality for working with images in Kotlin (as in Java) is provided by the java.awt and javax.imageio packages. Main classes:



  • java.awt.image.BufferedImage — represents a raster image with pixel access.
  • java.awt.Graphics2D — provides methods for drawing (lines, shapes, text) on an image.
  • javax.imageio.ImageIO — static methods for reading and writing images in various formats (PNG, JPEG, BMP, GIF).
  • java.awt.Color — working with colors.
  • java.awt.image.RescaleOp, java.awt.image.ConvolveOp — filters for changing brightness, contrast, blur.


Example 1: Loading, resizing, and saving an image



import java.awt.image.BufferedImageimport java.awt.Graphics2Dimport java.awt.RenderingHintsimport java.io.Fileimport javax.imageio.ImageIO

fun resizeImage(inputPath: String, outputPath: String, targetWidth: Int, targetHeight: Int) { // Load the original image val originalImage: BufferedImage = ImageIO.read(File(inputPath))

// Create a new image of the specified size val resizedImage = BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB) val graphics: Graphics2D = resizedImage.createGraphics()

// Configure resampling quality graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR) graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY) graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)

// Draw the scaled image graphics.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null) graphics.dispose()

// Save the result ImageIO.write(resizedImage, "jpg", File(outputPath)) println("Image saved: $outputPath")}

// Example callfun main() { resizeImage("input.jpg", "output_resized.jpg", 800, 600)}


Thumbnailator Library



Thumbnailator is a popular library for Java/Kotlin that greatly simplifies creating thumbnails and resizing images. Its main advantage is a simple fluent API.



Adding dependency (Gradle):

implementation("net.coobird:thumbnailator:0.4.20")


Example 2: Creating a thumbnail using Thumbnailator



import net.coobird.thumbnailator.Thumbnailsimport java.io.File

fun createThumbnail(inputPath: String, outputPath: String, size: Int) { Thumbnails.of(File(inputPath)) .size(size, size) // square thumbnail .keepAspectRatio(true) // preserve aspect ratio .outputFormat("png") .toFile(File(outputPath)) println("Thumbnail created: $outputPath")}

fun main() { createThumbnail("photo.jpg", "thumb.png", 150)}


The library also supports cropping, rotation, adding watermarks, and working with streams.



OpenCV Library for Kotlin



For serious computer image processing (face recognition, filtering, contour analysis), the

Recommendations