import matplotlib.pyplot import csv class ImageProcessor(): def read_image(self, file_name): with open(file_name, newline='') as f: reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC) image = [] for row in reader: rowlist = [] for item in row: rowlist.append(item) image.append(rowlist) return image def write_image(self, file_name, image): with open(file_name, 'w', newline='') as f: writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC) for row in image: writer.writerow(row) def smooth_image(self, image, neighbourhood_diameter): processed_image = [] for i in range(len(image)): row_list = [] for j in range(len(image[i])): row_list.append(0) processed_image.append(row_list) if neighbourhood_diameter < 1: neighbourhood_diameter = 1 if neighbourhood_diameter % 2 == 0: neighbourhood_diameter = neighbourhood_diameter + 1 neighbourhood_radius = int((neighbourhood_diameter - 1) / 2) for i in range(0 + neighbourhood_radius, len(image) - neighbourhood_radius): for j in range(0 + neighbourhood_radius, len(image[0]) - neighbourhood_radius): sum = 0 for y in range(i - neighbourhood_radius, i + neighbourhood_radius + 1): for x in range(j - neighbourhood_radius, j + neighbourhood_radius + 1): sum += image[y][x] average = sum / (neighbourhood_diameter * neighbourhood_diameter) processed_image[i][j] = average return processed_image def display_images(self, images): matplotlib.pyplot.ylim(0, len(images[0][0])) matplotlib.pyplot.xlim(0, len(images[0][0][0])) for i in range(len(images)): sp1 = matplotlib.pyplot.subplot(1, 2, i + 1) sp1.set_title(images[i][1]) matplotlib.pyplot.imshow(images[i][0]) matplotlib.pyplot.show()