Untitled
unknown
plain_text
5 months ago
4.6 kB
8
Indexable
################################################################# # FILE : image_editor.py # WRITER : your_name , your_login , your_id # EXERCISE : intro2cs ex6 2025 # DESCRIPTION: A simple program that... # STUDENTS I DISCUSSED THE EXERCISE WITH: Bugs Bunny, b_bunny. # Daffy Duck, duck_daffy. # WEB PAGES I USED: www.looneytunes.com/lola_bunny # NOTES: ... ################################################################# ############################################################################## # Imports # ############################################################################## from ex6_helper import * from typing import Optional ############################################################################## # Functions # ############################################################################## def separate_channels(image: ColoredImage) -> List[SingleChannelImage]: output_lst = [] for k in range(len(image[0][0])): chan_lst = [] for i in range(len(image)): lst = [] for j in range(len(image[0])): lst.append(image[i][j][k]) chan_lst.append(lst) output_lst.append(chan_lst) return output_lst def combine_channels(channels: List[SingleChannelImage]) -> ColoredImage: comb_image = [[[None] * len(channels) for u in range(len(channels[0][0]))] for y in range(len(channels[0]))] for i in range(len(channels)): for j in range(len(channels[0])): for k in range(len(channels[0][0])): comb_image[j][k][i] = channels[i][j][k] return comb_image def RGB2grayscale(colored_image): lst = [] for i in range(len(colored_image)): for j in range(len(colored_image[i])): r, g, b = round(colored_image[i][j][0] * 0.299), round(colored_image[i][j][1] * 0.587), round(colored_image[i][j][2] * 0.114) lst.append([r + g + b]) return lst def blur_kernel(size: int) -> Kernel: lst = [] for i in range(size): ker_lst = [] for j in range(size): ker_lst.append(1 / (size * size)) lst.append(ker_lst) return lst def apply_kernel(image: SingleChannelImage, kernel: Kernel) -> SingleChannelImage: lst = [] for i in range(len(image)): for j in range(len(image[0])): lst [i][j] = 0 sum = 0 kernel_center = (len(kernel) + 1) // 2 for n in range(len(image)): for m in range(len(image[0])): #if kernel_center - 1 ==0 and m == 0 or kernel_center - 1 == 0 and n == 0 or kernel_center + 1 == len(image[0]) : pixel = 0 for i in range(len(kernel)): for j in range(len(kernel[0])): # sum += kernel[i][j] * image[i][j] pixel_i = n + i - kernel_center pixel_j = m + j - kernel_center if pixel_i >= 0 and pixel_i < len(image) and pixel_j >= 0 and pixel_j < len(image[0]): pixel += image[pixel_i][pixel_j] * kernel[i][j] else: pixel += image[n][m] * kernel[i][j] pixel = round(pixel) if pixel < 0: pixel = 0 if pixel > 255: pixel = 255 lst[n][m] = pixel return lst # lst.append([sum]) def bilinear_interpolation(image: SingleChannelImage, y: float, x: float) -> int: closest_y = int(y) closest_x = int(x) lim_x = int(len(image[0]) - 1) lim_y = int(len(image) - 1) a = image[closest_y][closest_x] b = image[closest_y][lim_x] c = image[lim_y][closest_x] d = image[lim_y][lim_x] delta_x = x - closest_x delta_y = y - closest_y pixel_val = round((a * (1 - delta_x) * (1 - delta_y) + b * delta_x * (1 - delta_y) + c * delta_y * (1 - delta_x) + d * delta_x * delta_y) + c * delta_x * delta_y) pixel = min(pixel_val, 255) pixel_val = max(pixel, 0) return int(pixel_val) def resize(image: SingleChannelImage, new_height: int, new_width: int) -> SingleChannelImage: def rotate_90(image: Image, direction: str) -> Image: ... def get_edges(image: SingleChannelImage, blur_size: int, block_size: int, c: float) -> SingleChannelImage: ... def quantize(image: SingleChannelImage, N: int) -> SingleChannelImage: ... if __name__ == '__main__': ...
Editor is loading...
Leave a Comment