Python Forum
PIL imageText - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: PIL imageText (/thread-4483.html)



PIL imageText - tony1812 - Aug-20-2017

Hello, I am practicing use PIL to ouput text on screen. But I donot fin a clear answer about using draw,text.
My code is like this:

import RPi.GPIO as GPIO

import time

import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306

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

RST = None

# 128x64 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)

# Initialize library.
disp.begin()

# Clear display.
disp.clear()
disp.display()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
#draw.rectangle((0,0,width,height), outline=0, fill=0)

#draw a text
#font = ImageFont.truetype("media/text/fonts/" + font + ".ttf", fontsize, encoding="unic")
draw.text((0,16), "looooooooooooong text", fill=255)

disp.image(image)
    
disp.display()   
time.sleep(.01) 
The output diplays just fine by using the default draw.text((0,16), "looooooooooooong text", fill=255)
But it is the option the confuses me. draw.text((0,16), "looooooooooooong text", fill=255, font=?????)
Whst do I need to do to make the font size bigger or change font family? Thanks.


RE: PIL imageText - Larz60+ - Aug-20-2017

You can find the complete document here: http://pillow.readthedocs.io/en/4.1.x/handbook/index.html
and specific to your problem here http://pillow.readthedocs.io/en/4.1.x/reference/ImageDraw.html?highlight=draw.text


RE: PIL imageText - tony1812 - Aug-20-2017

(Aug-20-2017, 05:25 PM)Larz60+ Wrote: You can find the complete document here: http://pillow.readthedocs.io/en/4.1.x/handbook/index.html
and specific to your problem here http://pillow.readthedocs.io/en/4.1.x/reference/ImageDraw.html?highlight=draw.text
Yes, I aleardy read that but don't know the option field, the links above do not explain clearly, that's why I ask in the first place. Please tell me the actual workink code, I tried this font = ImageFont.truetype("/usr/share/fonts/dejavu/DejaVuSans.ttf",25) and font = ImageFont.truetype("arial") both give me error.


RE: PIL imageText - Larz60+ - Aug-20-2017

PIL.ImageDraw.Draw.text(xy, text, fill=None, font=None, anchor=None, spacing=0, align="left")
I found a couple of  examples here.
Sorry, I've used PIL quite a bit, but never the text method

sample1: http://nullege.com/codes/show/src%40m%40u%40Muntjac-1.1.2%40muntjac%40addon%40colorpicker%40color_picker_application.py/400/PIL.ImageDraw.Draw.text/python

sample2: http://nullege.com/codes/show/src%40p%40r%40Products.PyConBrasil-2.4.1%40Products%40PyConBrasil%40Extensions%40certificate.py/19/PIL.ImageDraw.Draw.text/python