MiniMagick
| Language | Ruby |
|---|---|
| License | MIT (needs ImageMagick: ImageMagick License) |
| Repository | github.com/minimagick/minimagick |
| Documentation | README |
| Depends on | ImageMagick 7 (or 6) command-line tools |
| Read / write | yes / yes, all TGA image types, raw or RLE (via ImageMagick) |
MiniMagick builds and runs ImageMagick commands (magick, identify) from Ruby. It doesn’t load images into Ruby memory, so it works with big files and many formats, TGA included. Each call starts a process, so batch several operations with combine_options or MiniMagick.convert.
ImageMagick must be installed on the machine. It identifies TGA by the .tga extension, since TGA has no magic bytes.
Installation#
Shell
# ImageMagick itself
sudo apt install imagemagick # Debian / Ubuntu
brew install imagemagick # macOS
# the gem
gem install mini_magick
Basic usage: convert TGA to PNG#
Ruby
require "mini_magick"
image = MiniMagick::Image.open("in.tga") # works on a temporary copy
puts "#{image.width} x #{image.height}, #{image.type}"
image.format "png"
image.write "out.png"
How to convert PNG to TGA with RLE compression#
Ruby
require "mini_magick"
MiniMagick.convert do |convert|
convert << "in.png"
convert.compress "RLE" # "None" for raw pixels
convert << "out.tga"
end
How to get TGA width, height and alpha#
Ruby
image = MiniMagick::Image.new("in.tga") # no copy, reads with identify
puts image.dimensions.inspect # [512, 512]
puts image["%[channels]"] # e.g. "srgba"
How to resize a TGA texture#
Ruby
image = MiniMagick::Image.open("in.tga")
image.combine_options do |b|
b.resize "50%"
b.compress "RLE"
end
image.write "half.tga"
How to read a TGA file that has another extension#
Prefix the path with the format so ImageMagick knows how to decode it:
Ruby
MiniMagick.convert do |convert|
convert << "TGA:texture.bin"
convert << "out.png"
end
Tips#
- In a Rails app, Active Storage variants use MiniMagick (or ruby-vips) under the hood; TGA uploads work when ImageMagick is the processor.
image.datareturns ImageMagick’s full JSON description, including the TGA compression and orientation.