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

Advanced level Task4 #9

Merged
merged 2 commits into from
May 8, 2024
Merged
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
4 changes: 4 additions & 0 deletions Konda Lakshmi Prasanna/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mage 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 a user interface.
This Python script utilizes the Boston housing dataset to perform linear regression. After splitting the data into training and testing sets, it trains a linear regression model on the training data and evaluates its performance on both sets. It calculates the model's scores for training and testing data. Additionally, it visualizes the residuals, highlighting discrepancies between predicted and actual values. This analysis aids in assessing the model's fit and identifying potential areas for improvement, crucial for understanding predictive accuracy and model effectiveness in real-world applications.
52 changes: 52 additions & 0 deletions Konda Lakshmi Prasanna/Task4
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from PIL import Image
import os

def get_size_format(b, factor=1024, suffix="B"):
"""
Scale bytes to its proper byte format.
e.g: 1253656 => '1.20MB', 1253656678 => '1.17GB'
"""
for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
if b < factor:
return f"{b:.2f}{unit}{suffix}"
b /= factor
return f"{b:.2f}Y{suffix}"

def compress_img(image_name, new_size_ratio=0.9, quality=90, width=None, height=None, to_jpg=True):
# Load the image into memory
img = Image.open(image_name)

# Print the original image shape
print("[*] Image shape:", img.size)

# Get the original image size in bytes
image_size = os.path.getsize(image_name)
print("[*] Size before compression:", get_size_format(image_size))

if new_size_ratio < 1.0:
# If resizing ratio is below 1.0, multiply width & height with this ratio to reduce image size
img = img.resize((int(img.size[0] * new_size_ratio), int(img.size[1] * new_size_ratio)), Image.ANTIALIAS)
elif width and height:
# If width and height are set, resize with them instead
img = img.resize((width, height), Image.ANTIALIAS)

# Split the filename and extension
filename, ext = os.path.splitext(image_name)

# Make a new filename appending "_compressed" to the original file name
if to_jpg:
# Change the extension to JPEG
new_filename = f"{filename}_compressed.jpg"
else:
# Retain the same extension of the original image
new_filename = f"{filename}_compressed{ext}"

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

# Print the new image shape
print("[+] New Image shape:", img.size)
print(f"[*] Compressed image saved as: {new_filename}")

# Example usage:
compress_img("wp1966881.jpg", new_size_ratio=0.8, quality=80, width=800, height=600)