Python-Gphoto2
概要
コマンド操作でカメラの制御を行います。 iso感度やシャッタスピード等の設定も可能です.
インストール
- debian
apt install gphoto2
- Python
(My Program)
使用例
class data:
def __init__(self):
self.buffer = [b"", b""]
self.e = [False, False]
self.f = False
def set(self, d, val=0):
self.buffer[val] = (b"--frame\r\n"+
b"Content-Type: image/jpeg\r\n\r\n" + d + b"\r\n")
def get(self, val=0):
return self.buffer[val]
def start(self, val=0):
self.e[val] = False
def finish(self, val=0):
self.e[val] = True
def end(self, val=0):
return self.e[val]
def setFinal(self):
self.f = True
def final(self):
return self.f
datas = data()
カメラ設定最適化・初期化
class GPhoto2:
def __init__(self):
self.iso = 3200
self.shutterspeed = 10
self.aperture = 2.8
self.capturetarget = 0
self.autofocusdrive = 0
self.manualfocusdrive = 3
self.eosremoterelease = 0
self.viewfinder = 2
self.autoexposuremode = 3
self.file = ""
self.fileNames = []
self.log = ""
self.json = []
self.setter = {"default":True,"capturetarget":False,"autofocusdrive":False,
"manualfocusdrive":False,"eosremoterelease":False,
"viewfinder":False,"autoexposuremode":False}
self.finish = False
self.turn = 0
self.log2 = "";
self.set();
def default(self):
self.iso = "Auto"
self.shutterspeed = "1/6"
self.aperture = 2.8
self.capturetarget = 0
self.autofocusdrive = 0
self.manualfocusdrive = 3
self.eosremoterelease = 0
self.viewfinder = 2
self.autoexposuremode = 3
def setFileName(self,name):
self.fileNames.append(name)
def getFileName(self):
return self.fileNames
def setting(self):
print("setting")
if self.setter["default"]:
self.log = ""
self.log = subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--set-config' ,f'shutterspeed={self.shutterspeed}'],cwd=r"/home/azuma/public_html/public/",stdout=subprocess.PIPE,shell=False).stdout
self.log = subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--set-config' ,f'aperture={self.aperture}'],cwd=r"/home/azuma/public_html/public/",stdout=subprocess.PIPE,shell=False).stdout
self.log = subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--set-config' ,f'iso={self.iso}'],cwd=r"/home/azuma/public_html/public/" ,stdout=subprocess.PIPE,shell=False).stdout
self.setter["default"] = False
if self.setter["capturetarget"]:
self.log = subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--set-config' ,f'capturetarget={self.capturetarget}'],cwd=r"/home/azuma/public_html/public/",stdout=subprocess.PIPE,shell=False).stdout
self.setter["capturetarget"] = False
if self.setter["autofocusdrive"]:
self.log = subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--set-config' ,f'autofocusdrive={self.autofocusdrive}'],cwd=r"/home/azuma/public_html/public/",stdout=subprocess.PIPE,shell=False).stdout
self.setter["autofocusdrive"] = False
if self.setter["manualfocusdrive"]:
self.log = subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--set-config' ,f'manualfocusdrive={self.manualfocusdrive}'],cwd=r"/home/azuma/public_html/public/",stdout=subprocess.PIPE,shell=False).stdout
self.setter["manualfocusdrive"] = False
if False:
#if self.setter["eosremoterelease"]:
self.log = subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--set-config' ,f'eosremoterelease={self.eosremoterelease}'],cwd=r"/home/azuma/public_html/public/",stdout=subprocess.PIPE,shell=False).stdout
self.setter["eosremoterelease"] = False
if self.setter["viewfinder"]:
self.log = subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--set-config' ,f'viewfinder={self.viewfinder}'],cwd=r"/home/azuma/public_html/public/",stdout=subprocess.PIPE,shell=False).stdout
self.setter["viewfinder"] = False
if self.setter["autoexposuremode"]:
self.log = subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--set-config' ,f'autoexposuremode={self.autoexposuremode}'],cwd=r"/home/azuma/public_html/public/",stdout=subprocess.PIPE,shell=False).stdout
self.setter["autoexposuremode"] = False
#- 設定
# 各設定や制限などによる代入をコントロールするため個々に設定
def setIso(self,iso):
self.iso = iso
self.setter["default"] = True
def setShutterspeed(self,shutterspeed):
self.shutterspeed = shutterspeed
self.setter["default"] = True
def setAperture(self,aperture):
self.aperture = aperture
self.setter["default"] = True
def setCapturetarget(self,capturetarget):
self.capturetarget = capturetarget
self.setter["capturetarget"] = True
def setAutofocusdrive(self,autofocusdrive):
self.autofocusdrive = autofocusdrive
self.setter["autofocusdrive"] = True
def setManualfocusdrive(self,manualfocusdrive):
self.manualfocusdrive = manualfocusdrive
self.setter["manualfocusdrive"] = True
def setEosremoterelease(self,eosremoterelease):
self.eosremoterelease = eosremoterelease
self.setter["eosremoterelease"] = True
def setViewfinder(self,viewfinder):
self.viewfinder = viewfinder
self.setter["viewfinder"] = True
def setAutoexposuremode(self,autoexposuremode):
self.autoexposuremode = autoexposuremode
self.setter["autoexposuremode"] = True
#gphoto2にあるパラメータを取得
def set(self):
self.json = ["[shutterspeed]\n"+str(subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--get-config' ,'shutterspeed'],cwd=r"/home/azuma/public_html/public/",stdout=subprocess.PIPE,shell=False).stdout),
"\n\n[iso]\n"+str(subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--get-config' ,'iso'],cwd=r"/home/azuma/public_html/public/",stdout=subprocess.PIPE,shell=False).stdout),
"\n\n[aperture]\n"+str(subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--get-config' ,'aperture'],cwd=r"/home/azuma/public_html/public/",stdout=subprocess.PIPE,shell=False).stdout),
"\n\n[capturetarget]\n"+str(subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--get-config' ,'capturetarget'],cwd=r"/home/azuma/public_html/public/",stdout=subprocess.PIPE,shell=False).stdout),
"\n\n[autofocusdrive]\n"+str(subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--get-config' ,'autofocusdrive'],cwd=r"/home/azuma/public_html/public/",stdout=subprocess.PIPE,shell=False).stdout),
"\n\n[manualfocusdrive]\n"+str(subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--get-config' ,'manualfocusdrive'],cwd=r"/home/azuma/public_html/public/",stdout=subprocess.PIPE,shell=False).stdout),
"\n\n[eosremoterelease]\n Sorry... No Parameters",
"\n\n[viewfinder]\n"+str(subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--get-config' ,'viewfinder'],cwd=r"/home/azuma/public_html/public/",stdout=subprocess.PIPE,shell=False).stdout),
"\n\n[autoexposuremode]\n"+str(subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--get-config' ,'autoexposuremode'],cwd=r"/home/azuma/public_html/public/",stdout=subprocess.PIPE,shell=False).stdout)
]
#- 取得
def get(self):
return self.json
def getIso(self):
return self.iso
def getShutterspeed(self):
return self.shutterspeed
def getAperture(self):
return self.aperture
def getCapturetarget(self):
return self.capturetarget
def getAutofocusdrive(self):
return self.autofocusdrive
def getManualfocusdrive(self):
return self.manualfocusdrive
def getEosremoterelease(self):
return self.eosremoterelease
def getViewfinder(self):
return self.viewfinder
def getAutoexposuremode(self):
return self.autoexposuremode
def AllGet(self):
jsons = [
["Iso",self.getIso()],
["Shutterspeed",self.getShutterspeed()],
["Aperture",self.getAperture()],
["Capturetarget",self.getCapturetarget()],
["Autofocusdrive",self.getAutofocusdrive()],
["Manualfocusdrive",self.getManualfocusdrive()],
["Eosremoterelease",self.getEosremoterelease()],
["Viewfinder",self.getViewfinder()],
["Autoexposuremode",self.getAutoexposuremode()]
]
return jsons
def logs(self):
return self.log
def setFinish(self):
self.finish = True
def getFinish(self):
return self.finish
def filename(self):
if self.file:
if self.turn:
self.file = self.file+"_"+str(self.turn)
path = f"/保存ディレクトリ/{self.file}.jpg"
self.files.append(path)
return f"--filename {path}"
else:
return ""
def getfiles(self):
return self.files
def setTurn(self,turn):
self.turn = turn
def getTurn(self):
return self.turn
def cons(self,msg):
print(str(msg))
self.log2 = str(msg);
def getCons(self):
return self.log2
gphoto = GPhoto2()
カメラ設定最適化・初期化
def video1(threadedname, GPhoto2_datas,d):
savePath = "保存するパス"
print("video1起動")
try:
GPhoto2_datas.setting()
desc = GPhoto2_datas.AllGet()
c = subprocess.run(['/var/lib/snapd/snap/bin/gphoto2', '--force-overwrite','--capture-image-and-download', '--filename', f'{savePath}/capt0000.jpg'],cwd=r"/保存するパス/",shell=False).stdout
GPhoto2_datas.cons(c)
with open(f"/{savePath}/capt0000.jpg",'rb') as f:
img = f.read()
d.set(d=img, val=1)
#時間取得
dt_now = datetime.datetime.now()
month = '{:0=2}'.format(dt_now.month)
day = '{:0=2}'.format(dt_now.day)
hour = '{:0=2}'.format(dt_now.hour)
minute = '{:0=2}'.format(dt_now.minute)
second = '{:0=2}'.format(dt_now.second)
fileName = f"{dt_now.year}{month}{day}{hour}{minute}{second}"
filepath = f"{savePath}/history/Y{dt_now.year}/M{month}/D{day}/{fileName}.jpg"
file_path = os.path.dirname(filepath)
if not os.path.exists(file_path):
os.makedirs(file_path)
try:
with open(filepath,'wb') as wf:
wf.write(img)
GPhoto2_datas.setFileName(fileName)
except:
pass
txt = ""
try:
with open(filepath[:-4]+".txt","w",encoding="utf-8") as tx:
for j in desc:
try:
txt += j[0]+" : "+str(j[1])
except:
txt += "--error--"
txt += "\n"
tx.write(txt)
except:
pass
except:
print("Cameraエラー")
print("video1終了")