#python-3.x #raspberry-pi #python-imaging-library
#python-3.x #raspberry-pi #python-imaging-library
Вопрос:
Я создаю простой код, который должен отображать некоторые данные на дисплее (дисплей ili9341, подключенный к raspberry pi4). До сих пор я добавлял только текст, и он работает нормально, но я не знаю, как добавить небольшое (20 пикселей * 20 пикселей) изображение под текстом. Я пробовал это, но идентификатор не работает:
image1 = Image.open("frame0.png")
ImageDraw.Draw(image1)
Я понятия не имею, как правильно это сделать, потому что я новичок в python
Здесь у вас есть весь код для контекста:
import time
import subprocess
import digitalio
import board
from PIL import Image, ImageDraw, ImageFont
import adafruit_rgb_display.st7789 as st7789
from PIL import ImageOps
# Configuration for CS and DC pins (these are FeatherWing defaults on M0/M4):
cs_pin = digitalio.DigitalInOut(board.CE0)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = None
# Config for display baudrate (default max is 24mhz):
BAUDRATE = 64000000
pubip="Pub IP: " subprocess.check_output("curl ifconfig.me", shell=True).decode("utf-8")
# Setup SPI bus using hardware SPI:
spi = board.SPI()
counter=0
# Create the ST7789 display:
disp = st7789.ST7789(
spi,
cs=cs_pin,
dc=dc_pin,
rst=reset_pin,
baudrate=BAUDRATE,
width=240,
height=320,
x_offset=0,
y_offset=0,
)
image1 = Image.open("frame0.png")
image2 = Image.open("frame1.png")
image3 = Image.open("frame2.png")
# Create blank image for drawing.
# Make sure to create image with mode 'RGB' for full color.
height = disp.width # we swap height/width to rotate it to landscape!
width = disp.height
image = Image.new("RGB", (width, height))
rotation = 90
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
mirror = ImageOps.mirror(image)
# Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
disp.image(image, rotation)
# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = 0
top = padding
bottom = height - padding
# Move left to right keeping track of the current x position for drawing shapes.
x=0
# Alternatively load a TTF font. Make sure the .ttf font file is in the
# same directory as the python script!
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20)
# Turn on the backlight
backlight = digitalio.DigitalInOut(board.D22)
backlight.switch_to_output()
backlight.value = True
counter=0
while True:
# Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height) , outline=0, fill="#FFFFFF")
# Shell scripts for system monitoring from here:
# https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
cmd = "hostname -I | cut -d' ' -f1"
IP = "IP: " subprocess.check_output(cmd, shell=True).decode("utf-8")
cmd = "top -bn1 | grep load | awk '{printf "CPU Load: %.2f", $(NF-2)}'"
CPU = subprocess.check_output(cmd, shell=True).decode("utf-8")
cmd = "free -m | awk 'NR==2{printf "Mem: %s/%s MB %.2f%%", $3,$2,$3*100/$2 }'"
MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8")
cmd = 'df -h | awk '$NF=="/"{printf "Disk: %d/%d GB %s", $3,$2,$5}''
Disk = subprocess.check_output(cmd, shell=True).decode("utf-8")
cmd = "cat /sys/class/thermal/thermal_zone0/temp | awk '{printf "CPU Temp: %.1f C", $(NF-0) / 1000}'" # pylint: disable=line-too-long
Temp = subprocess.check_output(cmd, shell=True).decode("utf-8")
date= subprocess.check_output("date", shell=True).decode("utf-8")
cmd='uptime | cut -f1 -d "l"'
uptime= subprocess.check_output(cmd, shell=True).decode("utf-8")
# Write four lines of text.
y = top
draw.text((x, y), date, font=font, fill="#FF0000")
y = font.getsize(date)[1]
draw.text((x, y), uptime, font=font, fill="#FF0000")
y = font.getsize(uptime)[1]
draw.text((x, y), IP, font=font, fill="#000000")
y = font.getsize(IP)[1]
draw.text((x, y), pubip, font=font, fill="#000000")
y = font.getsize(pubip)[1]
draw.text((x, y), CPU, font=font, fill="#FFFF00")
y = font.getsize(CPU)[1]
draw.text((x, y), Temp, font=font, fill="#FFFF00")
y = font.getsize(Temp)[1]
draw.text((x, y), MemUsage, font=font, fill="#00FF00")
y = font.getsize(MemUsage)[1]
draw.text((x, y), Disk, font=font, fill="#0000FF")
y = font.getsize(Disk)[1]
draw.text((x, y), str(counter), font=font, fill="#0000FF")
y = font.getsize(str(counter))[1]
if counter==0:
ImageDraw.Draw(image1)
elif counter==1 :
ImageDraw.Draw(image2)
elif counter==2 :
ImageDraw.Draw(image3)
counter=0
counter =1
# Display image.
disp.image(ImageOps.mirror(image), rotation)
time.sleep(0.1)
Ответ №1:
Вам просто нужно Image.paste() вот так:
background = Image.new('RGB', (640,480))
foreground = Image.open('foreground.png')
# Paste foreground onto background at location (20,10)
background.paste(foreground, (20,10))