fonts in PIL ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stéphane Ninin

    fonts in PIL ?


    Hello all,

    I am writing (on Linux Redhat 9)
    some script to make graphs using PIL, from a set of points.

    All is ok, except that there seems to be no font coming with PIL.
    Where could I fond some ?
    Also, are there some real examples of use of fonts in PIL ?

    Documentation says something like:
    [color=blue]
    > load
    > ImageFont.load( file) => Font instance
    >
    > Loads a font from the given file, and returns the corresponding font
    > object. If this function fails, it raises an IOError exception.[/color]

    but should "file" be the full path to the font,
    or just a font name ?

    Same question for method truetype:
    [color=blue]
    > truetype
    > ImageFont.truet ype(file, size) => Font instance
    >
    > Load a TrueType or OpenType font file, and create a font object. This
    > function loads a font object from the given file, and creates a font
    > object for a font of the given size.[/color]
    [color=blue]
    > On Windows, if the given file name does not exist, the loader also looks
    > in Windows fonts directory.[/color]


    Also, only true type fonts can be resized:
    there is no easy way to resize standard fonts ?
    (I am not a font expert :) )


    Thanks in advance for your answers.


    Regards,

    --
    Stephane Ninin







  • Jarek Zgoda

    #2
    Re: fonts in PIL ?

    Stéphane Ninin <stefnin@alussi nan.org> pisze:
    [color=blue]
    > All is ok, except that there seems to be no font coming with PIL.
    > Where could I fond some ?[/color]

    http://effbot.org -- look for pilfonts in download section.

    --
    Jarek Zgoda

    Comment

    • Stéphane Ninin

      #3
      Re: fonts in PIL ?

      Also sprach Jarek Zgoda :
      [color=blue]
      >[color=green]
      >> All is ok, except that there seems to be no font coming with PIL.
      >> Where could I fond some ?[/color]
      >
      > http://effbot.org -- look for pilfonts in download section.
      >[/color]

      Thanks a lot.
      Seems there is enough choice so that I wont even need to resize text.


      Comment

      • Cousin Stanley

        #4
        Re: fonts in PIL ?

        | ....
        | Also, are there some real examples of use of fonts in PIL ?
        | ....

        Stéphanie ....

        Following is an example of using the PIL ImageFont class ....

        '''
        Module ....... pil_ImageFont.p y
        Code_By ...... Stanley C. Kitching
        Code_Date .... 2004-02-09
        '''

        import Image
        import ImageFont
        import ImageDraw

        this_list = [ " Look, up in the sky ......... " ,
        " It's a bird ............... . " ,
        " It's a plane ............... " ,
        " It's S u p e r M a n !!!!! " ,
        " " ,
        " More powerful" ,
        " than a locomotive !!!!!!" ,
        " " ,
        " Able to leap tall buildings" ,
        " in a single bound !!!!!!" ]

        this_image = Image.new( 'RGB' , ( 360 , 240 ) )

        # Adjust dir_PIL path to YOUR installation

        dir_PIL = 'K:/Python23/Lib/site-packages/PIL/'

        dir_fonts = dir_PIL + 'pilfonts/'

        this_font = 'courB12.pil'

        i_font = ImageFont.load( dir_fonts + this_font )

        this_col = 10
        this_row = 10
        row_inc = 20

        for this_line in this_list :

        i_draw = ImageDraw.Draw( this_image )

        i_draw.text( ( this_col , this_row ) , this_line , font = i_font )

        this_row += row_inc

        this_image.save ( 'pil_ImageFont. png' )

        this_image.show ()

        --
        Cousin Stanley
        Human Being
        Phoenix, Arizona

        Comment

        • Stéphane Ninin

          #5
          Re: fonts in PIL ?

          Also sprach Cousin Stanley :
          [color=blue]
          >| ....
          >| Also, are there some real examples of use of fonts in PIL ?
          >| ....
          >
          > Stéphanie ....
          >[/color]

          Stéphane please, Stéphanie is a woman's name here. :)

          [color=blue]
          > [...][/color]

          Thanx for the script, I will look at it soon and adapt it for my plot script.

          Regards,

          --
          Stéphane


          Comment

          • Cousin Stanley

            #6
            Re: fonts in PIL ?

            | Stéphane please, Stéphanie is a woman's name here

            Stéphane ....

            Sorry for the typo ....

            Over-clocked fingers NOT synchronized with feeble brain ....

            | Thanx for the script, ....

            You're welcome ....

            --
            Cousin Stanley
            Human Being
            Phoenix, Arizona

            Comment

            • Stéphane Ninin

              #7
              Re: fonts in PIL ?

              Also sprach Cousin Stanley :
              [color=blue]
              > [ ..][/color]

              I had a quick look at it and at the pilfonts...

              Just one last question:
              am I dreaming or all fonts are in white on back ?
              (and cannot be seen if you have a white background) ?

              Or is there a way to set the color of text ?

              Regards,


              --
              Stephane

              Comment

              • Stéphane Ninin

                #8
                Re: fonts in PIL ?

                Also sprach Cousin Stanley :
                [color=blue]
                > [ ..][/color]

                I had a quick look at it and at the pilfonts...

                Just one last question:
                am I dreaming or all fonts are in white ?
                (and so cannot be seen if you have a white background) ?

                Or is there a way to set the color of text ?

                Regards,



                --
                Stéphane Ninin

                Comment

                • Cousin Stanley

                  #9
                  Re: fonts in PIL ?

                  | ....
                  | am I dreaming or all fonts are in white ?
                  | (and so cannot be seen if you have a white background) ?
                  |
                  | Or is there a way to set the color of text ?

                  Stéphane ....

                  # Set the background color when creating the image ....

                  this_image = Image.new( 'RGB' , ( 360 , 240 ) , 'white' )

                  # Set the fill color when drawing the text ....

                  i_draw.text( ( this_col , this_row ) ,
                  this_line ,
                  font = i_font ,
                  fill = 'black' )

                  --
                  Cousin Stanley
                  Human Being
                  Phoenix, Arizona

                  Comment

                  • Stéphane Ninin

                    #10
                    Re: fonts in PIL ?

                    Also sprach Cousin Stanley :
                    [color=blue]
                    >
                    > # Set the background color when creating the image ....
                    >
                    > this_image = Image.new( 'RGB' , ( 360 , 240 ) , 'white' )
                    >
                    > # Set the fill color when drawing the text ....
                    >
                    > i_draw.text( ( this_col , this_row ) ,
                    > this_line ,
                    > font = i_font ,
                    > fill = 'black' )
                    >[/color]

                    Well, it works just fine, thanks again.
                    Maybe I have not browsed the documentation long enough,
                    (the online doc,http://www.pythonware.com/library/pi...book/index.htm)
                    but I didnot see any information
                    about all possible options that can be used.

                    Image.new doc says just:

                    Image.new(mode, size) => image
                    Image.new(mode, size, color) => image
                    Creates a new image with the given mode and size. Size is given as a 2-
                    tuple. The colour is given as a single value for single-band images, and a
                    tuple for multi-band images (with one value for each band). If the colour
                    argument is omitted, the image is filled with black. If the colour is None,
                    the image is not initialised.

                    and for text in ImageDraw it says:

                    text
                    draw.text(posit ion, string, options)
                    Draws the string at the given position. The position gives the upper right
                    corner of the text.
                    The font option is used to specify which font to use. It should be an
                    instance of the ImageFont class, typically loaded from file using the load
                    method in the ImageFont module.
                    The fill option gives the colour to use for the text.


                    Where did you find all these options ? Are they only in source code ?
                    Or is there some other doc I am unaware of ?

                    Regards,

                    --
                    Stéphane Ninin







                    Comment

                    • Cousin Stanley

                      #11
                      Re: fonts in PIL ?

                      | Well, it works just fine, thanks again.

                      You're welcome ....

                      | Where did you find all these options ?

                      The PIL documentation I have is in a PDF file
                      that I think came from the following link ....



                      I don't know if the PDF version is still there
                      and if it is, wheter or not if is in sync ( older or newer than )
                      with HTML version from the link you gave



                      Those two sources are the only ones I have ....

                      --
                      Cousin Stanley
                      Human Being
                      Phoenix, Arizona

                      Comment

                      Working...