Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change font size using the Python ImageDraw Library

Tags:

python

I am trying to change the font size using python's ImageDraw library.

You can do something like this:

fontPath = "/usr/share/fonts/dejavu-lgc/DejaVuLGCSansCondensed-Bold.ttf"
sans16  =  ImageFont.truetype ( fontPath, 16 )

im  =  Image.new ( "RGB", (200,50), "#ddd" )
draw  =  ImageDraw.Draw ( im )
draw.text ( (10,10), "Run awayyyy!", font=sans16, fill="red" )

The problem is that I don't want to specify a font. I want to use the default font and just change the size of the font. This seems to me that it should be simple, but I can't find documentation on how to do this.

like image 891
Eldila Avatar asked Apr 28 '10 00:04

Eldila


People also ask

How do you change font size in Python?

Open the Python shell. Then, in the menu bar, under "Python" (directly to the right of the Apple icon), you will find "Preferences". Under this, you will find the "Font/Tabs" option, and you can change the font size according to your preference.

How do I change the font size in idle Python?

On the top menu, choose Options , then Configure IDLE . The Fonts/Tabs tab will be displayed. There is a Size button. Click on it and select a bigger size than the default of 10.

How do you change the font on Pyscripter?

Pyscripter Tools menu > Options > Editor Options. Display tab > click Font. Select "Source Code Pro" from list of fonts. Select Style and Size if desired.

How do you change font size and color in Python?

Define custom font and color settings for PythonPress Ctrl+Alt+S to open the IDE settings and select Editor | Color Scheme | Python.


2 Answers

Per PIL's docs, ImageDraw's default font is a bitmap font, and therefore it cannot be scaled. For scaling, you need to select a true-type font. I hope it's not difficult to find a nice truetype font that "looks kinda like" the default font in your desired font-size!

like image 125
Alex Martelli Avatar answered Sep 26 '22 06:09

Alex Martelli


Just do this

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

def image_char(char,image_size, font_size):
        img = Image.new("RGB", (image_size, image_size), (255,255,255))
        print img.getpixel((0,0))
        draw = ImageDraw.Draw(img)
        font_path = "/Users/admin/Library/Fonts/InputSans-Regular.ttf"
        font = ImageFont.truetype(font_path, font_size)

        draw.text((5, 5), char, (0,0,0),font=font)
        img.show()

main():
       image_char("A",36,16)

if __name__ == '__main__':
       sys.exit(main())
like image 28
loretoparisi Avatar answered Sep 26 '22 06:09

loretoparisi