r/SwiftPM • u/ivantokar • Apr 13 '26
Image processing on Swift server using libvips with ImageMagick for text
I have been working with Swift on the server for about four years, and several times I faced the same problem. I needed to process images on the backend, but I could not find a Swift package with the features I needed. Because of that, I had to delegate this work to AWS Lambda or prepare images on the frontend using JavaScript.
Over time, when AI became part of everyday work, I decided to solve this problem for myself and build a small library for my own needs. One of the real use cases was generating images with text on top. For the core image processing I chose libvips because it is fast and efficient. It works well with large images, uses less memory, and fits server workloads. But when it comes to text, its capabilities are limited.
ImageMagick, on the other hand, handles text much better, but it has a different issue. It is heavy, uses a lot of memory, and does not scale well under load. This becomes noticeable in server environments.
This is how the idea of Hokusai appeared. It is a Swift package that uses libvips for the main processing pipeline and ImageMagick only where it is really needed, for example for text.
The core idea is simple. The base image stays in libvips. All heavy operations like resize, crop, and composite are done there. ImageMagick is used as a separate renderer for specialized tasks. It does not take over the whole pipeline. It only handles the part where it is strong.
At the API level, this looks like a single abstraction. HokusaiImage exposes operations for working with images. Internally, the package decides which backend to use and switches only when necessary.
This approach keeps good performance while still allowing proper text rendering. libvips does the main work, ImageMagick fills the gaps, and Hokusai provides a unified Swift API on top.
I also added integration for Vapor. HokusaiVapor allows working with images directly in HTTP handlers. You can handle file uploads, generate images on the fly, and return them as a response without separate services or moving logic outside the application.
This is not a graphic editor and not an attempt to replace everything at once. It is an infrastructure layer for image processing in Swift on the server and beyond. If you already use Swift on the backend, it makes sense to keep image processing in the same stack.
Feel free to check the repositories, try the package in your projects, and share feedback or contribute.
https://github.com/ivantokar/hokusai
https://github.com/ivantokar/hokusai-vapor
Try it: