https://github.com/minimagick/minimagick
mini replacement for RMagick
https://github.com/minimagick/minimagick
Keywords from Contributors
activerecord mvc activejob rubygems rack ruby-gem rspec crash-reporting mongodb-driver background-jobs
Last synced: about 3 hours ago
JSON representation
Repository metadata
mini replacement for RMagick
- Host: GitHub
- URL: https://github.com/minimagick/minimagick
- Owner: minimagick
- License: mit
- Created: 2008-03-06T01:35:38.000Z (almost 18 years ago)
- Default Branch: master
- Last Pushed: 2025-08-19T19:49:44.000Z (5 months ago)
- Last Synced: 2026-01-03T09:31:15.724Z (7 days ago)
- Language: Ruby
- Homepage: https://rubydoc.info/gems/mini_magick
- Size: 3.6 MB
- Stars: 2,859
- Watchers: 43
- Forks: 349
- Open Issues: 3
- Releases: 57
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: MIT-LICENSE
README.md
MiniMagick
A ruby wrapper for ImageMagick command line.
Why?
I was using RMagick and loving it, but it
was eating up huge amounts of memory. Even a simple script would use over 100MB
of RAM. On my local machine this wasn't a problem, but on my hosting server the
ruby apps would crash because of their 100MB memory limit.
Solution!
Using MiniMagick the ruby processes memory remains small (it spawns
ImageMagick's command line program mogrify which takes up some memory as well,
but is much smaller compared to RMagick). See Thinking of switching from
RMagick? below.
MiniMagick gives you access to all the command line options ImageMagick has
(found here).
Requirements
ImageMagick command-line tool has to be installed. You can check if you have it
installed by running
$ magick -version
Version: ImageMagick 7.1.1-33 Q16-HDRI aarch64 22263 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(5.0)
Delegates (built-in): bzlib fontconfig freetype gslib heic jng jp2 jpeg jxl lcms lqr ltdl lzma openexr png ps raw tiff webp xml zlib zstd
Compiler: gcc (4.2)
Installation
Add the gem to your Gemfile:
$ bundle add mini_magick
Information
Usage
Let's first see a basic example of resizing an image.
require "mini_magick"
image = MiniMagick::Image.open("input.jpg")
image.path #=> "/var/folders/k7/6zx6dx6x7ys3rv3srh0nyfj00000gn/T/magick20140921-75881-1yho3zc.jpg"
image.resize "100x100"
image.format "png"
image.write "output.png"
MiniMagick::Image.open makes a copy of the image, and further methods modify
that copy (the original stays untouched). We then
resize
the image, and write it to a file. The writing part is necessary because
the copy is just temporary, it gets garbage collected when we lose reference
to the image.
MiniMagick::Image.open also accepts URLs, and options passed in will be
forwarded to open-uri.
image = MiniMagick::Image.open("http://example.com/image.jpg")
image.contrast
image.write("from_internets.jpg")
On the other hand, if we want the original image to actually get modified,
we can use MiniMagick::Image.new.
image = MiniMagick::Image.new("input.jpg")
image.path #=> "input.jpg"
image.resize "100x100"
# Not calling #write, because it's not a copy
Combine options
While using methods like #resize directly is convenient, if we use more
methods in this way, it quickly becomes inefficient, because it calls the
command on each methods call. MiniMagick::Image#combine_options takes
multiple options and from them builds one single command.
image.combine_options do |b|
b.resize "250x200>"
b.rotate "-90"
b.flip
end # the command gets executed
As a handy shortcut, MiniMagick::Image.new also accepts an optional block
which is used to combine_options.
image = MiniMagick::Image.new("input.jpg") do |b|
b.resize "250x200>"
b.rotate "-90"
b.flip
end # the command gets executed
The yielded builder is an instance of MiniMagick::Tool. To learn more
about its interface, see Tools below.
Attributes
A MiniMagick::Image has various handy attributes.
image.type #=> "JPEG"
image.width #=> 250
image.height #=> 300
image.dimensions #=> [250, 300]
image.size #=> 3451 (in bytes)
image.colorspace #=> "DirectClass sRGB"
image.exif #=> {"DateTimeOriginal" => "2013:09:04 08:03:39", ...}
image.resolution #=> [75, 75]
image.signature #=> "60a7848c4ca6e36b8e2c5dea632ecdc29e9637791d2c59ebf7a54c0c6a74ef7e"
If you need more control, you can also access raw image
attributes:
image["%[gamma]"] # "0.9"
To get the all information about the image, MiniMagick gives you a handy method
which returns the output from magick input.jpg json::
image.data #=>
# {
# "format": "JPEG",
# "mimeType": "image/jpeg",
# "class": "DirectClass",
# "geometry": {
# "width": 200,
# "height": 276,
# "x": 0,
# "y": 0
# },
# "resolution": {
# "x": "300",
# "y": "300"
# },
# "colorspace": "sRGB",
# "channelDepth": {
# "red": 8,
# "green": 8,
# "blue": 8
# },
# "quality": 92,
# "properties": {
# "date:create": "2016-07-11T19:17:53+08:00",
# "date:modify": "2016-07-11T19:17:53+08:00",
# "exif:ColorSpace": "1",
# "exif:ExifImageLength": "276",
# "exif:ExifImageWidth": "200",
# "exif:ExifOffset": "90",
# "exif:Orientation": "1",
# "exif:ResolutionUnit": "2",
# "exif:XResolution": "300/1",
# "exif:YResolution": "300/1",
# "icc:copyright": "Copyright (c) 1998 Hewlett-Packard Company",
# "icc:description": "sRGB IEC61966-2.1",
# "icc:manufacturer": "IEC http://www.iec.ch",
# "icc:model": "IEC 61966-2.1 Default RGB colour space - sRGB",
# "jpeg:colorspace": "2",
# "jpeg:sampling-factor": "1x1,1x1,1x1",
# "signature": "1b2336f023e5be4a9f357848df9803527afacd4987ecc18c4295a272403e52c1"
# },
# ...
# }
Pixels
With MiniMagick you can retrieve a matrix of image pixels, where each member of
the matrix is a 3-element array of numbers between 0-255, one for each range of
the RGB color channels.
image = MiniMagick::Image.open("image.jpg")
pixels = image.get_pixels
pixels[3][2][1] # the green channel value from the 4th-row, 3rd-column pixel
It can also be called after applying transformations:
image = MiniMagick::Image.open("image.jpg")
image.crop "20x30+10+5"
image.colorspace "Gray"
pixels = image.get_pixels
Pixels To Image
Sometimes when you have pixels and want to create image from pixels, you can do this to form an image:
image = MiniMagick::Image.open('/Users/rabin/input.jpg')
pixels = image.get_pixels
depth = 8
dimension = [image.width, image.height]
map = 'rgb'
image = MiniMagick::Image.get_image_from_pixels(pixels, dimension, map, depth ,'jpg')
image.write('/Users/rabin/output.jpg')
In this example, the returned pixels should now have equal R, G, and B values.
Configuration
Here are the available configuration options with their default values:
MiniMagick.configure do |config|
config.timeout = nil # number of seconds IM commands may take
config.errors = true # raise errors non nonzero exit status
config.warnings = true # forward warnings to standard error
config.tmpdir = Dir.tmpdir # alternative directory for tempfiles
config.logger = Logger.new($stdout) # where to log IM commands
config.cli_prefix = nil # add prefix to all IM commands
config.cli_env = {} # environment variables to set for IM commands
config.restricted_env = false # when true, block IM commands from accessing system environment variables other than those in cli_env
end
For a more information, see
Configuration API documentation.
Composite
MiniMagick also allows you to
composite images:
first_image = MiniMagick::Image.new("first.jpg")
second_image = MiniMagick::Image.new("second.jpg")
result = first_image.composite(second_image) do |c|
c.compose "Over" # OverCompositeOp
c.geometry "+20+20" # copy second_image onto first_image from (20, 20)
end
result.write "output.jpg"
Layers/Frames/Pages
For multilayered images you can access its layers.
gif.frames #=> [...]
pdf.pages #=> [...]
psd.layers #=> [...]
gif.frames.each_with_index do |frame, idx|
frame.write("frame#{idx}.jpg")
end
Image validation
You can test whether an image is valid by running it through identify:
image.valid?
image.validate! # raises MiniMagick::Invalid if image is invalid
Logging
You can choose to log MiniMagick commands and their execution times:
MiniMagick.logger.level = Logger::DEBUG
D, [2016-03-19T07:31:36.755338 #87191] DEBUG -- : [0.01s] identify /var/folders/k7/6zx6dx6x7ys3rv3srh0nyfj00000gn/T/mini_magick20160319-87191-1ve31n1.jpg
In Rails you'll probably want to set MiniMagick.logger = Rails.logger.
Tools
If you prefer not to use the MiniMagick::Image abstraction, you can use ImageMagick's command-line tools directly:
MiniMagick.convert do |convert|
convert << "input.jpg"
convert.resize("100x100")
convert.negate
convert << "output.jpg"
end #=> `magick input.jpg -resize 100x100 -negate output.jpg`
# OR
convert = MiniMagick.convert
convert << "input.jpg"
convert.resize("100x100")
convert.negate
convert << "output.jpg"
convert.call #=> `magick input.jpg -resize 100x100 -negate output.jpg`
This way of using MiniMagick is highly recommended if you want to maximize performance of your image processing. There are class methods for each CLI tool: animate, compare, composite, conjure, convert, display, identify, import, mogrify and stream. The MiniMagick.convert method will use magick on ImageMagick 7 and convert on ImageMagick 6.
Appending
The most basic way of building a command is appending strings:
MiniMagick.convert do |convert|
convert << "input.jpg"
convert.merge! ["-resize", "500x500", "-negate"]
convert << "output.jpg"
end
Note that it is important that every command you would pass to the command line
has to be separated with <<, e.g.:
# GOOD
convert << "-resize" << "500x500"
# BAD
convert << "-resize 500x500"
Shell escaping is also handled for you. If an option has a value that has
spaces inside it, just pass it as a regular string.
convert << "-distort"
convert << "Perspective"
convert << "0,0,0,0 0,45,0,45 69,0,60,10 69,45,60,35"
magick -distort Perspective '0,0,0,0 0,45,0,45 69,0,60,10 69,45,60,35'
Methods
Instead of passing in options directly, you can use Ruby methods:
convert.resize("500x500")
convert.rotate(90)
convert.distort("Perspective", "0,0,0,0 0,45,0,45 69,0,60,10 69,45,60,35")
Chaining
Every method call returns self, so you can chain them to create logical groups.
MiniMagick.convert do |convert|
convert << "input.jpg"
convert.clone(0).background('gray').shadow('80x5+5+5')
convert.negate
convert << "output.jpg"
end
"Plus" options
MiniMagick.convert do |convert|
convert << "input.jpg"
convert.repage.+
convert.distort.+("Perspective", "more args")
end
magick input.jpg +repage +distort Perspective 'more args'
Stacks
MiniMagick.convert do |convert|
convert << "wand.gif"
convert.stack do |stack|
stack << "wand.gif"
stack.rotate(30)
stack.foo("bar", "baz")
end
# or
convert.stack("wand.gif", { rotate: 30, foo: ["bar", "baz"] })
convert << "images.gif"
end
magick wand.gif \( wand.gif -rotate 90 -foo bar baz \) images.gif
STDIN and STDOUT
If you want to pass something to standard input, you can pass the :stdin
option to #call:
identify = MiniMagick.identify
identify.stdin # alias for "-"
identify.call(stdin: image_content)
MiniMagick also has #stdout alias for "-" for outputting file contents to
standard output:
content = MiniMagick.convert do |convert|
convert << "input.jpg"
convert.auto_orient
convert.stdout # alias for "-"
end
Capturing STDERR
Some MiniMagick tools such as compare output the result of the command on
standard error, even if the command succeeded. The result of
MiniMagick::Tool#call is always the standard output, but if you pass it a
block, it will yield the stdout, stderr and exit status of the command:
compare = MiniMagick.compare
# build the command
compare.call do |stdout, stderr, status|
# ...
end
Configuring
GraphicsMagick
As of MiniMagick 5+, GraphicsMagick isn't
officially supported. This means its installation won't be auto-detected, and no
attempts will be made to handle differences in GraphicsMagick API or output.
However, you can still configure MiniMagick to use it:
MiniMagick.configure do |config|
config.graphicsmagick = true
end
Some MiniMagick features won't be supported, such as global timeout,
MiniMagick::Image#data and MiniMagick::Image#exif.
Limiting resources
ImageMagick supports a number of environment variables for controlling its
resource limits. For example, you can enforce memory or execution time limits by
setting the following:
MiniMagick.configure do |config|
config.cli_env = {
"MAGICK_MEMORY_LIMIT" => "128MiB",
"MAGICK_MAP_LIMIT" => "64MiB",
"MAGICK_TIME_LIMIT" => "30"
}
end
For time limit you can also use the timeout configuration:
MiniMagick.configure do |config|
config.timeout = 30 # 30 seconds
end
Changing temporary directory
ImageMagick allows you to change the temporary directory to process the image file:
MiniMagick.configure do |config|
config.tmpdir = File.join(Dir.tmpdir, "/my/new/tmp_dir")
end
The example directory /my/new/tmp_dir must exist and must be writable.
If not configured, it will default to Dir.tmpdir.
Ignoring STDERR
If you're receiving warnings from ImageMagick that you don't care about, you
can avoid them being forwarded to standard error:
MiniMagick.configure do |config|
config.warnings = false
end
Avoiding raising errors
This gem raises an error when ImageMagick returns a nonzero exit code.
Sometimes, however, ImageMagick returns nonzero exit codes when the command
actually went ok. In these cases, to avoid raising errors, you can add the
following configuration:
MiniMagick.configure do |config|
config.errors = false
end
You can also pass errors: false to individual commands:
MiniMagick.identify(errors: false) do |b|
b.help
end
Thinking of switching from RMagick?
Unlike RMagick, MiniMagick is a much thinner wrapper around ImageMagick.
- To piece together MiniMagick commands refer to the Mogrify
Documentation. For instance
you can use the-flopoption asimage.flop. - Operations on a MiniMagick image tend to happen in-place as
image.trim,
whereas RMagick has both copying and in-place methods likeimage.trimand
image.trim!. - To open files with MiniMagick you use
MiniMagick::Image.openas you would
Magick::Image.read. To open a file and directly edit it, use
MiniMagick::Image.new.
Owner metadata
- Name: minimagick
- Login: minimagick
- Email:
- Kind: organization
- Description:
- Website:
- Location:
- Twitter:
- Company:
- Icon url: https://avatars.githubusercontent.com/u/2532832?v=4
- Repositories: 1
- Last ynced at: 2024-03-25T18:39:41.203Z
- Profile URL: https://github.com/minimagick
GitHub Events
Total
- Create event: 5
- Release event: 5
- Issues event: 15
- Watch event: 52
- Issue comment event: 27
- Push event: 17
- Pull request review event: 2
- Pull request review comment event: 2
- Pull request event: 15
- Fork event: 6
Last Year
- Create event: 5
- Release event: 5
- Issues event: 10
- Watch event: 39
- Issue comment event: 23
- Push event: 16
- Pull request review event: 2
- Pull request review comment event: 2
- Pull request event: 13
- Fork event: 4
Committers metadata
Last synced: 1 day ago
Total Commits: 709
Total Committers: 130
Avg Commits per committer: 5.454
Development Distribution Score (DDS): 0.59
Commits in past year: 28
Committers in past year: 9
Avg Commits per committer in past year: 3.111
Development Distribution Score (DDS) in past year: 0.321
| Name | Commits | |
|---|---|---|
| Janko Marohnić | j****c@g****m | 291 |
| Hampton Catlin | h****n@g****m | 91 |
| Thiago Fernandes Massa | t****n@g****m | 40 |
| James Miller | b****e@g****m | 27 |
| Nadav Shatz | n****z@g****m | 22 |
| Koichi ITO | k****o@g****m | 14 |
| Peter Kieltyka | p****a@n****m | 11 |
| Giuseppe Bertini | g****i@g****m | 10 |
| Lucas Hills | l****s@l****m | 9 |
| David A. Cuadrado | k****k@g****m | 7 |
| Robert Whitney | b****b@c****m | 7 |
| Corey Johnson | p****y@g****m | 6 |
| Steven Harman | s****n@h****m | 6 |
| Kasper Grubbe | k****r@g****m | 5 |
| Tobias Bohwalli | hi@f****o | 5 |
| Corey Johnson | p****m@g****m | 4 |
| Felix Bünemann | b****n@l****o | 4 |
| Fumiaki MATSUSHIMA | m****m@g****m | 4 |
| Carlos León | m****l@c****o | 4 |
| kawahara | t****a@t****p | 3 |
| Sagie Maoz | s****m@g****m | 3 |
| Geremia Taglialatela | t****a | 3 |
| Derek Ethier | d****r@g****m | 3 |
| Fernando Gessler | n****o@c****m | 3 |
| Jarmo Pertman | j****p@g****m | 2 |
| Henry Blyth | b****y@g****m | 2 |
| Fabian Schwahn | f****n@g****m | 2 |
| Dr Nic Williams | d****s@g****m | 2 |
| Diego Elio Pettenò | f****s@f****u | 2 |
| D.Katsuki | 5****i | 2 |
| and 100 more... | ||
Committer domains:
- clean.io: 1
- shopify.com: 1
- kelredd.com: 1
- igloonet.cz: 1
- farend.jp: 1
- fedoraproject.org: 1
- 46bit.com: 1
- me.com: 1
- atlee.ca: 1
- redhat.com: 1
- restorando.com: 1
- ivarvong.com: 1
- krutisch.de: 1
- timothymorton.com: 1
- eq8.eu: 1
- myconan.net: 1
- mediapeers.com: 1
- stoutner.com: 1
- khabarov.ca: 1
- aleksejleonov.com: 1
- starkast.net: 1
- cocoa-x.com: 1
- medwiztech.com: 1
- j10.io: 1
- utau.moe: 1
- xing.com: 1
- nutrient.(none): 1
- github.com: 1
- dsander.de: 1
- agorf.gr: 1
- aussiegeek.net: 1
- academia.edu: 1
- wlw.de: 1
- bernardo.me: 1
- semyonpupkov.com: 1
- andrewloe.com: 1
- bamaru.de: 1
- flavoursys.com: 1
- pololu.com: 1
- clove.com: 1
- seonic.net: 1
- flameeyes.eu: 1
- camaloon.com: 1
- toranoana.co.jp: 1
- carlosleon.info: 1
- louis.info: 1
- futhr.io: 1
- harmanly.com: 1
- carrotcreative.com: 1
- lucashills.com: 1
- nulayer.com: 1
Issue and Pull Request metadata
Last synced: about 1 month ago
Total issues: 90
Total pull requests: 69
Average time to close issues: 12 months
Average time to close pull requests: 3 months
Total issue authors: 86
Total pull request authors: 42
Average comments per issue: 3.08
Average comments per pull request: 1.54
Merged pull request: 53
Bot issues: 0
Bot pull requests: 0
Past year issues: 8
Past year pull requests: 16
Past year average time to close issues: 1 day
Past year average time to close pull requests: 2 days
Past year issue authors: 8
Past year pull request authors: 8
Past year average comments per issue: 1.38
Past year average comments per pull request: 1.0
Past year merged pull request: 15
Past year bot issues: 0
Past year bot pull requests: 0
Top Issue Authors
- fundawang (2)
- SRIKANTHAR (2)
- gerard-kelly-lu (2)
- mtasaka (2)
- paul-mesnilgrente (1)
- danielalves (1)
- sorenstoutner (1)
- emaroto (1)
- ankane (1)
- mauroc (1)
- duncan-bayne (1)
- klues (1)
- pipoblak (1)
- matthewblott (1)
- jeffcovey (1)
Top Pull Request Authors
- tagliala (8)
- henrahmagix (4)
- olleolleolle (3)
- nicedawg (3)
- janko (2)
- tatethurston (2)
- Earlopain (2)
- midnight-wonderer (2)
- mark-young-atg (2)
- artofhuman (2)
- drnic (2)
- dkatsuki (2)
- sorenstoutner (2)
- theomarkkuspaul (2)
- nishidayuya (2)
Top Issue Labels
- HackIllinois2018 (2)
- pr-welcome (1)
Top Pull Request Labels
Package metadata
- Total packages: 3
-
Total downloads:
- rubygems: 634,151,343 total
- Total docker downloads: 1,179,706,074
- Total dependent packages: 573 (may contain duplicates)
- Total dependent repositories: 90,598 (may contain duplicates)
- Total versions: 219
- Total maintainers: 4
- Total advisories: 2
gem.coop: mini_magick
Manipulate images with minimal use of memory via ImageMagick
- Homepage: https://github.com/minimagick/minimagick
- Documentation: http://www.rubydoc.info/gems/mini_magick/
- Licenses: MIT
- Latest release: 5.3.1 (published 5 months ago)
- Last Synced: 2026-01-09T09:58:23.624Z (about 16 hours ago)
- Versions: 79
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 317,272,140 Total
- Docker Downloads: 589,853,037
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 0.058%
- Downloads: 0.076%
- Docker downloads count: 0.156%
- Maintainers (4)
rubygems.org: mini_magick
Manipulate images with minimal use of memory via ImageMagick
- Homepage: https://github.com/minimagick/minimagick
- Documentation: http://www.rubydoc.info/gems/mini_magick/
- Licenses: MIT
- Latest release: 5.3.1 (published 5 months ago)
- Last Synced: 2026-01-07T18:09:47.019Z (2 days ago)
- Versions: 79
- Dependent Packages: 573
- Dependent Repositories: 90,598
- Downloads: 316,879,203 Total
- Docker Downloads: 589,853,037
-
Rankings:
- Dependent packages count: 0.08%
- Downloads: 0.087%
- Dependent repos count: 0.125%
- Docker downloads count: 0.198%
- Average: 0.443%
- Stargazers count: 0.687%
- Forks count: 1.48%
- Maintainers (4)
- Advisories:
proxy.golang.org: github.com/minimagick/minimagick
- Homepage:
- Documentation: https://pkg.go.dev/github.com/minimagick/minimagick#section-documentation
- Licenses: mit
- Latest release: v5.3.1+incompatible (published 5 months ago)
- Last Synced: 2026-01-08T06:19:15.749Z (2 days ago)
- Versions: 61
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Stargazers count: 1.283%
- Forks count: 1.546%
- Average: 5.802%
- Dependent packages count: 9.576%
- Dependent repos count: 10.802%
Dependencies
- pry >= 0
- guard >= 0 development
- guard-rspec >= 0 development
- rake >= 0 development
- rspec ~> 3.5.0 development
- webmock >= 0 development
Score: 34.145583164435884