2024年7月3日水曜日

textlist_gazou.py

 #!/usr/bin/env/python
import time
import argparse
import sys
import os
import random
import RPi.GPIO as GPIO
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from rgbmatrix import RGBMatrix, RGBMatrixOptions

GPIO.setmode(GPIO.BCM)
GPIO.setup(19,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
imgdir = "/home/pi/rpi-rgb-led-matrix/bindings/python/samples/test/gazou"

def prepareMatrix(parser):
    options = RGBMatrixOptions()
    options.hardware_mapping = "regular"
    options.brightness = 100
    options.rows = 32
    options.cols = 128
    options.pwm_bits = 11
    options.pwm_lsb_nanoseconds = 250
    options.gpio_slowdown = 4
    options.show_refresh_rate = 0
    options.disable_hardware_pulsing = True
    return RGBMatrix(options = options)


parser = argparse.ArgumentParser()
matrix = prepareMatrix(parser)

def getImageFromFile(path):
    image = Image.open(path).convert('RGB')
    return image


def scroll(image, matrix):
    image.resize((matrix.width, matrix.height), Image.ANTIALIAS)
    A = matrix.CreateFrameCanvas()
    img_width, img_height = image.size
    
    if 128 < img_width:
        print(f, "スクロール")
        xpos = 0
        while(True):
            IN = GPIO.input(26)
            xpos += 1
            
            if (xpos > img_width or IN == True):
                xpos = 0
                A.SetImage(image, 0)
                break
            
            if (img_height == 16):
                #A.SetImage(image, 0, 32)
                A.SetImage(image, -xpos ,16) #-xposで左から右スクロール
                A = matrix.SwapOnVSync(A)
                time.sleep(0.015)
            
            else:
                A.SetImage(image, -xpos) #-xposで左から右スクロール
                #A.SetImage(image, -xpos + img_width +200) #前の画像の後ろに次の画像を連結
                A = matrix.SwapOnVSync(A)
                time.sleep(0.015)    
                
    else:      
        print( f, "は横が128px以下のため,スクロールしません")
        xpos = 0
        A.SetImage(image, 0)
        A = matrix.SwapOnVSync(A)

#==========================================

while(True):
    text = open("toho.txt")
    files = text.readlines()
    for f in files:
        f = os.path.join(imgdir, f.rstrip("\n"))
        scroll(getImageFromFile(f), matrix)
        time.sleep(5)
        
        
GPIO.cleanup()

0 件のコメント: