import tkinter as tk
import tkinter.ttk as ttk
from tkinter import font
from time import sleep
from picamera import PiCamera
import math
from io import BytesIO
from PIL import Image, ImageTk, ImageOps, ImageColor
import serial
import time
from datetime import datetime
import os
import tkinter.messagebox
# font and size for labels
standardFont="DejaVu Sans"
standardFontSize=16
# possible shutter speed values (in milliseconds and in seconds)
shutterSpeedValues=[200,500,1000,2000,4000,8000,16666,33333,66666,125000,250000,500000,1000000,2000000,4000000,6000000]
shutterSpeedValuesText=["1/5000","1/2000","1/1000","1/500","1/250","1/125","1/60","1/30","1/15","1/8","1/4","1/2","1","2","4","6"]
# possible focus preview values (in seconds)
focusPreviewTimeValues=[1,5,10,20,30]
focusPreviewTimeValuesText=["1","5","10","20","30"]
# filenames and folders
fileName="frame"
fileNameFullGlobal=""
fileFolder="capture/"
global previewImage
# preview image size and location
xPreviewSize=466
yPreviewSize=350
xPreviewStart=5
yPreviewStart=90
xPreviewEnd=xPreviewStart+xPreviewSize
yPreviewEnd=yPreviewStart+yPreviewSize
# access to camera via PiCamera
camera=PiCamera()
# full resolution for the 5MP camera
camera.resolution=(2592,1944)
# callback for mouse click
def callback(event):
xMouseClicked=event.x
yMouseClicked=event.y
print (xMouseClicked,yMouseClicked)
global xGridCellSelected
global yGridCellSelected
def drawPreviewImage():
print ("in drawPreviewImage")
canvas.delete("all")
global previewImage
previewImageOnCanvas=canvas.create_image(xPreviewStart,yPreviewStart,anchor=tk.NW,image=previewImage)
# commands
def focusCommand():
print ("in focusCommand")
print ("focusPreviewTime: "+str(focusPreviewTime.get()))
camera.shutter_speed=shutterSpeedValues[shutterSpeed.current()]
print ("shutterSpeed: "+str(shutterSpeedValues[shutterSpeed.current()]))
camera.start_preview()
sleep(focusPreviewTime.get())
camera.stop_preview()
def captureCommand():
print ("in captureCommand")
global previewImage
global previewImagePIL
print (shutterSpeedValues[shutterSpeed.current()])
stream=BytesIO()
camera.shutter_speed=shutterSpeedValues[shutterSpeed.current()]
# capture image
now=datetime.now()
dateTime=now.strftime("%Y-%m-%d-%H-%M-%S")
fileNameFull=fileName+"_"+dateTime
global fileNameFullGlobal
fileNameFullGlobal=fileNameFull
print("raspi captured image: "+fileNameFull)
camera.capture(stream,format='jpeg')
stream.seek(0)
image=Image.open(stream)
image.save(fileFolder+fileNameFull+".jpg","JPEG")
# generate the preview image
size=466,350
image.thumbnail(size,Image.ANTIALIAS)
previewImage=ImageTk.PhotoImage(image)
# need to redraw preview image
drawPreviewImage()
# refresh the window
root.update()
def histogramCommand():
print ("in histogramCommand")
print (fileNameFullGlobal+".jpg")
commandString="cd "+fileFolder+";"+"convert "+fileNameFullGlobal+".jpg"+" -define histogram:unique-colors=false histogram:histogram.gif"
os.system(commandString)
commandString="cd "+fileFolder+";"+"convert histogram.gif -resize 466x350 "+fileNameFullGlobal+"_hist.png"
os.system(commandString)
print (fileNameFullGlobal+"_hist.png")
imHist=Image.open(fileFolder+fileNameFullGlobal+"_hist.png")
global previewImage
previewImage=ImageTk.PhotoImage(imHist)
# need to redraw preview image
drawPreviewImage()
def frameavgCommand():
print ("in frameavgCommand")
print (frameavgNumber.get())
now=datetime.now()
dateTime=now.strftime("%Y-%m-%d-%H-%M-%S")
currentFANumber=1
currentFANumberLabel=tk.Label(root,bg="black",fg="white",text=str(currentFANumber))
currentFANumberLabel.place(x=520,y=330)
currentFANumberLabel.config(font=(standardFont,standardFontSize))
for fa in range(frameavgNumber.get()):
currentFANumber=fa+1
currentFANumberLabel.configure(text=str(currentFANumber))
canvas.update_idletasks()
stream=BytesIO()
camera.shutter_speed=shutterSpeedValues[shutterSpeed.current()]
camera.capture(stream,format='jpeg')
stream.seek(0)
image=Image.open(stream)
image.save(fileFolder+"fa_"+dateTime+"_"+str(fa)+".jpg","JPEG")
currentFANumberLabel.configure(text="P")
canvas.update_idletasks()
fileNameFull=fileName+"_"+dateTime
global fileNameFullGlobal
fileNameFullGlobal=fileNameFull+"_fa"
commandString="cd "+fileFolder+";"+"convert fa_"+dateTime+"_*"+" -evaluate-sequence mean "+fileNameFull+"_fa.jpg"
os.system(commandString)
imFa=Image.open(fileFolder+fileNameFull+"_fa.jpg")
size=466,350
imFa.thumbnail(size,Image.ANTIALIAS)
global previewImage
previewImage=ImageTk.PhotoImage(imFa)
# need to redraw preview image
drawPreviewImage()
currentFANumberLabel.configure(text="")
canvas.update_idletasks()
print ("done frameaveraging")
def blackBirchCommand():
print ("in blackBirchCommand")
tk.messagebox.showinfo("About","blackBirch camera\ncontrol software\nA.M. Zsaki\nattila@amzsaki.com")
root=tk.Tk()
root.geometry("540x450+0+0")
root.title("blackBirch")
standardFontDefined=font.Font(family=standardFont,size=standardFontSize)
root.option_add("*Font",standardFontDefined)
canvas=tk.Canvas(root,bg="black",width=540,height=450)
canvas.bind("<Button-1>",callback)
canvas.pack()
shutterSpeedLabel=tk.Label(root,bg="black",fg="white",text="Shutter Speed (s)")
shutterSpeedLabel.place(x=5,y=1)
shutterSpeedLabel.config(font=(standardFont,standardFontSize))
shutterSpeedCurrentValue=0
shutterSpeed=ttk.Combobox(root,textvariable=shutterSpeedCurrentValue,values=shutterSpeedValuesText,exportselection=0)
shutterSpeed.place(x=5,y=50)
shutterSpeed.config(font=(standardFont,standardFontSize))
shutterSpeed.current(5)
focusPreviewTimeLabel=tk.Label(root,bg="black",fg="white",text="Preview (s)")
focusPreviewTimeLabel.place(x=310,y=1)
focusPreviewTimeLabel.config(font=(standardFont,standardFontSize))
focusPreviewTime=tk.Scale(root,bg="black",fg="white",from_=5,to=20,tickinterval=0,orient=tk.HORIZONTAL,resolution=5)
focusPreviewTime.set(5)
focusPreviewTime.place(x=310,y=30)
focusIcon=tk.PhotoImage(file="icons/focus_45_45.gif")
focusButton=tk.Button(root,image=focusIcon,width=45,height=45,command=focusCommand)
focusButton.place(x=480,y=90)
focusButton.config(font=(standardFont,standardFontSize))
captureIcon=tk.PhotoImage(file="icons/capture_45_45.gif")
captureButton=tk.Button(root,image=captureIcon,width=45,height=45,command=captureCommand)
captureButton.place(x=480,y=150)
captureButton.config(font=(standardFont,standardFontSize))
histogramIcon=tk.PhotoImage(file="icons/histogram_45_45.gif")
histogramButton=tk.Button(root,image=histogramIcon,width=45,height=45,command=histogramCommand)
histogramButton.place(x=480,y=210)
histogramButton.config(font=(standardFont,standardFontSize))
frameavgIcon=tk.PhotoImage(file="icons/frameavg_45_45.gif")
frameavgButton=tk.Button(root,image=frameavgIcon,width=45,height=45,command=frameavgCommand)
frameavgButton.place(x=480,y=270)
frameavgButton.config(font=(standardFont,standardFontSize))
frameavgNumber=tk.Scale(root,bg="black",fg="white",from_=1,to=8,tickinterval=0,orient=tk.VERTICAL,resolution=1)
frameavgNumber.set(1)
frameavgNumber.place(x=480,y=330)
previewImagePIL=Image.open("black_background.gif")
previewImage=ImageTk.PhotoImage(previewImagePIL)
previewImageOnCanvas=canvas.create_image(xPreviewStart,yPreviewStart,anchor=tk.NW,image=previewImage)
blackBirchIcon=tk.PhotoImage(file="icons/blackBirchIcon_45_45.gif")
blackBirchButton=tk.Button(root,image=blackBirchIcon,width=45,height=45,command=blackBirchCommand)
blackBirchButton.place(x=480,y=30)
blackBirchButton.config(font=(standardFont,standardFontSize))
root.mainloop()