Coder Social home page Coder Social logo

skeptick's Introduction

Skeptick: Better ImageMagick for Ruby

Build Status Code Climate Dependency Status

Skeptick is an all-purpose DSL for building and running ImageMagick commands. It helps you build any transformations, from trivial resizes to complex mask algorithms and free drawing. In a nutshell, Skeptick is nothing more than a string manipulator and a process spawner. That's all it's meant to be. However, with Skeptick you get quite a few advantages over using plain shell-out or other libraries.

What you get

  • Clean Ruby syntax to build ImageMagick commands
  • Composable Image objects
  • ImageMagick's STDERR output revealed in a Ruby exception
  • Ability to save intermediate images for debugging
  • Minimal memory consumption on shell-outs thanks to posix-spawn
  • Emphasis on performing the whole transformation in a single command

Installation

Add this line to your application's Gemfile:

gem 'skeptick'

And then execute:

$ bundle

Or install it yourself as:

$ gem install skeptick

Usage

To use Skeptick, you simply require it and include the module into your class.

require 'skeptick'

class MyClass
  include Skeptick

  def convert_some_image
    cmd = convert(to: 'result.png') do
      # ...
    end

    cmd.run
  end
end

The cmd object seen in above example can be inspected to see the exact command that Skeptick will run. Simply use cmd.inspect or cmd.to_s. Skeptick never runs anything until you call run (except for one very special case), so you can inspect commands all you want before executing them.

If you don't want to require all of Skeptick, you can just require the core, and and select any specific sugar you want.

require 'skeptick/core'
require 'skeptick/sugar/resized_image'
require 'skeptick/sugar/compose'

See the lib/skeptick/sugar dir for all the goodies.

In Rails Skeptick will automatically use Rails.logger and Rails.root as cd_path. You can also configure your own.

Skeptick.logger  = MyLogger.new
Skeptick.cd_path = '/some/dir'

You can enable debug_mode to display every executed command in the log.

Skeptick.debug_mode = true

Security Note

Never insert any user input into any of Skeptick's commands. This should be obvious. Skeptick executes strings in your shell.

DSL

Skeptick Logo

Take a look at logo.rb to see thow this logo was generated.

A lot is going on in the above script, no worries, it's just a showcase. I bet the first thing you noticed is a shitstorm of little method names like canvas, font, write, draw, etc. Well, they are all sugar. We will cover sugar below.

There are actually only 2 useful methods in all of Skeptick: convert and set.

Convert

convert can be used both standalone and inside another convert. You could say this.

command = convert('image1.png', to: 'image2.png') do
  set :resize, '200x200'
end

# OUTPUT:
# convert image1.png -resize 200x200 image2.png

Or you could put it inside, and it will become a parenthesized subcommand.

command = convert('image1.png', to: 'image2.png') do
  convert do
    set '+clone'           # pull in image1 into parentheses
    set :resize, '100x100' # resize image1's clone in memory
  end

  set '-compose', 'over'
  set '-composite'
end

# OUTPUT:
# convert image1.png ( +clone -resize 100x100 )
#   -compose over -composite image2.png

If you love parentheses a lot, you could nest convert infinitely. However, ImageMagick's clone, delete, and swap are your friends, learn them to cure parenthethitis.

Oh, speaking of nesting โ€” we can reuse that whole command inside another command by passing it to convert in place of an image filepath.

new_command = convert(command, to: 'whatever.png') do
  set '-resize', '300x300'
end

# OUTPUT:
# convert
#   ( image1.png ( +clone -resize 100x100 ) -compose over -composite )
#   -resize 300x300 whatever.png

See what I did there? The command from previous snippet is passed into convert. If you have a convert object in a variable, you can use it inside another convert object down the line. Nesting possibilities are endless.

The same snippet could also be written like this.

new_command = convert(to: 'whatever.png') do
  image command
  set :resize, '300x300'
end

2 differences: 1 - instead of passing in command as argument we declare it inside the block. 2 - resize is a symbol. Any symbol passed into set automatically becomes a string with dash in front of it. Speaking of set.

Set

set adds stuff to your command. You can give it any various arguments, it doesn't care.

# Works the same way:
set '-resize', '100x100'
set :resize, '100x100'

# bad, won't work
set '-resize 100x100'

Watch out for the fact that it shell-escapes every argument, so if you write set '-resize foo', you will get an error, since the space will be escaped, and shell would treat that whole string as single word.`

In addition to set there are also prepend and append to put stuff at the beginning or end of a command, but they are rarely useful, and mostly for implementing your own sugar.

Sugar

Skeptick comes with a bunch of sugar. When you require Skeptick, you can simply require everything. This includes all the sugar functionality.

require 'skeptick'

However, you can require just the core stuff described above, and select any sugar you want.

require 'skeptick/core'
require 'skeptick/sugar/compose'

Compose Sugar

Compose is sugar that adds compose shortcut to Skeptick's DSL.

command = compose(:multiply, 'a.png', 'b.png', to: 'out.png') do
  set :resize, '200x200'
end

# OUTPUT:
# convert a.png b.png -compose multiply -resize 200x200 -composite out.png

It takes the blending type as the first argument, and injects some extra stuff into the resulting command.

As with convert, you don't have to list your images as method arguments. Instead you could declare them inside the block using the image method. The following command does the same thing.

command = compose(:multiply, to: 'out.png') do
  image 'a.png'
  convert 'b.png'
  set :resize, '200x200'
end

Note: image is alias of convert.

Since most of Skeptick's power comes from the ability to infinitely nest things, here's an example involving a nested compose.

command = convert('image1.png', to: 'result.png') do
  compose(:multiply) do
    image 'image2.png[200x200]'

    convert 'image3.png' do
      set :unsharp, '0x5'
    end
  end
end

# OUTPUT:
# convert
#   image1.png image2.png[200x200] ( image3.png -unsharp 0x5 ) -compose
#   multiply -composite result.png"

Notice how we nest compose inside of convert, and then convert inside of compose.

Compose Operators

This is more of a gimmick than a real feature, but you can use math operators like +, -, *, /, &, | to compose images. These are all based on compose method. Here's a multiply example.

image1 = image('foo.png')
image2 = image('bar.png')
result = convert(image1 * image2, to: 'baz.png')

# OUTPUT:
# convert foo.png bar.png -compose multiply -composite baz.png

As you can see, this is equivalent of simply using compose.

# Same thing
result = compose(:multiply, 'foo.png', 'bar.png', to: 'baz.png')

Check out lib/skeptick/sugar/compose.rb for what these operators do.

clone, delete, swap

Skeptick provides methods clone, delete, and swap to manipulate declared images in a sequence, just like in ImageMagick CLI.

command = compose(:over, 'image1.png', to: 'result.png') do
  # You could think of image sequence as a ruby array. Here's what it would
  # look like right now.
  # [ 'image1.png' ]

  compose(:multiply) do
    image 'mask.png' # loading another image for this operation
    clone(0)         # cloning image1.png from outside "into parentheses"
  end

  # Sequence at this point:
  # [ 'image1.png', 'result of compose(:multiply)' ]

  delete(0) # deleting image1.png from the sequence and from memory

  # Sequence at this point:
  # [ 'result of compose(:multiply)' ]

  # At this point the only image loaded in memory is the one produced by the
  # compose(:multiply) command above. Let's load another one.

  image 'image2.png'

  # Sequence at this point:
  # [ 'result of compose(:multiply)', 'image2.png' ]

  # Now we have two images in the sequence. We can swap them in case we need
  # to change their order.

  swap

  # Sequence at this point:
  # [ 'image2.png', 'result of compose(:multiply)' ]

  # Now image2.png is first in the sequence, and the output of
  # compose(:multiply) is second. Since our outermost command is compose(:over),
  # at this point these 2 images will be composed over each other, and the
  # result written to result.png.
end

# OUTPUT
# convert
#  image1.png ( mask.png -clone 0 -compose multiply -composite )
#  -delete 0 image2.png +swap -compose over -composite result.png

You can use clone and delete to refer to multiple images at once by passing mutliple indexes as arguments, like clone(0,1,2) or delete(0,1). Ranges are also accepted. Without any arguments clone and delete are translated to ImageMagick's +clone and +delete. They then refer to the last image in the sequence. Same with swap - you can provide two indexes in arguments like swap(1,3) to swap any 2 images in the sequence, or without arguments it'll act as +swap - which swaps last two images.

Write

Sometimes you might want to take a look at an intermediate image that's being generated inside parentheses, nested somewhere in your command. You can do so with the help of write '/path/to/img.png', which is defined in skeptick/sugar/write.rb.

command = convert(to: 'result.png') do
  compose(:multiply, 'a.png', 'b.png') do
    write '~/Desktop/debug.png'
  end

  set :resize, '200x200'
end

In this case the result of inner compose command will be saved to desktop without affecting anything else. This is a feature that already exists in ImageMagick, as you can see for yourself from generated command:

convert
  ( a.png b.png -compose multiply -composite -write ~/Desktop/debug.png )
  -resize 200x200 result.png

Documentation is to be continued...

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

skeptick's People

Contributors

kachick avatar maxim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

skeptick's Issues

itunes carousel style composition

Hello, kind sir!

I like to create
Screen Shot 2013-04-16 at 3 44 27 PM

composition of images with your great skeptic script like i show here please advise the best way so that i can maybe give script 3 images and it creates this composition

thanking you kindly in advance

please this is urgent matter, Can you please advise how we can do it like that. waiting for your appreciation feedback...

Thanks

Imagemagick throws error when using skeptick, not CLI

So I got an interesting problem here.. I'm doing a somewhat complex combination of effects at once, making several compositions, overlaying / multiplying them, while reducing opacities and converting to jpg.

command = convert(to: 'foo.jpg') do

  compose('over') do
    image 'C1_L0_Leg_Metal_Arc_Chrome.png'
    image 'C1_L1_001.png'
  end

  compose('over') do
    clone(0)
    image 'C1_L2_001.png'
  end

  delete(0)

  compose('over') do
    clone(0)
    image 'C1_L3_001.png'
  end

  delete(0)

  compose('multiply') do
    clone(0)
    convert do
      image 'C1_L5_017.png'
      set '-alpha', 'on'
      set '-channel', 'a'
      set '-evaluate', 'multiply 0.5'
      set '+channel'
    end
  end

  set '-compose', 'over'
  set '-composite'
  set '-background', 'White'
  set '-layers', 'Flatten'

end

The command translates to:

Skeptick::Convert("convert ( C1_L0_Leg_Metal_Arc_Chrome.png C1_L1_001.png -compose over -composite ) ( -clone 0 C1_L2_001.png -compose over -composite ) -delete 0 ( -clone 0 C1_L3_001.png -compose over -composite ) -delete 0 ( -clone 0 ( C1_L5_017.png -alpha on -channel a -evaluate multiply 0.5 +channel ) -compose multiply -composite ) -compose over -composite -background White -layers Flatten foo.jpg")

When run via Skeptick, ImageMagick produces an error:

Skeptick::ImageMagickError: ImageMagick error
convert: invalid argument for option '-evaluate': +channel @ error/convert.c/ConvertImageCommand/1515.

However, if I take the exact command output by skeptick, and run on CLI, Imagemagick doesn't error.

% convert \( C1_L0_Leg_Metal_Arc_Chrome.png C1_L1_001.png -compose over -composite \) \( -clone 0 C1_L2_001.png -compose over -composite \) -delete 0 \( -clone 0 C1_L3_001.png -compose over -composite \) -delete 0 \( -clone 0 \( C1_L5_017.png -alpha on -channel a -evaluate multiply 0.5 +channel \) -compose multiply -composite \) -compose over -composite -background White -layers Flatten foo.jpg

Have we seen anything like this before? I'm wondering what could be causing it, and I'm coming up blank.

Curious if anyone has any ideas.

Windows support?

I have a small script that runs fine on my Mac OSX but when trying to run the same thing on a Windows machine, I get the following error:

c:/Ruby21/lib/ruby/gems/2.1.0/gems/posix-spawn-0.3.9/lib/posix/spawn.rb:164: warning: cannot close fd before spawn

Is this supported for a windows environment?

unrecognized option `-background transparent'

Try to run a converter from svg to png.

    command = convert(to: "#{file_name}.png") do
      set '-background transparent'
      image "#{file_name}.svg"
    end
    command.run

But get always error (no difference when i change order of 'image ...' and set '...')

convert: unrecognized option `-background transparent' @ error/convert.c/ConvertImageCommand/853.

When i run the output in the console or with method 'system' (f.e system command.to_s) it works fine.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.