Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Adding Copyright (C) symbol to an image using Image::Magick

by leighsharpe (Monk)
on Dec 03, 2006 at 11:00 UTC ( [id://587493]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All,
Having a devil of a time with this one.
I am trying to add a line of text to a JPEG image using Image::Magick. Where I am coming unstuck is that I want to use the copyright symbol. The following almost works:
#!/usr/local/bin/perl use strict; use warnings; use Image::Magick; my($image, $x); $image = Image::Magick->new; $x=$image->Read('SV000360.JPG'); warn "$x" if "$x"; my($xSize, $ySize) = $image->Get('width', 'height'); print "Width: $xSize\t Height: $ySize\n"; my $text = chr(hex (0xa9)); $text.="© Some person."; my $yPos=$ySize-50; print "Text:$text\n"; $x=$image->Annotate(font=>"/usr/share/fonts/msttcorefonts/verdana.ttf" +, pointsize=>40, weight=>10, fill=>'yellow', text=>"$text",strokewidt +h=>10, undercolor=>'green', x=>50, y=>$yPos); warn "$x" if "$x"; #$image->Draw(stroke=>'red', primitive=>'rectangle', points=>'20,20 20 +0,200', x=>100, y=>100); $x=$image->Write('SV000360_II.JPG'); warn "$x" if "$x";

If I leave off the "$text=chr(hex(0xa9))" line, the "©" symbol comes out as garbage. If I put that line in, I get the "©" symbol, but it is preceded by another garbage symbol. What am I doing wrong here?

Replies are listed 'Best First'.
Re: Adding Copyright (C) symbol to an image using Image::Magick
by Joost (Canon) on Dec 03, 2006 at 13:25 UTC
      Looks like it well may be encoding, unless my fonts are all shagged.
      I have printed $text to the command line and it comes out as the © symbol.
      The text on the image has a square, indicating that the font doesn't recognise the symbol. I can't download Encode to this box, as it's running an old version of perl (5.6.1), and Encode needs a later version. I tried on my Fedora Core 2 box, which has an updated perl, but it won't install Image::Magick, so I'm still none the wiser.
      For the purposes of now, I think I'll just create a jpeg of the © text I need and overlay it as a watermark.
      Thanks for your input.
Re: Adding Copyright (C) symbol to an image using Image::Magick
by shmem (Chancellor) on Dec 03, 2006 at 12:37 UTC
    If I leave off the "$text=chr(hex(0xa9))" line, the "©" symbol comes out as garbage. If I put that line in, I get the "©" symbol, but it is preceded by another garbage symbol. What am I doing wrong here?

    You can just say $text = chr 0xa9; - no need for hex and lispishness. Then, chr 0xa9 is the copyright symbol in iso8859-1, so I suspect an encoding problem. See Encode.

    Mind to post the garbage you get with and without the $text=chr(hex(0xa9)) line?

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Adding Copyright (C) symbol to an image using Image::Magick
by MaxKlokan (Monk) on Dec 04, 2006 at 11:50 UTC
    0xa9 is already an HEX number, you should not try to convert it with hex().
    For example:
    perl -e "$text=chr(0xa9); print $text"
    outputs:
    ©

    while the following:
    perl -e "$text=chr(hex(0xa9)); print $text"
    outputs:
    Wide character in print at -e line 1.
    ┼©

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://587493]
Approved by TStanley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 22:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found