# 188. Image Processing with PIL/Pillow

**1. Opening and Displaying an Image**

```python
from PIL import Image

# Open an image
img = Image.open("example.jpg")
# Display the image
img.show()
```

***

**2. Resizing an Image**

```python
from PIL import Image

# Open an image
img = Image.open("example.jpg")
# Resize the image to 200x200 pixels
resized_img = img.resize((200, 200))
# Save the resized image
resized_img.save("resized_example.jpg")
```

***

**3. Cropping an Image**

```python
from PIL import Image

# Open an image
img = Image.open("example.jpg")
# Crop the image (left, upper, right, lower)
cropped_img = img.crop((50, 50, 200, 200))
# Save the cropped image
cropped_img.save("cropped_example.jpg")
```

***

**4. Rotating an Image**

```python
from PIL import Image

# Open an image
img = Image.open("example.jpg")
# Rotate the image by 45 degrees
rotated_img = img.rotate(45, expand=True)
# Save the rotated image
rotated_img.save("rotated_example.jpg")
```

***

**5. Applying a Filter to an Image**

```python
from PIL import Image, ImageFilter

# Open an image
img = Image.open("example.jpg")
# Apply a blur filter
blurred_img = img.filter(ImageFilter.BLUR)
# Save the blurred image
blurred_img.save("blurred_example.jpg")
```

***

**6. Converting an Image to Grayscale**

```python
from PIL import Image

# Open an image
img = Image.open("example.jpg")
# Convert the image to grayscale
grayscale_img = img.convert("L")
# Save the grayscale image
grayscale_img.save("grayscale_example.jpg")
```

***

**7. Adding Text to an Image**

```python
from PIL import Image, ImageDraw, ImageFont

# Open an image
img = Image.open("example.jpg")
# Initialize the drawing context
draw = ImageDraw.Draw(img)
# Define text and position
text = "Hello, World!"
position = (50, 50)
# Add text to the image
draw.text(position, text, fill="white")
# Save the modified image
img.save("text_example.jpg")
```

***

**8. Creating a Thumbnail**

```python
from PIL import Image

# Open an image
img = Image.open("example.jpg")
# Create a thumbnail with a maximum size of 100x100
img.thumbnail((100, 100))
# Save the thumbnail
img.save("thumbnail_example.jpg")
```

***

**9. Merging Two Images**

```python
from PIL import Image

# Open two images
img1 = Image.open("example1.jpg")
img2 = Image.open("example2.jpg")
# Ensure both images are the same size
img2 = img2.resize(img1.size)
# Blend the images (alpha controls the blend ratio)
blended_img = Image.blend(img1, img2, alpha=0.5)
# Save the blended image
blended_img.save("blended_example.jpg")
```

***

**10. Extracting Image Metadata**

```python
from PIL import Image

# Open an image
img = Image.open("example.jpg")
# Get image format, size, and mode
print("Format:", img.format)
print("Size:", img.size)
print("Mode:", img.mode)
```

***
