(none) imager-devel
/ help / lists / applications / search /

Re: Transparency and Text Rendering

From: Mike Depot (
Date: Wed 02 May 2001 - 20:08:50 UTC

  • Next message: Mike Depot: "Re: Transparency and Text Rendering"

    > Hi guys - thanks very much for the help so far. My next challenge is to
    > make part of my text string one color and the other another color. For
    > those interested I've attached the current state of my gif (current.gif)
    > well as the manually created gif that I'm seeking to produce with the
    > module (objective.gif). Obviously I have a fair amount of tweaking to do,
    > but my primary question at this point is how to create the alternating
    > colors... will I need to combine two images to do this? Also, what are
    > of the likely parameters you would suggest playing with to attempt to size
    > my gif down to the smaller size of the photoshop created gif?

    Below is my attempt at solving this problem. I turned on antialiasing for
    the font so a smaller text size would be readable. It works great for PNG,
    but there's still a problem with the GIF version. The code below creates
    three images. The first image, date1.gif, looks great, but it's rendered on
    a white background instead of transparent. The second image, date2.png,
    seems to have dealt with the transparancy perfectly.

    The problem occurs in date3.gif. When write() did it's processing for
    transparency, it seems to have chosen blending colors assuming the
    background was black. This means black text on an assumed black background
    didn't get any antialiasing. Around the orange text, you can still see some
    antialiased pixels that are half way between orange and black.

    How would you make the antialiasing processing that write() uses for GIF
    images assume a paticular color background for transparent pixels? To get
    the desired result in this case, I would have wanted it to assume white
    instead of a black.


    #!/usr/bin/perl -w

    # tested on Imager version 0.38pre8
    use strict;
    use Imager qw(:handy);
    use POSIX qw(strftime);

    my ($mday, $mon, $year, $wday) = (localtime)[3,4,5,6];
    my $datestr = strftime("%A, %d-%b-%Y",0,0,0,$mday,$mon,$year,$wday);

    my $black = NC(0,0,0);
    my $orange = NC(255,102,0);
    my $white = NC(255,255,255);

    my $fontname = "/usr/share/fonts/times.ttf";
    my $font=NF(file=>$fontname,size=>30,aa=>1)
        or die "Couldn't create font: $Imager::ERRSTR\n";

    my $datestr1 = $datestr.' [';
    my $datestr2 = 'an orange day';
    my $datestr3 = ']';
    $datestr = $datestr1.$datestr2.$datestr3;

    my @bbox=$font->bounding_box(string=>$datestr);
    my @bbox1=$font->bounding_box(string=>$datestr1);
    my @bbox2=$font->bounding_box(string=>$datestr2);
    my @bbox3=$font->bounding_box(string=>$datestr3);

    my $xs1=$bbox1[2]-$bbox1[0]+2;
    my $xs2=$bbox2[2]-$bbox2[0]+2;
    my $xs3=$bbox3[2]-$bbox3[0]+2;
    my $xs = $xs1+$xs2+$xs3;
    my $ys=$bbox[3]-$bbox[1]+4;

    my $i1=Imager->new(xsize=>$xs,ysize=>$ys,channels=>4);
    $i1->string(font=>$font,string=>$datestr3, color=>$black,
    $i1->write(file=>"date1.gif",gifquant=>'gen', transp=>'threshold',

    my $i2=Imager->new(xsize=>$xs,ysize=>$ys,channels=>4);
    $i2->string(font=>$font,string=>$datestr3, color=>$black,
    $i2->write(file=>"date3.gif",gifquant=>'gen', transp=>'threshold',