Skip to content
This repository has been archived by the owner on Jun 29, 2024. It is now read-only.

Advanced Level Completed #49

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Pandey Abhishek Nath Roy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Advance Level

### Task 1 :Image Converter:
Where the program that accepts images in multiple formats that may be in JPEG, PNG, BMP, GIF and converts them into a desired format using Python Imaging Library (PIL) that may be any of the formats mentioned.


### Task 2:Data Analysis with Pandas:
In the program on loading the "Iris" dataset from Seaborn and analyzing it using Pandas and performed exploratory data analysis, cleaning, aggregation, visualizations, and correlation calculations.
The output of the program will consits of calculations , graph which will be more understanding to users.


### Task 3:Linear Regression with Scikit-learn:
Completed a program by applying linear regression to predict house prices from the Boston housing dataset using scikit-learn and compared train and test scores and plot residuals.

### Task 4:Image Compression:
Developed a Python program for compressing images while maintaining quality by exploring compression techniques like RLE and DCT and allowing users to adjust compression quality, support various image formats, and provide output options. Optionally, include user-interface.
53 changes: 53 additions & 0 deletions Pandey Abhishek Nath Roy/task_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os
from PIL import Image

# Function to process the images
def process_images(input_folder, output_folder, files):
images_details = []
for file in files:
file_path = os.path.join(input_folder, file)
with Image.open(file_path) as img:
# Details
details = {
'filename': file,
'format': img.format,
'mode': img.mode,
'size': img.size
}

images_details.append(details)

# Convert image format to PNG
new_file_name = os.path.splitext(file)[0] + '.png'
new_file_path = os.path.join(output_folder, new_file_name)
img.save(new_file_path, 'PNG')

return images_details

def main():
# Path to the input folder
input_folder = 'input'
output_folder = 'output'

# Create the output folder if it doesn't exist
os.makedirs(output_folder, exist_ok=True)

# List all files in the input folder
files = os.listdir(input_folder)

# Process the images and get their details
try:
image_details = process_images(input_folder, output_folder, files)

# Print the file details.
for detail in image_details:
print(detail)

# Successful Conversion of Images
print("Images Converted Successfully!")

except Exception as e:
print(e)

if __name__ == '__main__':
main()
42 changes: 42 additions & 0 deletions Pandey Abhishek Nath Roy/task_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
"""task_2.ipynb

Automatically generated by Colab.

Original file is located at
https://colab.research.google.com/drive/12wTnRcqvlTEaYNcCzEkldvcC37Y5DmPk
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# Load the iris dataset
iris = sns.load_dataset("iris")
print(iris.head(5))

# Get descriptive statistics of numerical features
print(iris.describe().T)

# Check for missing values
print(iris.isnull().sum())

# Group data by species and calculate some descriptive statistics
species_groups = iris.groupby("species")
print(species_groups.describe())

# Create visualizations
sns.displot(x="sepal_length", hue="species", data=iris) # Distribution of Sepal Length by Species
sns.pairplot(iris, hue="species") # Pairwise relationships between features

# Correlation matrix
correlation_matrix = iris.corr(numeric_only=True)
print(correlation_matrix)

# Correlation heatmap
plt.figure(figsize=(8, 6))
sns.heatmap(iris.corr(numeric_only=True), annot=True, cmap="autumn_r")
plt.title("Correlation Heatmap of Iris Dataset")
plt.show()

44 changes: 44 additions & 0 deletions Pandey Abhishek Nath Roy/task_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
import pandas as pd

# Feature names (replace with actual descriptions from the dataset)
feature_names = ["CRIM", "ZN", "INDUS", ...]

data_url = "http://lib.stat.cmu.edu/datasets/boston"
raw_df = pd.read_csv(data_url, sep="\s+", skiprows=22, header=None)

# Combine features and target
data = np.c_[raw_df.values[:, :], raw_df.values[1::2, 2]] # Select all features and target values

# Assign feature names to the DataFrame columns
df = pd.DataFrame(data, columns=feature_names)

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(df.iloc[:, :-1], df.iloc[:, -1], test_size=0.2, random_state=42)

# Initialize the linear regression model
model = LinearRegression()

# Fit the model on the training data
model.fit(X_train, y_train)

# Predict on the training and testing data
y_train_pred = model.predict(X_train)
y_test_pred = model.predict(X_test)

# Calculate the scores
train_score = r2_score(y_train, y_train_pred)
test_score = r2_score(y_test, y_test_pred)

# Print additional evaluation metrics
mse_train = mean_squared_error(y_train, y_train_pred)
mse_test = mean_squared_error(y_test, y_test_pred)

print("Training R-squared:", train_score)
print("Testing R-squared:", test_score)
print("Training MSE:", mse_train)
print("Testing MSE:", mse_test)
86 changes: 86 additions & 0 deletions Pandey Abhishek Nath Roy/task_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from PIL import Image
import os

def get_size_format(bytes_, factor=1024, suffixes=["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"]):
"""
Scales bytes to its proper byte format with IEC binary prefixes.

Args:
bytes_: The size in bytes to format.
factor (int, optional): The base unit for scaling (default: 1024).
suffixes (list, optional): The list of suffixes to use (default: IEC binary prefixes).

Returns:
str: The formatted size string (e.g., 1.20 MiB).
"""

for suffix in suffixes:
if bytes_ < factor:
return f"{bytes_:.2f}{suffix}"
bytes_ /= factor
return f"{bytes_:.2f}{suffixes[-1]}" # Use the last suffix for large sizes

def compress_image(image_path, output_path=None, new_size_ratio=0.9, quality=90, max_width=None, max_height=None, to_jpeg=True):
"""
Compresses an image with resizing and quality adjustments.

Args:
image_path (str): Path to the image file to compress.
output_path (str, optional): Path to save the compressed image. If not provided, a new filename with "_compressed" is generated next to the original image.
new_size_ratio (float, optional): Ratio to reduce the image size (default: 0.9).
quality (int, optional): JPEG quality for saving (default: 90).
max_width (int, optional): Maximum width for resizing (default: None).
max_height (int, optional): Maximum height for resizing (default: None).
to_jpeg (bool, optional): Whether to convert the output to JPEG format (default: True).

Raises:
ValueError: If both output_path and to_jpeg are False.
"""

# Load the image
try:
img = Image.open(image_path)
except FileNotFoundError:
print(f"Error: Image file not found - {image_path}")
return

# Print original image details
print(f"[*] Image: {image_path}")
print(f"[*] Original size: {img.size}")

# Get original image size in bytes
image_size = os.path.getsize(image_path)
print(f"[*] Size before compression: {get_size_format(image_size)}")

# Resizing logic
if new_size_ratio < 1.0:
new_size = (int(img.size[0] * new_size_ratio), int(img.size[1] * new_size_ratio))
elif max_width and max_height:
new_size = (min(max_width, img.size[0]), min(max_height, img.size[1]))
else:
new_size = img.size # No resizing if no ratio or dimensions provided

if new_size != img.size:
img = img.resize(new_size, Image.ANTIALIAS)

# Generate output filename
if not output_path:
filename, ext = os.path.splitext(image_path)
new_filename = f"{filename}_compressed{ext}" if to_jpeg else f"{filename}_compressed{ext}"
else:
new_filename = output_path

# Validate output format
if not to_jpeg and not new_filename.lower().endswith((".png", ".bmp", ".gif")):
print("Warning: Output format not supported. Keeping the original format.")

# Save the compressed image
img.save(new_filename, optimize=True, quality=quality)

# Print results
print(f"[+] New size: {img.size}")
print(f"[+] Compressed image saved as: {new_filename}")

# Example usage
image_path = input("Enter the image path: ")
compress_image(image_path)