Question: import numpy as np import struct import os def ReadBMP(filename): print(os.getcwd()) with open(filename, 'rb') as f: header = f.read(54) width, height = struct.unpack('Ii', header[18:26]) bits_per_pixel

import numpy as np import struct import os def ReadBMP(filename): print(os.getcwd()) with open(filename, 'rb') as f: header = f.read(54) width, height = struct.unpack('Ii', header[18:26])

bits_per_pixel = struct.unpack('H', header[28:30])[0] if bits_per_pixel != 24: raise ValueError("Only 24-bit BMP images are supported.")

pixel_data = f.read()

pixels = np.frombuffer(pixel_data, dtype=np.uint8) pixels = pixels.reshape((height, width, 3))

return pixels

def writeBMP(pixels, filename): height, width, _ = pixels.shape data_size = width * height * 3 file_size = data_size + 54

header = struct.pack('

pixel_data = pixels.tobytes()

with open(filename, 'wb') as f: f.write(header) f.write(pixel_data)

class ImageProcesser: def __init__(self, filename): self.pixelgrid = np.array(ReadBMP(filename)) self.height, self.width, _ = self.pixelgrid.shape

def save(self, new_name): writeBMP(self.pixelgrid.tolist(), new_name)

def invert(self): self.pixelgrid = 255 - self.pixelgrid

def displayChannel(self, channel): if channel == 'r': self.pixelgrid[:, :, (1, 2)] = 0 elif channel == 'g': self.pixelgrid[:, :, (0, 2)] = 0 elif channel == 'b': self.pixelgrid[:, :, (0, 1)] = 0

def flip(self, axis): if axis == 'v': self.pixelgrid = np.flip(self.pixelgrid, axis=0) elif axis == 'h': self.pixelgrid = np.flip(self.pixelgrid, axis=1)

def grayscale(self): gray = np.mean(self.pixelgrid, axis=2, dtype=int) self.pixelgrid = np.repeat(gray[:, :, np.newaxis], 3, axis=2)

def brightness(self, operation): if operation == '+': self.pixelgrid = np.clip(self.pixelgrid + 25, 0, 255) elif operation == '-': self.pixelgrid = np.clip(self.pixelgrid - 25, 0, 255)

def contrast(self, operation): if operation == '+': C = 45 elif operation == '-': C = -45

factor = 255.0 * (C + 255) / (255 * (259 - C)) self.pixelgrid = np.clip(factor * (self.pixelgrid - 128) + 128, 0, 255).astype(int)

def main(): filename = input("Enter the name of the BMP file: ") img = ImageProcesser(filename)

while True: print("1. Invert colors 2. Display channel 3. Flip image 4. Grayscale 5. Brightness 6. Contrast 7. Save 8. Quit") choice = int(input("Enter your choice: "))

if choice == 1: img.invert() elif choice == 2: channel = input("Enter channel to display (r/g/b): ") img.displayChannel(channel) elif choice == 3: axis = input("Enter axis to flip image (v/h): ") img.flip(axis) elif choice == 4: img.grayscale() elif choice == 5: operation = input("Enter operation to adjust brightness (+/-): ") img.brightness(operation) elif choice == 6: operation = input("Enter operation to adjust contrast (+/-): ") img.contrast(operation) elif choice == 7: new_name = input("Enter new file name: ") img.save(new_name) elif choice == 8: break else: print("Invalid choice. Please enter a number between 1 and 8.")

if __name__ == "__main__": main()

how do I fix this

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!