import os, sys, zipfile, functions, glob, getopt from PIL import Image from PIL import ImageDraw from PIL import ImageFont from zipfile import BadZipFile import numpy as np def get_args(argv): location_arg = "" file_types_arg = "" opts, args = getopt.getopt(argv,"hl:f:",["ifile=","ofile="]) for opt, arg in opts: if opt == '-h': print ('test.py -l -f ') sys.exit() elif opt in ("-l", "--l"): location_arg = arg elif opt in ("-f", "--f"): file_types_arg = arg return location_arg, file_types_arg location, file_types = get_args(sys.argv[1:]) file_types = list(file_types.split(" ")) print(type(file_types)) file_types = list(file_types) print(f"location = {location}") print(f"file_types = {file_types}") print(type(file_types)) print(type(location)) Image.MAX_IMAGE_PIXELS = 1000000000 bad_zip_files = "" good_files = 0 bad_files = 0 global page_count, files, file_type, dir_to_extract_to # Don't bother reading in files we aren't going to use #if len(files) > ncols*nrows: files = files[:ncols*nrows] page_count = 1 files = [] make_contact_sheet_from_filetypes = True perform_unzipping = True make_contact_after_unzipping = True clean_up_temp_zip_storage = True test_zip_files = False if location == "": #location = "C:\zip_storage" #location = "C:\Marks\Cricut\Design Bundles" #location = "E:\Michelles Mac\Photos Library" location = "C:\\Marks\\Cricut\\Creativefabrica.com\\Backgrounds patterns and papers\\2023" if file_types == [''] or file_types == "": file_types = ["jpg", "gif", "png", "tiff", "svg"] dir_to_extract_to = f"C:\zip_storage" if not os.path.exists(dir_to_extract_to): os.makedirs(dir_to_extract_to) if test_zip_files: # First test all the zip files files = glob.glob(location + f"/**/*.zip", recursive=True) while files: entry = files.pop() try: the_zip_file = zipfile.ZipFile(entry) ret = the_zip_file.testzip() print(f"{entry} is a good zip file.") good_files = good_files + 1 except BadZipFile as e: print(f"****************** {entry} is a BAD zip file ************") bad_zip_files = bad_zip_files + str(entry) + "\n" bad_files = bad_files + 1 total_files = good_files + bad_files print(f"\nProcessed {total_files} files, of which:") print(f" {good_files} are good") print(f" {bad_files} are bad\n") if bad_files != 0: print("Bad zip files are as follows: ") print(bad_zip_files) print("Found a bad file, exiting") exit() if make_contact_sheet_from_filetypes: # we first process the file types that are in the directory listed without zip files for file_type in file_types: print(file_type) files = glob.glob(location + f"/**/*.{file_type}", recursive=True) print(f"files = {files}") page_count = functions.make_contact_sheet_per_image_type(files, file_type, page_count, dir_to_extract_to) if perform_unzipping: f = open("zip_files.txt", "w", encoding="utf-8") # now find and uncompress the zip files files = glob.glob(location + f"/**/*.zip", recursive=True) while files: filename = files.pop() print(f"Found {filename} to unzip") with zipfile.ZipFile(filename, 'r') as zf: list_of_filenames = zf.namelist() f.write(f"Zip File = {filename}\n{list_of_filenames}\n") for fileName in list_of_filenames: for file_type in file_types: if fileName.endswith(f".{file_type}"): print(f"Unzipping file - {fileName}") try: zf.extract(fileName, dir_to_extract_to) except Exception as e: print(f"\nException encountered in perform_unzipping\n {e}\n") continue f.close() if make_contact_after_unzipping: # now do the files that were in zips that are now in zip_storage for file_type in file_types: files = glob.glob(dir_to_extract_to + f"/**/*.{file_type}", recursive=True) print(files) page_count = functions.make_contact_sheet_per_image_type(files, file_type, page_count, dir_to_extract_to) if clean_up_temp_zip_storage: # clean up temp zip storage for dirpath, dirnames, filenames in os.walk(dir_to_extract_to): try: #print(f"dirpath = {dirpath}") #print(f"dirnames = {dirnames}") #print(f"filenames = {filenames}") for file in filenames: os.remove(f"{dirpath}\\{file}") if dirpath != "zip_storage": os.rmdir(dirpath) except Exception as e: print(f"\nException encountered in clean_up_temp_zip_storage\n {e}\n") continue print("Complete")