I'm having an issue with garbled fonts and I was hoping that someone
might have some suggestions on how to fix them.

I'm trying to write some text (both Japanese and Roman characters) using
Japanese fonts, but the output is just a random string of lines if I am
below a specific size (18 for ipag-mona and 24 for msgothic). I don't
seem to have this problem if I print roman characters using a common
font (Arial for instance). Nothing seems to change even if I use the
"encoding= <insert encoding here>" with the ImageFont.truetype()
constructor.

I have included some example code that creates 8 small bitmaps. 4 with
Japanese characters using various font sizes and 4 with roman characters
using various font sizes.

Can anyone see something wrong with my code or have a suggestion for a
different way to do this?

Thanks,

Tim

msgothic.ttc comes with the east asian language package for MS Windows
(XP at least, I don't know about other versions).
ipag-mona.ttf can be downloaded from the following site:
http://www.geocities.jp/ipa_mona/

Code was written for Python 2.5 and PIL 1.1.6 (downloaded binary for
Win32 and Python2.5) under Windows XP SP2, Japanese Language Edition

# -*- coding: shift-jis -*-

#import needed python modules
#reading sjis
import codecs

#making images
import Image, ImageFile, ImageFont, ImageDraw


def makeImage(size,text,filename):
        
        #ipag-mona is freely available but msgothic is a standard japanese MS 
font
        font = ImageFont.truetype( "ipag-mona.ttf", size)
        #font = ImageFont.truetype( "msgothic.ttc", size)
        #font = ImageFont.truetype( "arial.ttf", size)

        #create a new greyscale bitmap image, the same size as the source image 
to write the text to
        textImage = Image.new('L',(137,60),'white')
        textDraw = ImageDraw.Draw(textImage)

        #find  the size of the image
        imageSize = textImage.size

        #find the size of the text
        textSize = textDraw.textsize( text, font=font )

        #calculate where to start drawing the text
        text_x = ( imageSize[0]/2 ) - ( textSize[0]/2 )
        text_y = ( imageSize[1]/2 ) - ( textSize[1]/2 ) 


        #draw the text onto the new bitmap
        textDraw.text( ( text_x, text_y ), text, font=font)

        #save just the text for comparison
        textImage.save(filename)


if __name__ == '__main__':
        makeImage(12,"Œ³‹C‚È”n".decode('shift-jis'),"out\\uma-12.bmp")
        makeImage(16,"Œ³‹C‚È”n".decode('shift-jis'),"out\\uma-16.bmp")
        makeImage(18,"Œ³‹C‚È”n".decode('shift-jis'),"out\\uma-18.bmp")
        makeImage(24,"Œ³‹C‚È”n".decode('shift-jis'),"out\\uma-24.bmp")
        
        makeImage(12,"fziggy_3876".decode('shift-jis'),"out\\fziggy-16.bmp")
        makeImage(16,"fziggy_3876".decode('shift-jis'),"out\\fziggy-16.bmp")
        makeImage(18,"fziggy_3876".decode('shift-jis'),"out\\fziggy-18.bmp")
        makeImage(24,"fziggy_3876".decode('shift-jis'),"out\\fziggy-24.bmp")
        
_______________________________________________
Image-SIG maillist  -  Image-SIG@python.org
http://mail.python.org/mailman/listinfo/image-sig

Reply via email to