Bulk File Rename Python Pbl
Bulk File Rename Python Pbl
Application
Image Processing:
Image processing involves transforming or analyzing digital images to extract useful
information or enhance their quality. Common operations include:
• Resizing: Changing the dimensions of an image.
• Filtering: Removing noise or enhancing features.
• Transformations: Rotating, flipping, cropping, etc.
• Compression: Reducing file size.
🛠 Libraries Used:
• PIL (Pillow): For loading, resizing, and saving images.
• OpenCV: A powerful library for real-time image and video processing (though
not actively used in your functions).
• Matplotlib: To visualize image comparisons.
• NumPy: Backbone for numerical operations on image arrays.
UI with ipywidgets:
ipywidgets are interactive HTML widgets for Jupyter notebooks that allow users to
manipulate code and data visually.
Your app uses widgets like:
• FileUpload: To upload images.
• IntText, Text: For user input.
• Button: For triggering actions.
• Output: For dynamically displaying results.
Using os:
• Read directory contents.
• Rename files.
• Create and delete temporary folders.
Good for:
• Batch processing
• Organizing outputs
CODE:
import os
from PIL import Image
import cv2
import numpy as np
from datetime import datetime
from google.colab import files
from IPython.display import display, HTML, clear_output
import ipywidgets as widgets
from io import BytesIO
import base64
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
class ImageProcessor:
def __init__(self):
self.history = []
class ColabImageProcessor:
def __init__(self):
self.processor = ImageProcessor()
self.setup_ui()
def setup_ui(self):
# Create widgets
self.upload_button = widgets.FileUpload(
accept='image/*',
multiple=False,
description='Upload Image'
)
self.width_input = widgets.IntText(
value=800,
description='Width:',
disabled=False
)
self.height_input = widgets.IntText(
value=600,
description='Height:',
disabled=False
)
self.resize_button = widgets.Button(
description='Resize Image',
disabled=False,
button_style='',
tooltip='Click to resize image'
)
self.rename_prefix = widgets.Text(
value='',
placeholder='Enter prefix',
description='Prefix:',
disabled=False
)
self.start_number = widgets.IntText(
value=1,
description='Start Number:',
disabled=False
)
self.rename_button = widgets.Button(
description='Rename Files',
disabled=False,
button_style='',
tooltip='Click to rename files'
)
self.history_button = widgets.Button(
description='Show History',
disabled=False,
button_style='',
tooltip='Click to view operation history'
)
self.output = widgets.Output()
# Create layout
resize_box = widgets.VBox([
widgets.HTML('<h2>Image Resize</h2>'),
self.upload_button,
self.width_input,
self.height_input,
self.resize_button
])
rename_box = widgets.VBox([
widgets.HTML('<h2>Bulk Rename</h2>'),
self.rename_prefix,
self.start_number,
self.rename_button
])
history_box = widgets.VBox([
widgets.HTML('<h2>History</h2>'),
self.history_button
])
# Display the UI
display(widgets.VBox([resize_box, rename_box, history_box, self.output]))
# Clean up
os.remove(temp_input)
if success:
# Display before and after comparison
display(HTML('<h3>Before and After Comparison:</h3>'))
# Original image
original_img = Image.open(temp_input)
ax1.imshow(original_img)
ax1.set_title('Original Image')
ax1.axis('off')
# Resized image
resized_img = Image.open(temp_output)
ax2.imshow(resized_img)
ax2.set_title('Resized Image')
ax2.axis('off')
plt.tight_layout()
plt.show()
# Offer download
print("Click the link below to download the resized image:")
files.download(temp_output)
# Clean up
os.remove(temp_input)
os.remove(temp_output)
else:
print("Failed to resize image!")
# Rename files
success = self.processor.rename_files(
temp_dir,
self.rename_prefix.value,
self.start_number.value
)
if success:
# Create a visualization of the renaming process
display(HTML('<h3>Renaming Process Visualization:</h3>'))
else:
print("Failed to rename files!")
# Clean up
for file in os.listdir(temp_dir):
os.remove(os.path.join(temp_dir, file))
os.rmdir(temp_dir)
# Create visualizations
plt.figure(figsize=(12, 6))
plt.tight_layout()
plt.show()
Image Details:
• Original Size: (500, 281)
• Resized Size: (200, 500)
• Scale Factor: 0.40x
Click the link below to download the resized image:
Downloading "temp_output.jpg":