Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: PIL question. having exactly same font on multiple platforms

Reply
Thread Tools

Re: PIL question. having exactly same font on multiple platforms

 
 
Gelonida N
Guest
Posts: n/a
 
      08-04-2011
On 08/04/2011 12:32 PM, Thomas Jollans wrote:
> On 04/08/11 12:04, Gelonida N wrote:

Thanks for your answer.

>> >From within a django application

>> I'd like create a small image file (e.g. .png)
>> which just contains some text.
>>
>> I wondered what library would be appropriate and would yield the same
>> result independent of the OS (assuming the versions of the python
>> libraries are the same)
>> Images should be pixel identical independent on the platform on which
>> the image is created.
>>
>> I made some attempts with PIL (Image / ImageFont / ImageDraw),
>> but have difficulties getting the same font under Linux and windows.

>
> Perhaps PIL uses a different font rendering library on each platform?
> You could try to get around this by using
> http://code.google.com/p/freetype-py/ directly. It's also possible that
> different hinting settings on the different systems are playing a role
> somehow, somewhere. Perhaps you can tell freetype precisely what to do,
> I'm not sure.


The reason why I want the images to look identical is very simple.
Though the final web server will run on a linux server, I use sometimes
windows for development or as test server.

For automated tests I would have liked pixel identical images.
this allows calculating the md5sum of images to know whether
a regression in the image layout occured. Well I can live without it.

The second (more import issue) is, that the images should look the same
whether created on a Windows or Linux host.

I didn't know that PIL delegated font rendering to the underlying OS,
but thought it contains its own rendering.

Here the problem is not if a few pixels are different, but currently I
even don't know how to choose a font, and make sure it exists on both
platforms. I also don't know how I can write portable python code, that
will find a given font on windows and on linux independent of the exact
font location.


>
> If you want exactly the same bitmap on different platforms, you could
> just scrap truetype alltogether, and use bitmaps directly, individually
> taking the characters you need from a resource file. (you know, one
> bitmap with all the characters at known coordinates - or a load of small
> bitmaps)


A resource file or a bitmapped font packaged with my application might
be a solution.
I just had to learn where to get one from or how to create one without
any copyright issues
>
> Why do you need the images to be identical to the pixel anyway? Surely,
> if it's about comparing the text, you would just compare the strings?
> And if it's only for displaying the text, then why bother with details
> like that as long as it looks good?


I hope I clarified a little what I was looking for.
It's not necessarily pixel true, but it should not be too visible
whether the image was rendered on a windows or linux PC



 
Reply With Quote
 
 
 
 
Irmen de Jong
Guest
Posts: n/a
 
      08-04-2011
On 4-8-2011 20:54, Gelonida N wrote:

> The reason why I want the images to look identical is very simple.
> Though the final web server will run on a linux server, I use sometimes
> windows for development or as test server.
>
> For automated tests I would have liked pixel identical images.
> this allows calculating the md5sum of images to know whether
> a regression in the image layout occured. Well I can live without it.


Then don't run your automated tests on the dev server but deploy your stuff to a
separate test server first, that runs the same software as production. And run the tests
there.


> The second (more import issue) is, that the images should look the same
> whether created on a Windows or Linux host.
>
> I didn't know that PIL delegated font rendering to the underlying OS,
> but thought it contains its own rendering.


I'm pretty sure it does have that indeed; it links with the freetype library. Are you
sure you're not just seeing differences because of differences in the typeface itself?

A courier.ttf on one system can look very different from a ms-courier-new.ttf on another...

> Here the problem is not if a few pixels are different, but currently I
> even don't know how to choose a font, and make sure it exists on both
> platforms. I also don't know how I can write portable python code, that
> will find a given font on windows and on linux independent of the exact
> font location.


I once made a module that uses PIL to draw captcha images. It uses one of the free
truetype font files that I just place next to the code. I downloaded the ttf files from
http://www.fontgirl.com/ but there are dozens of free font sites. Just be sure to check
the license terms of the the typeface files.

As far as I know, I did not see any difference in output on windows, linux and mac os x
as long as the code used the same ttf file and PIL versions. (but I'll double check now
and see if I remember this correctly).

Irmen
 
Reply With Quote
 
 
 
 
Irmen de Jong
Guest
Posts: n/a
 
      08-04-2011
On 4-8-2011 21:30, Irmen de Jong wrote:

> As far as I know, I did not see any difference in output on windows, linux and mac os x
> as long as the code used the same ttf file and PIL versions. (but I'll double check now
> and see if I remember this correctly).


To follow up on myself, I've just tested it with the same ttf file on windows, os x and
linux. (made an image in font size 40 with 'The quick brown fox....' as text line) The
resulting image files were byte-identical between os x and linux but the windows ones
were slightly different (just by a few bytes). The text in the image itself looked
identical though to the ones from the other systems (I couldn't see any difference by
looking at it) so I presume the difference is in the image compression or file header
metadata.

PIL's font rendering seems to produce identical results across platforms and if you see
a difference it must be caused by a difference in typeface file (or perhaps a different
PIL/freetype version on the platform).

Irmen
 
Reply With Quote
 
Gelonida N
Guest
Posts: n/a
 
      08-04-2011
Thanks again to everybody,

Your answers helped me to understand better.

My pragmatic solution is now to package my program
with an open source .ttf font,
which will be used on both platforms.


On 08/04/2011 10:24 PM, Irmen de Jong wrote:
> On 4-8-2011 21:30, Irmen de Jong wrote:
>
>> As far as I know, I did not see any difference in output on windows, linux and mac os x
>> as long as the code used the same ttf file and PIL versions. (but I'll double check now
>> and see if I remember this correctly).

>
> To follow up on myself, I've just tested it with the same ttf file on windows, os x and
> linux. (made an image in font size 40 with 'The quick brown fox....' as text line) The
> resulting image files were byte-identical between os x and linux but the windows ones
> were slightly different (just by a few bytes). The text in the image itself looked
> identical though to the ones from the other systems (I couldn't see any difference by
> looking at it) so I presume the difference is in the image compression or file header
> metadata.
>
> PIL's font rendering seems to produce identical results across platforms and if you see
> a difference it must be caused by a difference in typeface file (or perhaps a different
> PIL/freetype version on the platform).
>



What I finally did was following:

I copied a ttf font whoose copyright seems to be permissive ('Vera.ttf')
from my linux machine to my windows host.

I wrote following script:
import Image
import ImageFont, ImageDraw
import ImageChops


im = Image.new( "RGB", (250,60) , "#000000" )

draw = ImageDraw.Draw(im)

font = ImageFont.truetype("Vera.ttf", 40)

draw.text((5, 5), "Hello world", font=font)
im.save("img.png")

im = Image.open('img.png')
iml = Image.open('img_other.png')
diff = ImageChops.difference(im, iml)
diff.save('diff.png')



and ran it on one machine
then I copied the generated file img.png to my other platform and
renamed it to img_other.png

then I ran my script there

and displayed diff.png


What I see (but I do not have the same version of PIL on both machines)
is, that the images are different, though the difference as rather local
and not too huge.

So copying an open source ttf font to my windows machine
creates fonts, which are from an optical aspect similair enough.

I will still check whether the imgaes are identical if using the same
font file and the same PIL version.
THis should clarify whether the OS is involved in rendering the images
or not.

If really wanted to run automated tests verifying the image result I
could still do it by saving the md5sums for both platforms and veryfing
whether the result is identical to one of them.








 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PIL question. having exactly same font on multiple platforms Gelonida N Python 1 08-04-2011 12:37 PM
PIL: problem to convert an image array to PIL format Sverre Python 2 12-17-2009 04:33 PM
Swing Font, it's Java Font? ot native? how install new font? mttc Java 2 07-03-2009 07:29 PM
[PIL] is there a downloadable docs for PIL Egor Bolonev Python 2 12-24-2004 11:05 AM
Inserting an image in a select list, or having multiple font styles -- anyway to do this? Julia Briggs Javascript 1 01-15-2004 05:20 PM



Advertisments