Page 1 of 1

Text on Transparent background image breaks

Posted: 2013-11-10T23:30:48-07:00
by SrinivasT
I would like to add a Text with given font, size to an image that has transparent background.

The code is as follows

Method#1:
my $im = Image::Magick->new(magick => 'png');
$im->Set( size => "$options{image_length} x $options{image_width}" );
$im->ReadImage( "xc:none" );
$im->Annotate( fill => $options{text_color},
pointsize => $options{text_size},
x => $x_pos,
text => $text,
y => $y_pos,
font => $options{text_font} );
Method#2:
my $im = Image::Magick->new(magick => 'png');
$im->Set( size => "$options{image_length} x $options{image_width}" );
$im->ReadImage( "xc:#FFFFFF" );
$im->Transparent(color=>'#FFFFFF');
$im->Annotate( fill => $options{text_color},
pointsize => $options{text_size},
x => $x_pos,
text => $text,
y => $y_pos,

font => $options{text_font} );

I'm creating an image with white background, and making it transparent, and then writing text in black color.
However when I make the background transparent, the text gets distorted - font breaks.

I see that with above 2 options, the font of the text gets changed - and if font size is very small, I see blurred/overlapped text. If the background is not made transarent, the text looks fine.

The requirement is: Need to create an image with transparent background and annotate the same.



Please suggest.

Re: Text on Transparent background image breaks

Posted: 2013-11-11T16:33:01-07:00
by SrinivasT
Actually above code works, However the text breaks when we create animation out of it.

The code is as follows:
my $image_frames = Image::Magick->new('magick' => 'gif');
for(my $i = 0; $i < 60; $i++) {
# Create an image object, $image - or Clone the transparent image (with no annotation) - this is PNG
# Write the text - $i
push @$image_frames, $image
}
$image_frames->Set('delay' => 100);
$image_frames->Set('loop' => 1);

$image_frames has the problem.

I'm extremely sorry for missing that in initial discussion.
I think this may be happening because I convert a PNG to GIF to create frames - please suggest any work around to fix that

Re: Text on Transparent background image breaks

Posted: 2013-11-11T18:03:06-07:00
by fmw42
Add the equivalent of +dither -colors 2 before writing to gif.

For example, compare these (in command line)


convert -size 200x200 xc:none -fill black -pointsize 20 -gravity center -annotate +0+0 "This is some text" 1tmp1.png

convert -size 200x200 xc:none -fill black -pointsize 20 -gravity center -annotate +0+0 "This is some text" 1tmp1.gif

convert -size 200x200 xc:none -fill black -pointsize 20 -gravity center -annotate +0+0 "This is some text" \
+dither -colors 2 1tmp2.gif