element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum Problem with ST7735 display code?
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 3 replies
  • Subscribers 655 subscribers
  • Views 509 views
  • Users 0 members are here
Related

Problem with ST7735 display code?

Kenne76
Kenne76 over 1 year ago

When I run the following code

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

import ST7735
import os

disp = ST7735.ST7735(port=0, cs=0, dc=24, backlight=None, rst=25, width=128, height=160, rotation=0, invert=False)

WIDTH = disp.width
HEIGHT = disp.height

img = Image.new('RGB', (WIDTH, HEIGHT))
draw = ImageDraw.Draw(img)

font_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'fonts', 'PixelOperator.ttf'))
font2 = ImageFont.truetype(font_path, 16)

# Write some text
draw.text((0,0), "Uptime: ", font=font2, fill="red")
draw.text((0,15), "Hostname: ", font=font2, fill="red")
draw.text((67,15), "Q3Server", font=font2, fill="white")
draw.text((0,30), "IP: ", font=font2, fill="red")
draw.text((0,45), "CPU Usage: ", font=font2, fill="red")
draw.text((0,60), "CPU Temp: ", font=font2, fill="red")
draw.text((0,75), "Ram: ", font=font2, fill="red")
draw.text((0,90), "SD Card: ", font=font2, fill="red")
#draw.text((0, 105), "Network: ", font=font2, fill="red")

# Write buffer to display hardware, must be called to make things visible on the
# display!
disp.display(img)

I get this error message

Traceback (most recent call last):
  File "/home/ken/Programming/weather/test4.py", line 17, in <module>
    font2 = ImageFont.truetype(font_path, 16)
  File "/usr/lib/python3/dist-packages/PIL/ImageFont.py", line 861, in truetype
    return freetype(font)
  File "/usr/lib/python3/dist-packages/PIL/ImageFont.py", line 858, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "/usr/lib/python3/dist-packages/PIL/ImageFont.py", line 203, in __init__
    self.font = core.getfont(
OSError: cannot open resource

Can someone help me with this issue?

  • Sign in to reply
  • Cancel
  • shabaz
    0 shabaz over 1 year ago

    According to the error message, its possibly a folder or file issue. Do you have a folder called fonts, and does it contain a file called  PixelOperator.ttf ?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Jan Cumps
    0 Jan Cumps over 1 year ago

    This line throws the error:

    font_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'fonts', 'PixelOperator.ttf'))
    font2 = ImageFont.truetype(font_path, 16)

    (if you are showing the source for /home/ken/Programming/weather/test4.py)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • embeddedguy
    0 embeddedguy over 1 year ago

    Do you run the code with sudo? try doing that.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube