How to change font size using Python ImageDraw Image Library

I am trying to change the font size using the Python ImageDraw image 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 do not want to specify the font. I want to use the default font and just change the font size. It seems to me that this should be simple, but I can not find the documentation on how to do this.

+4
source share
2 answers

Per PIL docs , ImageDraw default font is a bitmap font, so it cannot be scaled. To scale, you need to select a true type font. I hope you can easily find a good TrueType font that looks โ€œsimilarโ€ to the default font in the desired font size!

+5
source

Just do it

 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()) 
0
source

Source: https://habr.com/ru/post/1308192/


All Articles