Image Processing Library

Install Pillow

First, make sure you have Pillow installed. You can install it using pip:

bashCopy codepip install Pillow

Step 2: Create the Image Processing Library

Create a file named image_processing.py and add the following code:

pythonCopy codefrom PIL import Image, ImageFilter

class SimpleImageProcessor:
def __init__(self, image_path):
    self.image_path = image_path
    self.image = self.load_image()
def load_image(self):
    """Load an image from the specified path."""
    try:
        image = Image.open(self.image_path)
        print(f"Loaded image: {self.image_path}")
        return image
    except Exception as e:
        print(f"Error loading image: {e}")
        return None
def save_image(self, output_path):
    """Save the current image to a specified path."""
    try:
        self.image.save(output_path)
        print(f"Image saved to: {output_path}")
    except Exception as e:
        print(f"Error saving image: {e}")
def resize_image(self, width, height):
    """Resize the image to the specified width and height."""
    if self.image is not None:
        self.image = self.image.resize((width, height))
        print(f"Resized image to: {width}x{height}")
def rotate_image(self, angle):
    """Rotate the image by the specified angle."""
    if self.image is not None:
        self.image = self.image.rotate(angle)
        print(f"Rotated image by: {angle} degrees")
def apply_blur(self):
    """Apply a blur filter to the image."""
    if self.image is not None:
        self.image = self.image.filter(ImageFilter.BLUR)
        print("Applied blur filter to the image.")
def show_image(self):
    """Display the current image."""
    if self.image is not None:
        self.image.show()
def main():
image_path = input("Enter the path of the image file: ")
processor = SimpleImageProcessor(image_path)
while True:
    print("\nImage Processing Options:")
    print("1. Resize Image")
    print("2. Rotate Image")
    print("3. Apply Blur")
    print("4. Show Image")
    print("5. Save Image")
    print("6. Exit")
    choice = input("Choose an option (1-6): ")
    if choice == '1':
        width = int(input("Enter new width: "))
        height = int(input("Enter new height: "))
        processor.resize_image(width, height)
    elif choice == '2':
        angle = int(input("Enter rotation angle: "))
        processor.rotate_image(angle)
    elif choice == '3':
        processor.apply_blur()
    elif choice == '4':
        processor.show_image()
    elif choice == '5':
        output_path = input("Enter output file path: ")
        processor.save_image(output_path)
    elif choice == '6':
        print("Exiting the image processor.")
        break
    else:
        print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()

Step 3: Running the Image Processing Library

  1. Open your terminal (or command prompt).
  2. Navigate to the directory where you saved image_processing.py.
  3. Run the script using the command:bashCopy codepython image_processing.py

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *