Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PIL imageText
#1
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.
Reply
#2
You can find the complete document here: http://pillow.readthedocs.io/en/4.1.x/ha...index.html
and specific to your problem here http://pillow.readthedocs.io/en/4.1.x/re...=draw.text
Reply
#3
(Aug-20-2017, 05:25 PM)Larz60+ Wrote: You can find the complete document here: http://pillow.readthedocs.io/en/4.1.x/ha...index.html
and specific to your problem here http://pillow.readthedocs.io/en/4.1.x/re...=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.
Reply
#4
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%40...ext/python

sample2: http://nullege.com/codes/show/src%40p%40...ext/python
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020