Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # pyCineScan.py
- # by David Holt
- # RUN THIS SCRIPT AS ADMINISTRATOR
- import datetime
- import logging
- import os
- import sys
- import time
- import serial.tools.list_ports
- import win32ui
- from pywinauto.application import Application
- # from py_cinescan_timer import time_functions
- # Set up logging configuration
- logging.basicConfig(filename='pyCineScan.log', level=logging.INFO, format='%(asctime)s - %(levelname)s: %(message)s')
- def time_functions(scan, moveFilm):
- # Initialize total time
- total_time = 0
- count = 0
- # Start the loop
- while True:
- # Timing scan()
- start_time_scan = time.perf_counter()
- scan()
- end_time_scan = time.perf_counter()
- # Calculating time taken for scan()
- time_taken_scan = end_time_scan - start_time_scan
- # Timing moveFilm()
- start_time_moveFilm = time.perf_counter()
- moveFilm()
- end_time_moveFilm = time.perf_counter()
- # Calculating time taken for moveFilm()
- time_taken_moveFilm = end_time_moveFilm - start_time_moveFilm
- # Calculating total time for this iteration
- iteration_total_time = time_taken_scan + time_taken_moveFilm
- # Adding the iteration time to the total time
- total_time += iteration_total_time
- # Increment the count
- count += 1
- # Convert total_time to days, hours, minutes, and seconds
- total_time_td = datetime.timedelta(seconds=int(total_time))
- # Extract the number of days from total_time
- total_days = total_time_td.days
- # Format the remaining time (hours, minutes, seconds) as HH:MM:SS
- remaining_time_str = str(total_time_td - datetime.timedelta(days=total_days))
- # Print the individual times and current cumulative time in the desired format
- print(f"Scan: {count}")
- print()
- print(f"Time taken for scan: {datetime.timedelta(seconds=int(time_taken_scan))}")
- print(f"Time taken for moveFilm: {datetime.timedelta(seconds=int(time_taken_moveFilm))}")
- print()
- print(f"Total time elapsed: Day(s) {total_days}, {remaining_time_str}")
- print()
- def get_ports():
- os.system('cls')
- ports = serial.tools.list_ports.comports()
- return ports
- def findArduino(portsFound):
- commPort = 'None'
- numConnection = len(portsFound)
- for i in range(0, numConnection):
- port = foundPorts[i]
- strPort = str(port)
- if 'Arduino' in strPort:
- splitPort = strPort.split(' ')
- commPort = (splitPort[0])
- return commPort
- foundPorts = get_ports()
- connectPort = findArduino(foundPorts)
- if connectPort != 'None':
- ser = serial.Serial(connectPort, baudrate=9600, timeout=None)
- print('Searching for Arduino UNO')
- time.sleep(0.5)
- print('Arduino UNO Connected to ' + connectPort)
- print()
- print('Waiting for the Arduino UNO to reset....')
- time.sleep(3)
- else:
- print('Serial connection FAILED!')
- time.sleep(1.5)
- sys.exit()
- print('Arduino Is Ready')
- print()
- def clear_receive_buffer():
- ser.reset_input_buffer()
- # Main Menu Start
- def mainMenu():
- os.system('cls')
- print()
- print(" Cine Scan")
- print(" =-=-=-=-=")
- print(" Version 1.0.0")
- print(" By David Holt")
- print()
- print("Main Menu")
- print("---------")
- print()
- print("1 - Enable Takeup Reel.")
- print("2 - Align Film With Scanner.")
- print("3 - Start Scanning.")
- print("4 - Advance by 10 frames.") # not used yet!
- print("5 - Advance by 20 frames.") # not used yet!
- print()
- print("0. Quit ")
- print()
- while True:
- try:
- selection = int(input("Enter choice: "))
- if selection == 1:
- one() # 1 - Enable Takeup Reel
- break
- elif selection == 2:
- global main_window
- two() # 2 - Align Film With Scanner
- break
- elif selection == 3:
- global main_window
- three() # 3 - Start Scanning
- break
- elif selection == 0:
- sys.exit(1)
- break
- else:
- print("Invalid choice. Enter 1-3")
- mainMenu()
- except ValueError:
- print("Invalid choice. Enter 1-3")
- exit
- def one():
- ser.write(b'T')
- mainMenu()
- def two():
- ser.write(b'X')
- mainMenu()
- def three():
- global main_window
- if main_window:
- main_window.set_focus()
- repeatFunc()
- mainMenu()
- # Main Menu Finish
- def loadProg():
- global main_window
- # Start VueScan
- print("Checking If VueScan Is Installed...")
- time.sleep(0.5)
- if os.path.exists(r"D:\Apps_Portable\VueScan"):
- vuescan_file = r"D:\Apps_Portable\VueScan\vuescan.exe"
- print("VueScan Found at " + vuescan_file)
- print()
- time.sleep(0.5)
- print("Checking if VueScan is currently running...")
- time.sleep(0.5)
- def vuescanExists(classname):
- try:
- win32ui.FindWindow(classname, None)
- except win32ui.error:
- return False
- else:
- return True
- else:
- print("Can't find VueScan on your computer")
- print("Terminating Script!")
- print()
- time.sleep(0.5)
- sys.exit(1)
- if vuescanExists("wxWindowNR"):
- print("VueScan already running")
- print()
- time.sleep(0.5)
- print("Attempting to connect....")
- time.sleep(0.5)
- app = Application(backend="win32").connect(title_re="VueScan 9 x64*.", class_name="wxWindowNR")
- time.sleep(6)
- print('Connected')
- print()
- time.sleep(0.5)
- else:
- print("Loading VueScan...")
- print()
- app = Application(backend="win32").start(vuescan_file)
- time.sleep(7.5)
- app = Application(backend="win32").connect(title_re="VueScan 9 x64*.", class_name="wxWindowNR")
- # Select the main GUI window
- print("Selecting main window...")
- time.sleep(0.5)
- main_window = app.wxWindowNR
- print("Done")
- print()
- time.sleep(2)
- print("Loading scanner config...")
- time.sleep(0.5)
- menu_item = main_window.menu_item(u'&File->Load film_scan\tF1')
- menu_item.select()
- print("Done")
- time.sleep(0.5)
- def scan(): # Main Scan Function, Use This When Not Testing.
- try:
- while True:
- main_window.set_focus()
- print()
- print("-------------------------------------------------------------")
- print()
- button = main_window.Preview
- button.wait('enabled', timeout=30)
- print("Scanning Film Strip...", end='')
- button.click()
- button.wait_not('enabled', timeout=600)
- print(" OK!")
- print()
- break
- except KeyboardInterrupt:
- print()
- mainMenu()
- def moveFilm(): # Main moveFilm Function, Use This When Not Testing.
- startMove = time.perf_counter()
- ser.write(b'M')
- clear_receive_buffer() # Clear the receive buffer after sending the command
- print()
- print("Moving Film...", end='')
- ser.read_until(b'S\r\n')
- print(" OK!")
- print()
- def repeatFunc():
- time_functions(scan, moveFilm)
- if __name__ == '__main__':
- get_ports()
- print()
- loadProg()
- mainMenu()
- repeatFunc()
- print("If your seeing this, Something has gone wrong!!!!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement