Advertisement
creativesamurai1982

PJ01_Python_pyCineScan.py

Mar 6th, 2024
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.63 KB | Software | 0 0
  1. # pyCineScan.py
  2. # by David Holt
  3. # RUN THIS SCRIPT AS ADMINISTRATOR
  4.  
  5. import datetime
  6. import logging
  7. import os
  8. import sys
  9. import time
  10.  
  11. import serial.tools.list_ports
  12. import win32ui
  13. from pywinauto.application import Application
  14.  
  15. # from py_cinescan_timer import time_functions
  16.  
  17.  
  18. # Set up logging configuration
  19. logging.basicConfig(filename='pyCineScan.log', level=logging.INFO, format='%(asctime)s - %(levelname)s: %(message)s')
  20.  
  21.  
  22. def time_functions(scan, moveFilm):
  23.     # Initialize total time
  24.     total_time = 0
  25.     count = 0
  26.  
  27.     # Start the loop
  28.     while True:
  29.         # Timing scan()
  30.         start_time_scan = time.perf_counter()
  31.         scan()
  32.         end_time_scan = time.perf_counter()
  33.  
  34.         # Calculating time taken for scan()
  35.         time_taken_scan = end_time_scan - start_time_scan
  36.  
  37.         # Timing moveFilm()
  38.         start_time_moveFilm = time.perf_counter()
  39.         moveFilm()
  40.         end_time_moveFilm = time.perf_counter()
  41.  
  42.         # Calculating time taken for moveFilm()
  43.         time_taken_moveFilm = end_time_moveFilm - start_time_moveFilm
  44.  
  45.         # Calculating total time for this iteration
  46.         iteration_total_time = time_taken_scan + time_taken_moveFilm
  47.  
  48.         # Adding the iteration time to the total time
  49.         total_time += iteration_total_time
  50.  
  51.         # Increment the count
  52.         count += 1
  53.  
  54.         # Convert total_time to days, hours, minutes, and seconds
  55.         total_time_td = datetime.timedelta(seconds=int(total_time))
  56.  
  57.         # Extract the number of days from total_time
  58.         total_days = total_time_td.days
  59.  
  60.         # Format the remaining time (hours, minutes, seconds) as HH:MM:SS
  61.         remaining_time_str = str(total_time_td - datetime.timedelta(days=total_days))
  62.  
  63.         # Print the individual times and current cumulative time in the desired format
  64.         print(f"Scan: {count}")
  65.         print()
  66.         print(f"Time taken for scan: {datetime.timedelta(seconds=int(time_taken_scan))}")
  67.         print(f"Time taken for moveFilm: {datetime.timedelta(seconds=int(time_taken_moveFilm))}")
  68.         print()
  69.         print(f"Total time elapsed: Day(s) {total_days}, {remaining_time_str}")
  70.         print()
  71.  
  72.  
  73. def get_ports():
  74.     os.system('cls')
  75.     ports = serial.tools.list_ports.comports()
  76.     return ports
  77.  
  78.  
  79. def findArduino(portsFound):
  80.     commPort = 'None'
  81.     numConnection = len(portsFound)
  82.  
  83.     for i in range(0, numConnection):
  84.         port = foundPorts[i]
  85.         strPort = str(port)
  86.  
  87.         if 'Arduino' in strPort:
  88.             splitPort = strPort.split(' ')
  89.             commPort = (splitPort[0])
  90.  
  91.     return commPort
  92.  
  93.  
  94. foundPorts = get_ports()
  95. connectPort = findArduino(foundPorts)
  96.  
  97. if connectPort != 'None':
  98.     ser = serial.Serial(connectPort, baudrate=9600, timeout=None)
  99.     print('Searching for Arduino UNO')
  100.     time.sleep(0.5)
  101.     print('Arduino UNO Connected to ' + connectPort)
  102.     print()
  103.     print('Waiting for the Arduino UNO to reset....')
  104.     time.sleep(3)
  105.  
  106.  
  107. else:
  108.     print('Serial connection FAILED!')
  109.     time.sleep(1.5)
  110.     sys.exit()
  111.  
  112. print('Arduino Is Ready')
  113. print()
  114.  
  115.  
  116. def clear_receive_buffer():
  117.     ser.reset_input_buffer()
  118.  
  119. # Main Menu Start
  120. def mainMenu():
  121.     os.system('cls')
  122.     print()
  123.     print("       Cine Scan")
  124.     print("       =-=-=-=-=")
  125.     print("     Version 1.0.0")
  126.     print("     By David Holt")
  127.     print()
  128.     print("Main Menu")
  129.     print("---------")
  130.     print()
  131.  
  132.     print("1 - Enable Takeup Reel.")
  133.     print("2 - Align Film With Scanner.")
  134.     print("3 - Start Scanning.")
  135.     print("4 - Advance by 10 frames.") # not used yet!
  136.     print("5 - Advance by 20 frames.") # not used yet!
  137.     print()
  138.     print("0. Quit ")
  139.     print()
  140.     while True:
  141.         try:
  142.             selection = int(input("Enter choice: "))
  143.             if selection == 1:
  144.                 one()  # 1 - Enable Takeup Reel
  145.                 break
  146.             elif selection == 2:
  147.                 global main_window
  148.                 two()  # 2 - Align Film With Scanner
  149.                 break
  150.             elif selection == 3:
  151.                 global main_window
  152.                 three()  # 3 - Start Scanning
  153.                 break
  154.             elif selection == 0:
  155.                 sys.exit(1)
  156.                 break
  157.             else:
  158.                 print("Invalid choice. Enter 1-3")
  159.                 mainMenu()
  160.         except ValueError:
  161.             print("Invalid choice. Enter 1-3")
  162.     exit
  163.  
  164.  
  165. def one():
  166.     ser.write(b'T')
  167.     mainMenu()
  168.  
  169.  
  170. def two():
  171.     ser.write(b'X')
  172.     mainMenu()
  173.  
  174.  
  175. def three():
  176.     global main_window
  177.     if main_window:
  178.         main_window.set_focus()
  179.     repeatFunc()
  180.     mainMenu()
  181.  
  182.  
  183. # Main Menu Finish
  184.  
  185.  
  186. def loadProg():
  187.     global main_window
  188.     # Start VueScan
  189.     print("Checking If VueScan Is Installed...")
  190.     time.sleep(0.5)
  191.     if os.path.exists(r"D:\Apps_Portable\VueScan"):
  192.         vuescan_file = r"D:\Apps_Portable\VueScan\vuescan.exe"
  193.         print("VueScan Found at " + vuescan_file)
  194.         print()
  195.         time.sleep(0.5)
  196.         print("Checking if VueScan is currently running...")
  197.         time.sleep(0.5)
  198.  
  199.         def vuescanExists(classname):
  200.             try:
  201.                 win32ui.FindWindow(classname, None)
  202.             except win32ui.error:
  203.                 return False
  204.             else:
  205.                 return True
  206.     else:
  207.         print("Can't find VueScan on your computer")
  208.         print("Terminating Script!")
  209.         print()
  210.         time.sleep(0.5)
  211.         sys.exit(1)
  212.  
  213.     if vuescanExists("wxWindowNR"):
  214.         print("VueScan already running")
  215.         print()
  216.         time.sleep(0.5)
  217.         print("Attempting to connect....")
  218.         time.sleep(0.5)
  219.         app = Application(backend="win32").connect(title_re="VueScan 9 x64*.", class_name="wxWindowNR")
  220.         time.sleep(6)
  221.         print('Connected')
  222.         print()
  223.         time.sleep(0.5)
  224.     else:
  225.         print("Loading VueScan...")
  226.         print()
  227.         app = Application(backend="win32").start(vuescan_file)
  228.         time.sleep(7.5)
  229.         app = Application(backend="win32").connect(title_re="VueScan 9 x64*.", class_name="wxWindowNR")
  230.  
  231.     # Select the main GUI window
  232.     print("Selecting main window...")
  233.     time.sleep(0.5)
  234.     main_window = app.wxWindowNR
  235.     print("Done")
  236.     print()
  237.  
  238.     time.sleep(2)
  239.     print("Loading scanner config...")
  240.     time.sleep(0.5)
  241.     menu_item = main_window.menu_item(u'&File->Load film_scan\tF1')
  242.     menu_item.select()
  243.     print("Done")
  244.     time.sleep(0.5)
  245.  
  246.  
  247. def scan():  # Main Scan Function, Use This When Not Testing.
  248.     try:
  249.         while True:
  250.             main_window.set_focus()
  251.             print()
  252.             print("-------------------------------------------------------------")
  253.             print()
  254.             button = main_window.Preview
  255.             button.wait('enabled', timeout=30)
  256.             print("Scanning Film Strip...", end='')
  257.             button.click()
  258.             button.wait_not('enabled', timeout=600)
  259.             print(" OK!")
  260.             print()
  261.             break
  262.     except KeyboardInterrupt:
  263.         print()
  264.         mainMenu()
  265.  
  266.  
  267. def moveFilm():  # Main moveFilm Function, Use This When Not Testing.
  268.     startMove = time.perf_counter()
  269.     ser.write(b'M')
  270.     clear_receive_buffer()  # Clear the receive buffer after sending the command
  271.     print()
  272.     print("Moving Film...", end='')
  273.     ser.read_until(b'S\r\n')
  274.     print(" OK!")
  275.     print()
  276.  
  277.  
  278. def repeatFunc():
  279.     time_functions(scan, moveFilm)
  280.  
  281.  
  282. if __name__ == '__main__':
  283.     get_ports()
  284.     print()
  285.     loadProg()
  286.     mainMenu()
  287.     repeatFunc()
  288.  
  289.     print("If your seeing this, Something has gone wrong!!!!")
  290.  
  291.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement