From c08ae520de26bd6dc01dc7e2817d49d56b35bf99 Mon Sep 17 00:00:00 2001 From: vjabhi00985 Date: Mon, 3 Jun 2024 14:06:08 +0530 Subject: [PATCH 1/3] Task 1 and Task 2 Completed --- Pandey Abhishek Nath Roy/README.md | 20 +++++++++++ Pandey Abhishek Nath Roy/task_1.py | 53 ++++++++++++++++++++++++++++++ Pandey Abhishek Nath Roy/task_2.py | 42 +++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 Pandey Abhishek Nath Roy/README.md create mode 100644 Pandey Abhishek Nath Roy/task_1.py create mode 100644 Pandey Abhishek Nath Roy/task_2.py diff --git a/Pandey Abhishek Nath Roy/README.md b/Pandey Abhishek Nath Roy/README.md new file mode 100644 index 0000000..a15df09 --- /dev/null +++ b/Pandey Abhishek Nath Roy/README.md @@ -0,0 +1,20 @@ +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. \ No newline at end of file diff --git a/Pandey Abhishek Nath Roy/task_1.py b/Pandey Abhishek Nath Roy/task_1.py new file mode 100644 index 0000000..089ab57 --- /dev/null +++ b/Pandey Abhishek Nath Roy/task_1.py @@ -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() \ No newline at end of file diff --git a/Pandey Abhishek Nath Roy/task_2.py b/Pandey Abhishek Nath Roy/task_2.py new file mode 100644 index 0000000..9c718fc --- /dev/null +++ b/Pandey Abhishek Nath Roy/task_2.py @@ -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() + From cb4c24323c0388fd290e373f0e013fe5ea303ad2 Mon Sep 17 00:00:00 2001 From: vjabhi00985 Date: Mon, 3 Jun 2024 14:14:52 +0530 Subject: [PATCH 2/3] Advanced Level Completion --- Pandey Abhishek Nath Roy/task_3.py | 44 +++++++++++++++ Pandey Abhishek Nath Roy/task_4.py | 86 ++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 Pandey Abhishek Nath Roy/task_3.py create mode 100644 Pandey Abhishek Nath Roy/task_4.py diff --git a/Pandey Abhishek Nath Roy/task_3.py b/Pandey Abhishek Nath Roy/task_3.py new file mode 100644 index 0000000..a7d3f97 --- /dev/null +++ b/Pandey Abhishek Nath Roy/task_3.py @@ -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) diff --git a/Pandey Abhishek Nath Roy/task_4.py b/Pandey Abhishek Nath Roy/task_4.py new file mode 100644 index 0000000..53a364e --- /dev/null +++ b/Pandey Abhishek Nath Roy/task_4.py @@ -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) From 43877b44632e07cd2a535b977555e743015d4a8f Mon Sep 17 00:00:00 2001 From: Pandey Abhishek Nath Roy Date: Mon, 3 Jun 2024 14:16:44 +0530 Subject: [PATCH 3/3] Updated README.md --- Pandey Abhishek Nath Roy/README.md | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/Pandey Abhishek Nath Roy/README.md b/Pandey Abhishek Nath Roy/README.md index a15df09..ef49cf2 100644 --- a/Pandey Abhishek Nath Roy/README.md +++ b/Pandey Abhishek Nath Roy/README.md @@ -1,20 +1,16 @@ -Advance Level - -Task 1 :Image Converter: +# 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: - +### 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: - +### 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. \ No newline at end of file +### 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.