Gradient Relfelction on Transparent Background

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.

Gradient Relfelction on Transparent Background

Post by jatimon »

Hello everyone, I found an excellent snippet of code in the ImageMagick forum and can't figure out how to generate it in PerlMagick.

here is the link to the article viewtopic.php?f=1&t=15371&p=58415#p58415

this is what I have written,

Code: Select all

sub GlassTable {
# emulate the glasstable effect with ImageMagick
  my $base_image=shift;
  my $x_offset=shift;
  my $y_offset=shift;
  my $opacity=shift;
  my $percent=shift;

  # glasstable is basically just a mirror reflection
  # to do a mirror reflection;
    # clone, flip and crop the original
    # create gradient
    # combine the above images
    # make semi transparent
    # append to bottom of original image

  # Clone/Flip/Crop
  my ($width, $height) = $base_image->Get('columns', 'rows');
  my $first_image=Image::Magick->new();
  my $new_height=int($height*$percent/100);

  # Create the Gradient
  my $size=sprintf("%dx%d",$width,$new_height);
  my $second_image=Image::Magick->new();
  print $second_image->Read("gradient: +level 0x$opacity%");

  # Combine Flipped image and the gradient
  $first_image->Composite(image=>$second_image, compose=>"Overlay", alpha=>"on");

  # Make transparent
  my $transparency=1-($opacity/100);
  $first_image->Evaluate(value=>$transparency, operator=>'Multiply', channel=>'Alpha');

  # Append the reflection to the bottom of the image
  my $image_ary=Image::Magick->new();
  push (@$image_ary, $base_image);
  push (@$image_ary, $first_image);
  return $image_ary->Append(stack=>"true");


I am having a problem with creating the gradient.

this is the error

Code: Select all

Exception 310: unrecognized color `+level 0x90%' @ color.c/GetColorInfo/946
Which I believe is pretty obvious that I can't use the +level bit directly from the CLI reference in the link.

Can someone help me reproduce the steps to generate the same effect from the CLI in the link into PerlMagick?



User avatar
Posts: 8884
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Gradient Relfelction on Transparent Background

Post by anthony »

In you code you have...

Code: Select all

print $second_image->Read("gradient: +level 0x$opacity%");
That is you gave it as a single file name for the image to be created!
As such IM thinks the stuff after the ':' is a color name, thus the error.

+level is a separate operation. You can not directly use command line operations in PerlMagick, you need to call the appropriate PerlMagick functions.

When I created +level I also included the ability to add '!' to the level argument to means use the reversed or '+' form of level. The solution then is to use generate the image then...

Code: Select all

note the '!'.

Note I don't normally use PerlMagick (though have used it), I only responded because I added +level to the library (last year sometime). I have not tested the above, but it should work from what I can see from the Perlmagick Reference.

The Perl docs does not mention the '!' flag. But the CLI and MagickCore function descripts does.
I'm suprised it does not have an 'Invert' flag like LevelColors(), though it would need some Perl Interface specify code as internally their are two separate library functions involved.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages