def limitHeight(fileImages): height_limit = HEIGHT_LIMIT added = [] deleted = [] for image in fileImages: whole = Image.open(image) if whole.size[1] > height_limit: results = image_tools.separate(image, 2) fileImages.remove(image) deleted.append(image) fileImages = results + fileImages added += results disk.cleanUp(image) del whole return fileImages
def createImage(target, first=True, index=0, movie=False, info=True, forced_width=0, forced_height=0): '''Generates a digital image based on the contents of the target folder.''' # Figure out the filenames based on the target name. base = git_info.getBaseRepoName(target) output_file_name = os.path.join( OUTPUT_FOLDER, base + '_%04d' % index + '.png') # Generate the list of paths to the target files that will be used # to generate the images. allFiles, neededFiles = getAllFiles([target], first) # Calculate the scale of the output based on the total number of files. global scale_div if first: scale_div = 1 - len(allFiles)/1000.0 if scale_div < .1: scale_div = .1 if NO_SCALE: scale_div = 1 print(('Scale = ' + str(scale_div))) # Generate the list of images that will be created. allFileImages = [] for _, f in enumerate(allFiles): dirname, filename = os.path.split(f) fileImage = os.path.join(TEMP_FOLDER, dirname.split( os.path.sep)[-1] + '_' + filename + '.png') allFileImages.append(fileImage) # Setup a temp folder for the files during processing. disk.makeFolder(TEMP_FOLDER) # Setup an output folder for the end result. disk.makeFolder(OUTPUT_FOLDER) # Generate the images themselves. drawImages(output_file_name, neededFiles, scale_div) # Get just the images we need, # ignoring other temp images that are generated. folderImages = os.listdir(TEMP_FOLDER) runImages = [] for image in folderImages: for match in allFileImages: if os.path.split(match)[1] in image: runImages.append(os.path.join(TEMP_FOLDER, image)) runImages.sort() if len(runImages) == 0: print("Error: No images found.") return None # Append all the files together into a long strip. pile_file = image_tools.pile(runImages) # Split the strip into each segments. separated_files = image_tools.separate(pile_file) disk.cleanUp(pile_file) # Stitch together the segments. connected = image_tools.connect(separated_files) disk.cleanUp(separated_files) # Force the height and width to be even numbers. if FORCE_EVEN: connected = image_tools.make_even(connected) # When making a movie adjust each frame to # the same height and width. if movie and FORCE_WIDTH and not first: img = Image.open(connected) if img.size[0] < forced_width: blank = drawBlank('blank.png', forced_width - img.size[0], img.size[1]) connected = image_tools.couple([connected, blank]) disk.cleanUp('blank.png') img = Image.open(connected) if img.size[1] < forced_height: blank = drawBlank( 'blank.png', img.size[0], forced_height-img.size[1]) connected = image_tools.pile([connected, blank]) disk.cleanUp('blank.png') img.close() del img # Fix the color and brightness. enhanced = image_tools.enhance([connected]) disk.cleanUp(connected) # Apply text overlay in the corner. if CORNER_TEXT: overlaid = cornerText(target, enhanced[0]) disk.cleanUp(enhanced) else: overlaid = enhanced[0] # Apply text overlay in the center. if info: overlaid2 = centerText(target, overlaid, extra=True) disk.cleanUp(overlaid) else: overlaid2 = overlaid disk.move(overlaid2, output_file_name) return output_file_name
def createImage(target, first=True, index=0, movie=False, info=True, alphabetical_sort=False, forced_width=0, forced_height=0): global scale_div base = git_info.getBaseRepoName(target) commit = git_info.getCommitNumber(target) output_file_name = OUTPUT_FOLDER + base + '_%04d' % index + '.png' allFiles, neededFiles = getAllFiles([target], first) if first: scale_div = 1 - len(allFiles) / 1000.0 if scale_div < .1: scale_div = .1 print(('Scale = ' + str(scale_div))) allFileImages = [] for i, f in enumerate(allFiles): dirname, filename = os.path.split(f) fileImage = os.path.join( TEMP_FOLDER, dirname.split(os.path.sep)[-1] + '_' + filename + '.png') allFileImages.append(fileImage) disk.makeFolder(TEMP_FOLDER) disk.makeFolder(OUTPUT_FOLDER) newFileImages = drawImages(output_file_name, neededFiles, scale_div) #newFileImages = limitHeight(newFileImages) # @TODO: Need modify allFiles to include filename changes from chopping files. folderImages = os.listdir(TEMP_FOLDER) runImages = [] for image in folderImages: for match in allFileImages: if os.path.split(match)[1] in image: runImages.append(os.path.join(TEMP_FOLDER, image)) runImages.sort() if alphabetical_sort: total_height = 0 batch = [] separated_files = [] letters = 'abcdefghijklmnopqrstuvwxyz' index = 0 for i, f in enumerate(runImages): img = Image.open(f) name = os.path.split(f)[1] letter = name[0].lower() print((letters[index])) if letter not in letters or letter != letters[index]: while letter != letters[index] and index < 25: index += 1 if TOTAL_HEIGHT - total_height > 0: batch.append( drawBlank('blank.png', MAX_WIDTH, TOTAL_HEIGHT - total_height)) pile_file = image_tools.pile(batch) batch = [] batch.append(f) separated_files.append(pile_file) total_height = img.size[1] else: batch.append(f) total_height += img.size[1] if len(batch) > 0: if TOTAL_HEIGHT - total_height > 0: batch.append( drawBlank('blank.png', MAX_WIDTH, TOTAL_HEIGHT - total_height)) pile_file = image_tools.pile(batch) batch = [] separated_files.append(pile_file) else: pile_file = image_tools.pile(allFileImages) separated_files = image_tools.separate(pile_file) disk.cleanUp(pile_file) connected = image_tools.connect(separated_files) disk.cleanUp(separated_files) if FORCE_EVEN: connected = image_tools.make_even(connected) if movie and FORCE_WIDTH and not first: img = Image.open(connected) if img.size[0] < forced_width: blank = drawBlank('blank.png', forced_width - img.size[0], img.size[1]) connected = image_tools.couple([connected, blank]) disk.cleanUp('blank.png') img = Image.open(connected) if img.size[1] < forced_height: blank = drawBlank('blank.png', img.size[0], forced_height - img.size[1]) connected = image_tools.pile([connected, blank]) disk.cleanUp('blank.png') enhanced = image_tools.enhance([connected]) disk.cleanUp(connected) if CORNER_TEXT: overlaid = cornerText(target, enhanced[0]) disk.cleanUp(enhanced) else: overlaid = enhanced[0] if info: overlaid2 = centerText(target, overlaid, extra=True) disk.cleanUp(overlaid) else: overlaid2 = overlaid # img = Image.open(overlaid2) # if img.size[0] > REALLY_BIG: # image_tools.scale(overlaid2,.5) disk.move(overlaid2, output_file_name) return output_file_name