How to convert text to an image?
August 18, 2006 1:03 PM   Subscribe

How can I convert few lines of text to a png or gif image? (With perl/python/ruby). Need it for a web-site to make text not copy-able.
posted by chexov to Computers & Internet (12 answers total)
Just do it in Photoshop, or hell failing that, MS Paint. But why bother? If I can see it, I can copy it.
posted by Orange Goblin at 1:16 PM on August 18, 2006

If it needs to be dynamic, you want to use a library like ImageMagick. This can be used from both Perl and Ruby (and probably Python). In Ruby it's known as rmagick, and if you Google for that you'll find a lot of references. This does require your host to have ImageMagick installed though, although it's extremely common.

The other alternative that tends to work on most environments is using PHP and GD.
posted by wackybrit at 1:24 PM on August 18, 2006

From python, using PIL (,

# creates a 50x50 pixel black box with hello world written in white, 8 point Arial text
import Image, ImageDraw, ImageFont

i ="RGB", (50,50))
d = ImageDraw.Draw(i)
f = ImageFont.truetype("Arial.ttf", 8)
d.text((0,0), "hello world", font=f)"helloworld.png", "wb"), "PNG")
posted by bonecrusher at 2:09 PM on August 18, 2006

GD or ImageMagick. Or an online tool (such as one of these?)

If you go down this path (which I wouldn't recommend without a really good reason, like maybe to protect an email address), please provide the same content in an alt attribute so that the content of your site is accessible (and visible to google!) (at which point I could just copy/paste from your HTML source, but is it better to alienate your readers or "protect" your content?)
posted by misterbrandt at 2:17 PM on August 18, 2006

Yeah, the alternative to ImageMagick is GD, but there's no gif support on recent versions so you'd have to rebuild that yourself from older src.
posted by beatrice at 2:22 PM on August 18, 2006

Oh, nevermind on that. Just remembered having heard gif support was put back in GD, and it actually was. ^
posted by beatrice at 2:24 PM on August 18, 2006

Yeah, PHP + GD if you need to do it as part of a website. See the php manual entry for imagettftext for an example. There's Perl and other language bindings for GD, but they're not as integrated as with PHP.

On the command-line, ImageMagick is great. Doing something like:
convert -size 200x30 xc:transparent -font Arial.ttf -fill black -pointsize 12 -draw "text 5,15 ''" addr.png Results in an image like this:

In both examples you'll need some TrueType fonts (.ttf) You probably have some laying around on your computer already. Otherwise you can find some on the Net.
posted by todbot at 2:32 PM on August 18, 2006

Another vote for "why?". I can't think of a single good reason to make something "non-copyable".

Also, having tried to do this in the past, it's OK if you know the length of the text, but this kind of thing gets complicated if you have arbitrary text you want to fit into a non-arbitrary space. You have to do lots of calculations and tweak the font size, possibly iterating a few times.
posted by AmbroseChapel at 3:18 PM on August 18, 2006

Another vote for "why?". I can't think of a single good reason to make something "non-copyable".

I can. I imagine the poster is attempting to avoid copying through automation (i.e. Googlebot, spam harvesters, etc) rather than casual copying by the human eye.
posted by thanotopsis at 5:20 PM on August 18, 2006

But couldn't you just use OCR on an image and automate it just as well (or close enough)?
posted by Orange Goblin at 5:48 PM on August 18, 2006

That's exceptionally terrible to do as far as accessibility goes. You're really punishing more good people than these shadowy scary text-thieves.
posted by floam at 1:15 AM on August 19, 2006

Googlebot? Spam harvesters?

To hide your text from Google is to make yourself harder to find, and I have no idea what you mean by spam harvesters -- harvesters of email addresses?

Do, please, chexov, tell us what your problem is that you're trying to solve in this way.
posted by AmbroseChapel at 4:58 AM on August 19, 2006

« Older How do I make faded highlighting visible?   |   Run, mefi, run! Newer »
This thread is closed to new comments.