utils.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import os
  2. import cv2
  3. import shutil
  4. import img2pdf
  5. import glob
  6. # PIL can also be used to convert the image set into PDFs.
  7. # However, using PIL requires opening each of the images in the set.
  8. # Hence img2pdf package was used, which is able to convert the entire image set into a PDF
  9. # without opening at once.
  10. def resize_image_frame(frame, resize_width):
  11. ht, wd, _ = frame.shape
  12. new_height = resize_width * ht / wd
  13. frame = cv2.resize(frame, (resize_width, int(new_height)), interpolation=cv2.INTER_AREA)
  14. return frame
  15. def create_output_directory(video_path, output_path, type_bgsub):
  16. vid_file_name = video_path.rsplit('/')[-1].split('.')[0]
  17. output_dir_path = os.path.join(output_path, vid_file_name, type_bgsub)
  18. # Remove the output directory if there is already one.
  19. if os.path.exists(output_dir_path):
  20. shutil.rmtree(output_dir_path)
  21. # Create output directory.
  22. os.makedirs(output_dir_path, exist_ok=True)
  23. print('Output directory created...')
  24. print('Path:', output_dir_path)
  25. print('***'*10,'\n')
  26. return output_dir_path
  27. def convert_slides_to_pdf(video_path, output_path):
  28. pdf_file_name = video_path.rsplit('/')[-1].split('.')[0]+'.pdf'
  29. output_pdf_path = os.path.join(output_path, pdf_file_name)
  30. print('Output PDF Path:', output_pdf_path)
  31. print('Converting captured slide images to PDF...')
  32. with open(output_pdf_path, "wb") as f:
  33. f.write(img2pdf.convert(sorted(glob.glob(f"{output_path}/*.png"))))
  34. print('PDF Created!')
  35. print('***'*10,'\n')