As you can see some fonts are affected more than others. A few have over half the line cut off. I started digging into the code that displays the text. I figured out the code is determining the height and width of the text and then turning the text into an image to be displayed on the screen. This is done so text can easily be rotated on the display.
Line number 17 in this snippet of code is where the height and width is determined before making the image.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Image | |
import ImageDraw | |
import ImageFont | |
import Adafruit_ILI9341 as TFT | |
import Adafruit_GPIO as GPIO | |
import Adafruit_GPIO.SPI as SPI | |
font = ImageFont.truetype('Minecraftia.ttf', 16) | |
# Define a function to create rotated text. Unfortunately PIL doesn't have good | |
# native support for rotated fonts, but this function can be used to make a | |
# text image and rotate it so it's easy to paste in the buffer. | |
def draw_rotated_text(image, text, position, angle, font, fill=(255,255,255)): | |
# Get rendered font width and height. | |
draw = ImageDraw.Draw(image) | |
width, height = draw.textsize(text, font=font) | |
# Create a new image with transparent background to store the text. | |
textimage = Image.new('RGBA', (width, height), (0,0,0,0)) | |
# Render the text. | |
textdraw = ImageDraw.Draw(textimage) | |
textdraw.text((0,0), text, font=font, fill=fill) | |
# Rotate the text image. | |
rotated = textimage.rotate(angle, expand=1) | |
# Paste the text into the image, using it as a mask for transparency. | |
image.paste(rotated, position, rotated) | |
# Write two lines of white text on the buffer, rotated 90 degrees counter clockwise. | |
draw_rotated_text(disp.buffer, 'Hello World!', (150, 120), 90, font, fill=(255,255,255)) | |
draw_rotated_text(disp.buffer, 'This is a line of text.', (170, 90), 90, font, fill=(255,255,255)) | |
# Write buffer to display hardware, must be called to make things visible on the | |
# display! | |
disp.display() |
Here is how I fixed it.
cd /usr/local/lib/python2.7/dist-packages/PIL
sudo vi ImageFont.py
At about line 142 look for the getsize function. Here is what it looked like before the change.
![]() |
And here it is after the change.
![]() |
Save the file and then you need to compile it into python byte-code.
sudo pycompile ImageFont.py
This creates an ImageFont.pyc file. Now to test it again.All fixed! I'm sure that fix to getsize will be pushed out soon so this won't be a problem in the future but until then this will let me continue my experimentation.
I am using nanopi neo air development board. I am working with ubuntu 15.10 OS. I would like to connect adafruit 2.4 inch pitft via spi. It is worked fine with matrix library provided by nanopineo board. The display got worked. But it outputs the mirror image of the desktop and also half of the display is out of the screen.What could be the reason for this error?. Could you please tell me a way to solve out this problem?
ReplyDelete