Simple graphics. explain simple graphic and Image processing, manipulation in detail.

Simple Graphics

 

Simple graphics typically involve drawing basic shapes, lines, and colors on a canvas. In Python, the `turtle` module is often used for educational purposes, allowing users to create simple graphics with a turtle that moves around a canvas.

 

### Example using Turtle:

 

```python

import turtle

 

# Create a turtle

my_turtle = turtle.Turtle()

 

# Draw a square

for _ in range(4):

    my_turtle.forward(100)

    my_turtle.left(90)

 

# Close the turtle graphics window when clicked

turtle.done()

 

This code creates a square using the `turtle` module. It moves the turtle forward and turns left in a loop to create each side of the square.

 

## Image Processing and Manipulation:

 

Image processing involves applying various techniques to modify and enhance images. In Python, the `PIL` (Python Imaging Library) module is commonly used for basic image processing. The `PIL` module has been succeeded by `Pillow`, which is a fork of the original library.

 

### Installing Pillow:

 

```bash

pip install Pillow

```

 

### Example using Pillow:

 

```python

from PIL import Image, ImageFilter

 

# Open an image file

image = Image.open("example.jpg")

 

# Display the image

image.show()

 

# Convert the image to grayscale

gray_image = image.convert("L")

 

# Apply a blur filter

blurred_image = gray_image.filter(ImageFilter.BLUR)

 

# Save the modified image

blurred_image.save("blurred_example.jpg")

 

# Display the modified image

blurred_image.show()

```

 

In this example:

 

- We open an image file using `Image.open`.

- Convert the image to grayscale using `convert`.

- Apply a blur filter using `filter`.

- Save the modified image using `save`.

- Display the modified image using `show`.

 

## Further Image Processing Techniques:### 1. Resizing:

   - Change the size of an image.

 

   ```python

   resized_image = image.resize((width, height))

   ```

 

### 2. Rotating:

   - Rotate an image by a specified angle.

 

   ```python

   rotated_image = image.rotate(angle)

 

   ```### 3. Cropping:**

   - Extract a region from an image.

 

   ```python

   cropped_image = image.crop((left, top, right, bottom))

   ```

 

### 4. Filtering:

   - Apply various filters for effects.

 

   ```python

   filtered_image = image.filter(ImageFilter.FILTER_NAME)

   ```

 

### 5. Drawing on Images:

   - Add text or draw shapes on an image.

 

   ```python

   draw = ImageDraw.Draw(image)

   draw.text((x, y), "Hello, World!", fill=color, font=font)

   ```

 

### 6. Image Enhancements:

   - Adjust brightness, contrast, and other enhancements.

 

   ```python

   enhanced_image = ImageEnhance.Contrast(image).enhance(factor)

   ```

 

These are just a few examples of what you can do with image processing and manipulation in Python using the `Pillow` library. For more advanced tasks, you might explore computer vision libraries like OpenCV.

-SHUBHAM KUMAR LABH

Post a Comment

0 Comments