images.remove(curr_slice) min_sim = 2000000000 index = 0 for i in range(len(images)): next_slice = images[i] x = min(slice_similarity(curr_slice, next_slice), slice_similarity(next_slice, curr_slice)) if x < min_sim: min_sim = x index = i next_slice = images[index] if slice_similarity(curr_slice, next_slice) < slice_similarity(next_slice, curr_slice): images[index] = curr_slice + images[index] else: images[index] = images[index] + curr_slice if __name__ == '__main__': """ Opens file and reads it using a list comprehension into a list called images. After reconstructing the image, this method then displays the image and saves it to the hard drive. """ IMGS_DIR = 'shredded/destination' images = [imageutils.load_image(filename) for filename in imageutils.files_in_directory(IMGS_DIR)] reconstruct_image(images) imageutils.show_all_images(*images) imageutils.save_image(images[0], 'final-destination')
""" files_names = imageutils.files_in_directory(directory) files = {} for file_name in files_names: img_arr = imageutils.load_image(file_name) files[file_name] = Segment(tuple(img_arr[0]), tuple(img_arr[-1])) return files if __name__ == '__main__': Segment = namedtuple('Segment', 'left_col, right_col') ''' ***** good decomp, but here's a very pythonic way to load the files: files = dict(zip(imageutils.files_in_directory(dir), map(imageutils.load_image, imageutils.files_in_directory(dir)))) ''' files = build_file_segments(DIR_NAME) ordered_file_names = build_image(files) files_list = [imageutils.load_image(file_name) for file_name in ordered_file_names.split('\t')] imageutils.show_all_images(*files_list, buffer_width=0) # prints message #print(''.join( [f[len(f) - 5] for f in ordered_file_names.split('\t')] ) )
for p1, p2 in zip(col1, col2): sim_score += pixel_sim(p1, p2) return sim_score def pixel_sim(pixel1, pixel2): """ Helper function that compares two Pixel objects by computing the sum of the square of each of the pixel differences, between red, green, blue, and alpha. This value is returned and used in column_sim. """ return sum([(pixel1.red - pixel2.red)**2, (pixel1.green - pixel2.green)**2, (pixel1.blue - pixel2.blue)**2, (pixel1.alpha - pixel2.alpha)**2]) if __name__ == '__main__': pass dirname = "shredded/grail20/" #reads in all the vertical slice image files and put them into a list files_list = imageutils.files_in_directory(dirname) image_files = [imageutils.load_image(file) for file in files_list] #reassembles the list of slices and renders the image reassemble_slices(image_files) imageutils.show_all_images(*image_files, buffer_width=0)
candidate_insert_left_shred = shred # if the same shred is the best option for right and left insertion # compare the relative similarities of each option if candidate_insert_left_shred == candidate_insert_right_shred: if max_right_to_left_sim > max_left_to_right_sim: ordered_shreds.append(candidate_insert_right_shred) shreds.remove(candidate_insert_right_shred) else: ordered_shreds.insert(0, candidate_insert_left_shred) shreds.remove(candidate_insert_left_shred) # if the right and left insertion shreds are different # insert both on their appropriate sides else: ordered_shreds.append(candidate_insert_right_shred) shreds.remove(candidate_insert_right_shred) ordered_shreds.insert(0, candidate_insert_left_shred) shreds.remove(candidate_insert_left_shred) # transform shreds into picture iu.show_all_images(*ordered_shreds) # an alternate approach that you could have used to doing this is writing a: # pixel similarity function # column similarity function # slice similarity function # best match function # merge all function # to simplify your code! but great job overall