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

Hi, Not sure it is really a "perl question", but after 4 hours tests I am getting mad :(

I want to print text on a picture. I tried with png and jpg format, the result is the same: The text is not displayed in the color that I want, but in a color that matches the color of the picture at the left of the text. Crazy? Yes!

It means for example: I work on a picture that has a black background and a yellow square on the left side. I print the text in "red" and the text is... yellow. Same for any colors.

The code:

use GD;
$testline="qwerty";
$jpg="test.jpg";
$im = GD::Image->new( $jpg );
$mycolor = $im->colorAllocate(255,0,0);
$im->string(gdSmallFont,135,6,$testline,$mycolor);
$pic_dyn="test2.jpg"; open IMG,">$pic_dyn"; binmode IMG; print IMG $im->jpeg; close IMG;


This is in jpg, if I make the test source picture in png, and save the result in png (or even in jpg), same problem: The text "qwerty" is in yellow where I have a yellow square on the left, in green where I have a green square, etc.

Any suggestion?

Thank you!

Replies are listed 'Best First'.
Re: Perl GD problem with colors for text
by zentara (Cardinal) on Nov 27, 2009 at 17:30 UTC
    ...if you are not stuck with GD, and can use Image::Magick, here is a sample script that will overlay text of any color or transparency, and/or another image
    #!/usr/bin/perl use warnings; use strict; use Image::Magick; #usage: script baseimage overlayimage text my $image = Image::Magick->new; my $oimage = Image::Magick->new; my $rc = $image->Read('1Zen16.jpg'); $rc = $oimage->Read('overlay.png'); my $text = "\nlooking for nirvana\n :-)\n"; $rc = $image->Composite( geometry => '+30+30', compose=>'Atop', image => $oimage, opacity => '100%', tile => 0, ); $image->Annotate(pointsize => 36, fill => '#ffcc00ff', #last 2 digits opacity in hex ff=ma +x text => $text, gravity => 'South' ); $image->Write("$0.png"); exit;
    ...also see Image and Text Watermarked Letters and
    #!/usr/bin/perl -w use strict; use Image::Magick; =comment my $text = 'test'; my $im = new Image::Magick; $im->Read("meteo.jpg"); my $rv = $im->Annotate( text => $text, font => 'C:\\WINDOWS\\Fonts\\Arial.ttf', x => 10, y => 10, ); die("Unable to annotate image: $rv\n") if "$rv"; print "Content-type: image/jpeg\n\n"; binmode STDOUT; print $im->Write('jpeg:-'); =cut my $image = Image::Magick->new; $image->Set(size=>'250x100'); $image->Read('1Zen16.jpg'); my $text = "This is a test"; $image->Annotate( pointsize=>40, text=>$text, stroke => '#C0C0C0', # Increase to make stroke more white fill => '#505050', #or red etc, Decrease to make fill more + black gravity => 'center', x=> -10, # offsets y=> -10 # offsets ); print $image->Write("$0.png");

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: Perl GD problem with colors for text
by gmargo (Hermit) on Nov 27, 2009 at 17:06 UTC

    Your GD::Image constructor is basing the image on the file "test.jpg". Without that file, we cannot test this code.

      File is here :)

      http://www.2shared.com/file/9495528/250982fa/test.html

      Then I tried to print in green (0,255,0) and it shows up red.

      Thank you
        The GD documentation for the colorAllocate function says:
        If no colors are allocated, then this function returns -1.

        Checking $mycolor, I found that it was set to -1 for all 2553 possible RGB combinations.

        Assumption: Because the string function received -1 for the color value, it used the (first|last|?) value in the image's existing palette, thus explaining why the text appeared in the same color as the box.

        I then converted test.jpg to test.gif with an image editor, and changed $jpg to "test.gif". $mycolor received a positive value, and the text appeared in the chosen color.

        I then tried an explicit GD::Image->newFromJpeg on test.jpg, to no avail. I also tried converting to test.png, also to no avail.

        It appears that colorAllocate works correctly when starting with .gif images, but not for .jpg or .png.

Re: Perl GD problem with colors for text
by BrowserUk (Patriarch) on Nov 28, 2009 at 06:43 UTC

    #! perl -slw use strict; use GD; my $testline="qwerty"; my $jpg="test.jpg"; my $im = GD::Image->new( $jpg ) or die $!; my $mycolor = $im->colorClosest(255,0,0); $im->string(gdSmallFont,135, -2,$testline,$mycolor); my $pic_dyn="test2.jpg"; open IMG,">$pic_dyn" or die $!; binmode IMG; print IMG $im->jpeg; close IMG;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.