Pure-C lua bindings to ImageMagick
Because existing FFI-based bindings are hackish and buggy, duh.
- Q) Will this work with openresty/nginx-lua?
A) Yes. But remember, that IM operations are blocking. - Q) Is this production ready?
A) Yes, we're using it for couple of months now without problems. - Q) Is this feature-complete?
A) Hell no. There's lot of uncovered IM api. I've implemented only needed for me api for now. - Q) How do i properly resize animated gif?
A) Firstly, coalesce() it, then resize as usual, then optimize() it back. You really should cache coalesce()'ed image if you resizing it frequently.
- ImageMagick developer headers (>=6.8.8.3)
- Lua (5.1/5.2) or LuaJit
- Cmake 2.8.12 or later
- Working C compiler
As easy as
mkdir build
cd build
cmake ..
make
make install
You can also use make unittest
after make to run tests.
By default module compiles with support for luajit
For other Lua interpreters see cmake options.
local magick = require "imagick"
local img = magick.open("filename.jpg")
img:set_gravity(magick.gravity["NorthGravity"])
img:smart_resize("100x100^")
img:extent(100, 100)
img:set_quality(90)
img:strip()
img:write("out.jpg")
Gravity values
See here
Example
local magick = require "imagick"
print(magick.gravity["WestGravity"])
Interlace scheme values
See here
Example
local magick = require "imagick"
print(magick.interlace["JPEGInterlace"])
Colorspace values
See here
Example
local magick = require "imagick"
print(magick.colorspace["YUVColorspace"])
Scale filters
See here
Example
local magick = require "imagick"
print(magick.filters["LanczosSharpFilter"])
Composite operations
See here
Example
local magick = require "imagick"
local img = magick.open("input.jpg")
local logo = magick.open("logo.jpg")
img:set_compose( magick.composite_op["CopyCompositeOp"] )
img:composite(logo, 0, 0)
img:write("out.jpg")
Font styles
- UndefinedStyle
- NormalStyle
- ItalicStyle
- ObliqueStyle
- AnyStyle
Font align
- UndefinedAlign
- LeftAlign
- CenterAlign
- RightAlign
Color channels
See here
methods for MagickDistortImage
See here
Color channels
See here
Opens image from given filepath or image definition
Example
local img = magick.open("input.jpg") -- open jpg file
Open image from data blob
Create image from pseudo-image definition. See here
Manually free all allocated for image memory.
Use with caution. Never call to any image methods afterwards.
Get image width in pixels
Get image height in pixels
Get number of images inside an image (e.g. frames in GIF or pages in PDF)
Clone image with all current settings/values
Write image to file
This outputs only first frame
Write all image frames to file
If join is false this will create sequentaly numbered file for each image frame
If join is true this will create one file with all frames (this demends on image format, works with gif, for example)
Return raw image data as string
Get image format ("JPEG"/"GIF"/"PNG"/etc.)
Set image format ("JPEG"/"GIF"/"PNG"/etc.)
Get image compression quality (0-100)
Set image compression quality (0-100)
Get current image gravity
Set image gravity (See imagick.gravity enum)
Get current image interlace scheme
Set image interlace sheme (See imagick.interlace enum)
e.g. for Progressive JPEG set it to JPEGInterlace
Get imagemagick option for image
Set imagemagick option for image
Get imagemagick artifact for image. See here
Set imagemagick artifact for image
Get image background color
Returns comma-separated color values.
Set image background color (html hex notation or comma-separated)
Get image colorspace (See imagick.colorspace enum)
Set image colorspace (See imagick.colorspace enum)
Returns true if image has alpha-channel
Returns true if image has embedded icc profile
Returns image icc profile as blob
Set (and convert image to) image icc profile from blob
Set (and convert image to) image icc profile from file
Set image composite operator (See imagick.composite_op)
Set font to use in annotate, full path to font file
Set font to use in annotate, font family string
Set font size to use in annotate
Set font style to use in annotate (See imagick.font_style enum)
Set font weight to use in annotate
Set font align to use in annotate (See imagick.font_align enum)
<bool>status, <string>error = img:annotate(<string> color, <string> text, <int> x, <int> y, <int> angle)
Annotate image
Set image mask for compositing operations
You can set it to nil
to reset
Coalesce (rebuild) all image frames
Optimise all image frames
Deconstruct all image frames (MagickDeconstructImages() )
Strip exif data and profiles from image
Apply swirl filter
Apply oilpaint filter
Rotate image on angle filling empty space with color
Modify brightness, saturation, and hue of an image
Blur image
Creates a vertical mirror image by reflecting the pixels around the central x-axis.
Creates a horizontal mirror image by reflecting the pixels around the central y-axis.
Creates a vertical mirror image by reflecting the pixels around the central x-axis while rotating them 90-degrees.
Creates a horizontal mirror image by reflecting the pixels around the central y-axis while rotating them 270-degrees.
Sharpen image
Blur image adaptively
Sharpen image adaptively
Blur image channel
<bool>status, <string>error = img:sharpen_channel(<channel> channel, <double> sigma, <double> radius)
Sharpen image channel
<bool>status, <string>error = img:adaptive_blur_channel(<channel> channel, <double> sigma, <double> radius)
Blur image channel adaptively
<bool>status, <string>error = img:adaptive_sharpen_channel(<channel> channel, <double> sigma, <double> radius)
Sharpen image channel adaptively
Modify image gamma
Modify image channel gamma
Auto-adjust image gamma
Auto-adjust image channel gamma
Adjust image levels. Black/white points is 0-100%
<bool>status, <string>error = img:level_channel(<double> black, <double> white, <double> gamma, <channel> channel)
Adjust image levels for channel. Black/white points is 0-100%
Enhances the intensity differences between the lighter and darker elements of the image.
Set sharpen to 'true' to increase the image contrast, otherwise the contrast is reduced
Surrounds the image with a border of the color
Blends the fill color with each pixel in the image
Resize image using current scale filter
Adaptively resize image with data dependent triangulation using current image filter
Resample image. See https://imagemagick.org/api/magick-image.php#MagickResampleImage
Fast scale image
Crop image
You can pass additional x,y coordinates, e.g.: <bool>status, <string>error = img:crop(<int> width, <int> height, <int> x, <int> y)
Resize image and remove all profiles.
Apply one image on top of another at x/y with composite operator compositeop (See imagick.composite_op)
<bool>status, <string>error = img:composite_channel(<image> src, <channel> channel, <int> x, <int> y, <int> compositeop)
Apply one image channel on top of another at x/y with composite operator compositeop (See imagick.composite_op)
Extent image
Smartly resize image.
Format is one of:
- WxH (Keep aspect-ratio, use higher dimension)
- WxH^ (Keep aspect-ratio, use lower dimension (crop))
- WxH! (Ignore aspect-ratio)
It uses Mitchell filter for upscaling/downscaling all formats and Lanczos for downscaling JPEG.
You should use img:extent after it to really crop or add borders (with img:get_bg_color()
) to image.
Set DrawingWand fill color
Set DrawingWand stroke color
Query font metrics. See https://imagemagick.org/api/magick-wand.php#MagickQueryFontMetrics Returns 13 variables or nil,error
Query font metrics. See https://imagemagick.org/api/magick-wand.php#MagickQueryMultilineFontMetrics Returns 13 variables or nil,error
Distorts image. See https://imagemagick.org/api/magick-image.php#MagickDistortImage
Example (https://imagemagick.org/Usage/distorts/#arc):
local magick = require "imagick"
local img = magick.open("rose.jpg")
img:distort(magick.distort_method["ArcDistortion"], { 60, 90 }, false)
img:write("out.jpg")
Apply simultaneous black/white threshold to the image.
See https://imagemagick.org/api/magick-image.php#MagickThresholdImage
See https://imagemagick.org/script/command-line-options.php#threshold
value
is effective between 0
and 0xffff
local magick = require "imagick"
local img = magick.open("rose.jpg")
---Converts percents to value
function pct(pct)
return 65535 * pct / 100
end
img:threshold(pct(95))
img:write("out.jpg")
Trim an image.
This option removes any edges that are exactly the same color as the corner pixels. Use fuzz to make trim remove edges that are nearly the same color as the corner pixels.
See https://imagemagick.org/api/magick-image.php#MagickTrimImage
See https://imagemagick.org/script/command-line-options.php#trim
fuzz
is effective between 0
and 0xffff
local magick = require "imagick"
local img = magick.open("rose.jpg")
---Converts percents to value
function pct(pct)
return 65535 * pct / 100
end
img:trim(pct(5))
img:write("out.jpg")
local magick = require "imagick"
local code ="hello"
local i = 0
math.randomseed(os.time())
local r=100+math.random(35)
local g=100+math.random(35)
local b=100+math.random(35)
local img = assert(magick.open_pseudo(150,50, "xc:rgb("..r..","..g..","..b..")"))
for c in code:gmatch(".") do
local r=150+math.random(105)
local g=150+math.random(105)
local b=150+math.random(105)
img:set_font_size(25+math.random(10))
img:annotate("rgb("..r..","..g..","..b..")", c, (i*20)+10, math.random(50-50)+30, math.random(55)-20)
i=i+1
end
img:swirl(10)
img:oilpaint(1)
img:write("captcha.png")
local ima = require("imagick")
local img, err = ima.open("input.jpg")
img:modulate(120, 10 ,100)
img:colorize("#222b6d", 0.2)
img:gamma(0.5)
img:contrast(true)
img:contrast(true)
img:border('black', 20, 20)
img:write("out.jpg")
local ima = require("imagick")
local img, err = ima.open("input.jpg")
img:level_channel(33, 66, 1.0, ima.channel["RedChannel"])
img:level_channel(33, 66, 1.0, ima.channel["GreenChannel"])
crop_x = math.floor(img:width() * 1.5)
crop_y = math.floor(img:height() * 1.5)
local gradient = ima.open_pseudo(crop_x, crop_y, "radial-gradient:none-black")
gradient:set_gravity(ima.gravity['CenterGravity'])
gradient:crop(img:width(), img:height())
img:composite(gradient, 0, 0, ima.composite_op['MultiplyCompositeOp'])
img:border('black', 20, 20)
img:write("out.jpg")
Epifanov Ivan isage.dna@gmail.com
This module is licensed under the WTFPL license.
(See LICENSE)