import glob from PIL import Image from PIL import ImageDraw from PIL import ImageFont import os import zipfile import traceback import numpy as np global dir_to_extract_to, file_type, current_file def make_contact_sheet(fnames, dir_to_extract_to): # set the number of rows and columns per contact page ncols, nrows = 7, 14 photow, photoh = 200, 150 photo = (photow, photoh) marl, mart, marr, marb = [5, 5, 5, 5] padding = 1 """\ Make a contact sheet from a group of filenames: fnames A list of names of the image files ncols Number of columns in the contact sheet nrows Number of rows in the contact sheet photow The width of the photo thumbs in pixels photoh The height of the photo thumbs in pixels marl The left margin in pixels mart The top margin in pixels marr The right margin in pixels marb The bottom margin in pixels padding The padding between images in pixels returns a PIL image object. """ # Calculate the size of the output image, based on the # photo thumb sizes, margins, and padding marw = marl + marr marh = mart + marb padw = (ncols - 1) * padding padh = (nrows - 1) * padding isize = (ncols * photow + marw + padw, nrows * photoh + marh + padh) # Create the new image. The background doesn't have to be white white = (255, 255, 255) black = (0, 0, 0) font = ImageFont.truetype('C:\Windows\Fonts\\times.ttf') inew = Image.new(mode='RGB', size=isize, color=white) count = 0 # Insert each thumb: for irow in range(nrows): for icol in range(ncols): left = marl + icol * (photow + padding) right = left + photow upper = mart + irow * (photoh + padding) lower = upper + photoh try: if len(fnames) > 0: if fnames[count].endswith(".svg"): print("working with file - ", fnames[count]) output_file = fnames[count].split('\\')[-1] output_file = output_file.replace("svg", "png") output_file = dir_to_extract_to + "\\" + output_file print(f"output_file = {output_file}") svg2png_string = f"inkscape \"{fnames[count]}\" " \ f"--export-type=png " \ f"--export-background-opacity=200 " \ f"--export-filename=\"{output_file}\"" print(f"svg2png_string = {svg2png_string}") os.system(svg2png_string) img=Image.open(output_file) img_bbox =img.getbbox() os.remove(output_file) else: try: print("working with file - ", fnames[count]) img = Image.open(fnames[count]) img_bbox = img.getbbox() except Exception as e: print("Exception encountered in make_contact_sheet inner loop\n {e}\n") return inew width = img_bbox[2] - img_bbox[0] height = img_bbox[3] - img_bbox[1] # calculate a scaling factor depending on fitting the larger dimension into the thumbnail ratio = max(height / float(photoh), width / float(photow)) newWidth = int(width / ratio) newHeight = int(height / ratio) newSize = (newWidth, newHeight) img = img.resize(newSize) except Exception as e: print(f"\nException encountered in make_contact_sheet outer loop\n {e}\n") return inew new_left = left new_upper = upper if (newWidth < photow): new_left = (left + ((photow - newWidth) / 2)) if (newHeight < photoh): new_upper = (upper + ((photoh - newHeight) / 2)) new_tuple = (int(new_left), int(new_upper)) img = img.convert("RGB") basename = os.path.basename(fnames[count]) d1 = ImageDraw.Draw(img) meanpercent = np.mean(img) * 100 / 255 if meanpercent < 50: d1.text((0, 0), basename, fill=white, font=font) else: d1.text((0, 0), basename, fill=black, font=font) inew.paste(im=img, box=new_tuple, mask=None) count += 1 return inew def make_contact_sheet_per_image_type(files, file_type, page_count, dir_to_extract_to): while files: #try: files_page = files[:98] files = files[99:] #print(f"files_page = {files_page}") inew = make_contact_sheet(files_page, dir_to_extract_to) inew.save(f"CS_{page_count}_{file_type}.png") page_count = page_count + 1 # except Exception as e: # print(f"\nException encountered in make_contact_sheet_per_image_type\n {e}\n") # continue return page_count