Showing posts with label ImageMagick. Show all posts
Showing posts with label ImageMagick. Show all posts

Thursday, March 15, 2012

Gel button with mini_magick

"Gel Button" is also popular effect. And of course, mini_magick can do this effect.

require 'mini_magick'

image = MiniMagick::Image.open("http://duckduckgo.com/spread/spread4.png")
width = image[:width]
height = image[:height]
image.write 'spread4.png'

image_cloned = MiniMagick::Image.open('spread4.png')
image_cloned.combine_options do |mogrify|
  mogrify.alpha 'off'
  mogrify.blur "#{width}x#{height}"
  mogrify.shade "#{width}x#{height}"
  mogrify.normalize
  mogrify.sigmoidal_contrast '8,60%'
  mogrify.evaluate 'multiply', '.5'
  mogrify.roll "+0+#{(height*0.05).to_i}"
end
image_composed = image_cloned.composite(image) do |c|
  c.compose 'Screen'
end

image_composed.write 'gel_spread4.png'

This is the original image (from Duck Duck Go).

And, this is the converted one.

Saturday, February 18, 2012

Two little stumbling points of ImageMagick

IMHO, there are two little stumbling points when I use ImageMagick command line. The first one is for Windows only, but the other one is for all platforms.

The Convert issue

The official usage page explains what is "The Convert issue". This is because that any default Windows environment contains convert.exe command, so that there is the risk of conflicting with ImageMagick's convert.exe. This is the reason why Windows installer add ImageMagick's root path to the system path, not to the user's path. And you can also avoid this risk as written in "The Convert issue".

Command options are not liner.

In CUI commands, their command options generally have two simple rules.
  • Command options start with the character -(minus).
  • Command options are interpreted in liner.
But, in ImageMagick, these are not true. In other words, there are another two rules. You could find this in all over ImageMagick examples.
  • Command options start with... not only the character -(minus) but also the character +(plus). Plus option is the shortcut for corresponding minus option with default values.
  • Command options are interpreted with ()(paren) priority.
Off course, these rule is so powerful to generate complex image effects, but it's not easy for ImageMagick newbies.

Friday, February 10, 2012

specular reflection with mini_magick (2)

(continued from phosphorescence: specular reflection with mini_magick (1))

"Specular Reflection" consists of 4 operations.
  1. clone from original image and rotate it
  2. add transparency to cloned image
  3. combine original image and cloned one
  4. resize combined image
In this article, I introduce the operation of both (3) and (4) with referring ImageMagick's sample page and using mini_magick. Roughly instructions are below:

  1. Create canvas with extending the vertical length
  2. Put the rotated image onto the bottom of this canvas
  3. Put the original image onto the top of this canvas

Sample of concrete implementation is below:
require 'mini_magick'

original_file = "original.jpg"

image_cloned = MiniMagick::Image.open(original_file)

background_color = 'black' # set background color
COMPOSITE_GRAVITY = 'north'
TRANSPARENCY = '65%'
REFLECTION_FACTOR = Math.sqrt(2)

image_cloned.combine_options do |mogrify|
  mogrify.flip
  mogrify.fill background_color
  mogrify.colorize TRANSPARENCY
end

image_original = MiniMagick::Image.open(original_file)

image_cloned.combine_options(:convert) do |convert|
  new_width = image_original[:width]
  new_height = (image_original[:height]*REFLECTION_FACTOR).to_i
  convert.size "#{new_width}x#{new_height}"
  convert.xc background_color
  convert.swap '0,1'
  convert.gravity COMPOSITE_GRAVITY
  convert.geometry "+0+#{image_original[:height]}"
  convert.composite
end

image_combined = image_cloned.composite(image_original) do |c|
  c.gravity COMPOSITE_GRAVITY
end

output_file = "reflected_#{background_color}.jpg"
image_combined.write output_file
puts "#{output_file} is generated."

These are the output samples.

for white background:

for black background:

Tuesday, February 7, 2012

specular reflection with mini_magick (1)

"Specular Reflection" - a.k.a. "mirror effect" - is the most popular effect for image file to be good-looking. This effect consists of 4 operations.
  1. clone from original image and rotate it
  2. add transparency to cloned image
  3. combine original image and cloned one
  4. resize combined image
In this article, I introduce the operation of both (1) and (2) with referring ImageMagick's sample page and using mini_magick.

This ruby script is like below:
require 'mini_magick'

image_original = MiniMagick::Image.open("original.jpg")

image_cloned = image_original.clone

image_cloned.combine_options do |c|
  c.flip
  c.fill 'white' # set background color
  c.colorize '65%'
end

image_cloned.write "cloned_rotate_transparent.jpg"

You can change the argument(color name) of fill method depending on the background color where you want to put the image file onto. These are original image and rotated and transparent one.


(continue to phosphorescence: specular reflection with mini_magick (2))

Friday, January 13, 2012

mini_magick 3.4

At the end of last year, both mini_magick 3.4 and subexec 0.2.1 are released. Before this release, we had to write different codes for POSIX and for Windows as mentioned in phosphorescence: mini_magick - A better RMagick alternative. Then now, since this release, we can write the same codes both for POSIX and for Windows.
require 'mini_magick'
image = MiniMagick::Image.open("http://www.google.com/images/logos/logo.png")
image.resize "50%"
image.format :gif
image.write "localcopy.gif"

Thursday, November 10, 2011

mini_magick - A better RMagick alternative

Unfortunately, RMagick has been a vaporware despite new developer had been inherited from former one. There are less activities. So that we should seek its alternative. The better one is mini_magick.

installation

> gem list mini_magick
Successfully installed subexec-0.1.0
Successfully installed mini_magick-3.3

Usage

In short, check readme.

For example for POSIX:
require 'mini_magick'
image = MiniMagick::Image.open("http://www.google.com/images/logos/logo.png")
image.resize "50%"
image.format :gif
image.write "localcopy.gif"


(Since the end of 2011, we can write the same codes both for POSIX and for Windows. Please check this article.)

For example for Windows:
require 'mini_magick'
MiniMagick.timeout = 42
image = MiniMagick::Image.open("http://www.google.com/images/logos/logo.png")
image.resize "50%"
image.format :gif
image.write "localcopy.gif"


Because of the bug both in mini_magick and in subexec, the developer using Ruby 1.9.x on Windows should write the mantra MiniMagick.timeout = milliseconds

Next version of mini_magick and subexec

As you see the sources on github, next version of mini_magick and subexec fix bugs for running on Windows. So you can write the same codes both for POSIX and for Windows.

Thursday, December 9, 2010

If you fail Install RMagick on MinGW and MSYS...

If you fail phosphorescence: Install RMagick on MinGW and MSYS, it may be the reason that convert command in extconf.rb file is overloaded with Windows File System's one. So edit the gems/rmagick-2.13.1/ext/RMagick/extconf.rb file at #171 and #178 like below:
`identify -version` =~ /Version: ImageMagick (\d+\.\d+\.\d+)-\d+ /

Then let's type the command in gems/rmagick-2.13.1 directory.
> gem build rmagick.gemspec
> gem install rmagick-2.13.1.gem --platform=ruby -- --with-opt-lib=c:/ImageMagick/6.6.6-Q16/lib --with-opt-include=c:/ImageMagick/6.6.6-Q16/include

And Now I'm requesting pulling it.

Tuesday, October 5, 2010

Install RMagick on MinGW and MSYS

Currently, Windows Rubyist is installing RMagick from here. But there are 3 problems.
  1. Not latest RMagick version
  2. Not latest Ruby version
  3. Not latest bundled ImageMagick

But we are already having Ruby Devkit (MinGW). So let's get latest RMagick on Windows.