MiniMagick
| Language | Ruby |
|---|---|
| License | MIT |
| Repository | github.com/minimagick/minimagick |
| Documentation | README, ImageMagick BMP options |
| Depends on | ImageMagick 7 (or 6) command-line tools |
| Read / write | yes, all common variants including RLE and OS/2 / yes, BMP2, BMP3, V4/V5, 16-bit subtypes |
MiniMagick builds ImageMagick commands and runs them as separate processes, so it supports every BMP feature of ImageMagick: reading RLE, bit field and OS/2 files, and writing 1 to 32-bit BMP with the header version you choose.
ImageMagick’s default BMP writer uses a V4 or V5 header when the image has alpha, gamma or a color profile, which PNG and JPEG files often have. Some old programs and devices only read the 40-byte header: write with the BMP3: prefix for them. Each MiniMagick operation starts a process; batch steps with combine_options or MiniMagick.convert.
Installation#
# ImageMagick first
sudo apt install imagemagick # Debian / Ubuntu
brew install imagemagick # macOS
# then
gem install mini_magick
Basic usage: open, resize and save a BMP#
require "mini_magick"
image = MiniMagick::Image.open("in.bmp")
puts "#{image.width} x #{image.height}, #{image.type}" # type: "BMP" or "BMP3"
image.resize "50%"
image.write "out.bmp"
How to convert PNG to BMP for old software#
Flatten transparency and force the 40-byte Windows 3 header with BMP3::
MiniMagick.convert do |convert|
convert << "in.png"
convert.background "white"
convert.flatten
convert.type "TrueColor" # 24-bit, no palette
convert << "BMP3:out.bmp"
end
How to write a 16-bit RGB565 BMP#
Small LCD displays and microcontrollers often expect 16-bit 5-6-5 pixels:
MiniMagick.convert do |convert|
convert << "in.png"
convert.define "bmp:subtype=RGB565"
convert << "out565.bmp"
end
How to write an 8-bit palette BMP#
MiniMagick.convert do |convert|
convert << "in.png"
convert.colors 256
convert.type "Palette"
convert << "BMP3:palette.bmp"
end
How to convert BMP to PNG#
image = MiniMagick::Image.open("in.bmp")
image.format "png" # converts the temporary copy
image.write "out.png"
How to read BMP details#
data returns ImageMagick’s JSON description, including compression and bit depth:
image = MiniMagick::Image.new("in.bmp")
info = image.data
puts info["compression"], info["depth"], info["type"]
Tips#
magick identify -verbose in.bmpshows the same details from the command line, useful to debug a file a program refuses.image.valid?returnsfalsefor truncated or corrupt files.- Set
MiniMagick.configure { |config| config.timeout = 30 }when you process uploads.