mcoblentz has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks!

I'm trying to use PerlMagick to build out a semi-opaque caption box. The following ImageMagick command works (with one nuance to deal with) pretty well. It creates the box, captions the data in the file "updatelabel' and outputs it right-justified in the box, then merges the caption box on the main graphic.

convert -background '#0008' -fill white -display 90 -pointsize 16 -gra +vity east -size 500x100 \ caption:'@updatelabel' \ rectangular.jpg +swap -geometry +500+100 -gravity southeast -composit +e rectangle.jpg

The nuance is that the caption is all one color. What is needed is to have each line control it's own color (that's based on specific file parameters and I have that elsewhere).

I built the following in PerlMagick and immediately ran into several problems, for which I am seeking the wisdom of the Monks.

#!/usr/bin/perl use strict; use warnings; use Image::magick; my $image; $image = Image::Magick->new; open( IMAGE, '/Users/coblem/testing/rectangular.jpg' ); $image->Read( file=>\*IMAGE ); my $box; $box->Draw( primitive=>'rectangle', points=>'900,1000 1400,1180'); $box->Qpaque( color=>'#0008'); #$image->Annotate( gravity=>'southeast', font=>'Arial', pointsize=>20, + fill=>'red', x=>500, y=>100, text=>"Quake Information Last Updated + at: Mon Jul 1 21:09:46 2013" ); open( IMAGE, ">/Users/coblemtesting/rectangle.jpg" ); $image->write( file=>\*IMAGE, filename=>'/Users/coblem/testing/rectang +le.jpg' ); close (IMAGE);

First, I thought I could just build a box, fill it, then set the opacity, then annotate it (note the commented line can control the text color uniquely; the plan was to repeat this command as often as needed, adjusting the vertical location for each line); and then merge everything onto the main graphic.

The first issue is that Opaque isn't a valid module (although the instructions sure read like it is). Second issue is that I'm struggling to get the box to be semi-transparent - I can't figure that one out at all, although CAPTION seems to do it natively. Third, if I could substitute ANNOTATE for CAPTION I would have control over the color of each line of text in the box. And I can't figure out that one even in ImageMagick (but that's not really a Perl question).

Questions:

  1. Am I going in the right direction? Build the box, set the transparency, then annotate with the text (each color uniquely)?
  2. If so, do I need two images then merge them? (Build box, then annotate; then merge with main graphic)?
  3. If not that approach, how would you do it?

I would show you the graphic with caption, but I have no idea how to attach an image.

Thanks (in advance) for your suggestions!

Matt

Replies are listed 'Best First'.
Re: Caption-like text with PerlMagick
by mcoblentz (Scribe) on Jul 04, 2013 at 22:02 UTC
    Turns out I needed to control the "alpha" channel with the fill. The code looks like this:
    <snippet> $image->Draw( fill=>'rgba(0,0,0,0.55)', primitive=>'rectangle', points +=>'1460,1000 2060,1180'); $image->Annotate( gravity=>'southeast', font=>'Arial', pointsize=>18, +fill=>'red', x=>500, y=>100, text=>"Quake Information Last Updated +at: Mon Jul 1 21:09:46 2013" ); $image->Annotate( gravity=>'southeast', font=>'Arial', pointsize=>18, +fill=>'green', x=>500, y=>125, text=>"NORAD Information Last Update +d at: Mon Jul 1 21:09:46 2013" ); </snippet>