ruby-vips
| Language | Ruby |
|---|---|
| License | MIT (libvips: LGPL-2.1) |
| Repository | github.com/libvips/ruby-vips |
| Documentation | ruby-vips on rubydoc, jxlsave options |
| Depends on | libvips built with libjxl |
| Read / write | yes / yes, lossy and lossless, 8/16-bit and float, animation |
ruby-vips binds libvips through FFI, so the gem has no C extension to compile, but libvips must be installed on the system. libvips streams images in pieces, which makes it fast and light on memory. JPEG XL support comes from libjxl and is present in Homebrew’s vips and the libvips packages of current Linux distributions; check with vips -l | grep jxl.
Installation#
Shell
sudo apt install libvips42 # Debian / Ubuntu
brew install vips # macOS
gem install ruby-vips
Basic usage: convert JXL to PNG and back#
Ruby
require "vips"
image = Vips::Image.new_from_file("in.jxl")
puts "#{image.width} x #{image.height}, #{image.bands} bands, #{image.format}"
image.write_to_file("out.png")
photo = Vips::Image.new_from_file("photo.png")
photo.write_to_file("photo.jxl", Q: 90)
How to save a lossless JPEG XL#
Ruby
image = Vips::Image.new_from_file("screenshot.png")
image.jxlsave("screenshot.jxl", lossless: true)
How to set distance and effort#
Ruby
image.jxlsave("photo.jxl",
distance: 1.0, # 0 lossless, 1.0 visually lossless (default), up to 15
effort: 7) # 1-9: higher is slower and smaller
How to make a JXL thumbnail#
Ruby
thumb = Vips::Image.thumbnail("big.jxl", 400) # fit in 400 x 400
thumb.write_to_file("thumb.jpg", Q: 85)
How to convert to JXL in memory#
Ruby
image = Vips::Image.new_from_buffer(File.binread("photo.jpg"), "")
jxl_bytes = image.write_to_buffer(".jxl", Q: 85)
How to read metadata#
Ruby
image = Vips::Image.new_from_file("in.jxl")
image.get_fields.each { |name| puts name }
exif = image.get("exif-data") if image.get_typeof("exif-data") != 0
Tips#
- In Rails, Active Storage uses ruby-vips via
image_processing:image.variant(resize_to_limit: [800, 800], format: :jxl). - libvips decodes JPEG input to pixels; for lossless JPEG recompression use
cjxl.