python PIL 在文字下方添加图片

python PIL add image under text

我正在构建一个应该在显示器上显示一些数据的简单代码(连接到树莓派 pi4 的 ili9341 显示器)。 直到现在我只添加了文本并且它工作正常,但我不知道如何在文本下方添加一点(20px * 20px)图像。我试过了,但 id 不起作用:

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%%\", ,,*100/ }'"
    MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8")
    cmd = 'df -h | awk \'$NF=="/"{printf "Disk: %d/%d GB  %s", ,,}\''
    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)

你只需要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))