前提・実現したいこと
ラズベリーパイのスタータキットを購入し、プログラミングの練習をしています
現在簡単なGUI、アプリを作ろうとしています。
目標としているのは、
スイッチを押したら、カメラで撮影を行い、その画像を貼り付けて表示させる
というものです。
発生している問題・エラーメッセージ
カメラで撮影できており、フォルダにも保存されているが、
キャンバスなどに貼り付けて表示させることができない
(gpicviewでは、撮影した画像を確認できる)
該当のソースコード
python
1from tkinter import * 2import subprocess 3import RPi.GPIO as GPIO 4import time 5from PIL import Image, ImageTk 6 7class App: 8 def __init__(self, master): 9 frame = Frame(root, relief = "raised", borderwidth = 5, bg = "black") 10 frame.pack(fill = BOTH) 11 12 c = Canvas(frame, bg = "white", width = 500, height = 500) 13 14 photo = "/home/pi/program/API/capture.png" 15 img = Image.open(open(photo,"rb")) 16 img_tk = ImageTk.PhotoImage(img) 17 c.create_image(0, 0, image = img_tk, anchor = NW) 18 c.grid(column = 2, row = 1, columnspan = 4, rowspan = 4, padx = 10) 19 20if __name__ == "__main__": 21 root = Tk() 22 23 PIN = 14 24 GPIO.cleanup() 25 GPIO.setmode(GPIO.BCM) 26 GPIO.setup(PIN, GPIO.IN) 27 28 while True: 29 if (GPIO.input(PIN) == GPIO.HIGH): 30 wavfile = "/home/pi/program/API/alert.wav" 31 command = "aplay " + wavfile 32 subprocess.call(command, shell = True) 33 break 34 35 image_file = "/home/pi/program/API/capture.png" 36 image_command = "raspistill -w 500 -h 500 -t 5 -n -a 12 -q 10 -o " + image_file 37 subprocess.call(image_command, shell = True) 38 39 app = App(root) 40 41 root.mainloop()
試したこと
・撮影した画像の保存形式をjpg,pngなどに変更する
・キャンバスではなく、ラベルに貼り付ける
・ラベルにimage = img_tk の部分を削除し、textに変更した場合、textは表示された。
したがって、img_tkの変換あたりがうまくいっていないと思う
補足情報(FW/ツールのバージョンなど)
os raspbian
python3.5.3
RaspberryPi 3 model B