MiniMagick (TIFF)
| Language | Ruby |
|---|---|
| License | MIT |
| Repository | github.com/minimagick/minimagick |
| Documentation | README, ImageMagick TIFF options |
| Depends on | ImageMagick 7 (or 6) command-line tools |
| Read / write | yes / yes, everything ImageMagick supports: multi-page, CMYK, 16-bit, BigTIFF (TIFF64:), all common compressions |
MiniMagick runs ImageMagick’s magick command in a subprocess and parses the output. It uses little Ruby memory and supports every TIFF feature of ImageMagick, which reads and writes TIFF through libtiff. Each operation starts a process, so it’s slower than ruby-vips for many small operations.
Installation#
Shell
# install ImageMagick first
sudo apt install imagemagick # Debian / Ubuntu
brew install imagemagick # macOS
gem install mini_magick
Basic usage: convert a TIFF to PNG#
Ruby
require "mini_magick"
image = MiniMagick::Image.open("in.tif") # works on a temporary copy
puts "#{image.width} x #{image.height}, #{image.type}"
image.format("png") # converts the first page; image.format("png", 2) converts page 3
image.write("out.png")
How to save a TIFF with compression#
Ruby
MiniMagick.convert do |convert|
convert << "in.png"
convert.compress("LZW") # also Zip, JPEG, Group4, RLE, Zstd, None
convert << "out.tif"
end
How to split a multi-page TIFF into pages#
Ruby
image = MiniMagick::Image.open("scan.tif")
puts "#{image.pages.size} pages"
image.pages.each_with_index do |page, i|
page.write("page-#{i}.png")
end
How to merge images into a multi-page TIFF#
Ruby
MiniMagick.convert do |convert|
convert << "page1.png" << "page2.png" << "page3.png"
convert.compress("LZW")
convert << "pages.tif"
end
How to read TIFF properties#
ImageMagick exposes TIFF tags as tiff:* properties:
Ruby
image = MiniMagick::Image.new("in.tif") # reads in place, no copy
puts image["%[tiff:software]"]
puts image["%[tiff:photometric]"]
puts image["%[compression]"]
puts image.resolution.inspect # [x, y] DPI
Tips#
- Write BigTIFF by prefixing the output:
convert << "TIFF64:out.tif". MiniMagick::Image.new(path)modifies the file in place;MiniMagick::Image.openworks on a copy.