Initiate Compression
Upload Your Image
Image Preview
Original size:
System Capabilities Briefing
🖥️ Intuitive Web Interface
A responsive and dark-themed web UI for easy image selection and conceptual compression previews.
🧠 Smart Compression Logic
The core Python script intelligently adjusts JPEG quality to meet a 1MB target size or minimum quality threshold.
⚙️ Backend Integration
Designed for seamless integration with Python backends to perform actual server-side image processing.
🌐 Cross-Platform Ready
Broad compatibility across various operating systems for efficient image compression anywhere.
Operational Protocol
The core Python script utilizes file system operations and image processing libraries that run on a server. This web application provides a user-friendly interface to demonstrate the *concept* of the Shrink Ray. In a full deployment, the image would be sent to a backend server for actual compression and then returned.
To use the full power of The Shrink Ray, you'll run the Python script directly on your computer.
Prerequisites
Make sure you have Python installed (Python 3.6+ recommended).
You'll also need the Pillow
library.
Install it using pip:
pip install Pillow
Running the Script
- Save the Script: Save the provided Python
code as
shrink_ray.py
in your project directory. - Place Your Image: Put the image you want
to compress (e.g.,
my-large-photo.png
) in the same directory asshrink_ray.py
. - Modify the Script (Optional but
Recommended): Open
shrink_ray.py
and change theinput-image.png
andoutput-image.jpg
values in theif __name__ == '__main__':
block to match your desired filenames. - Run from Terminal: Navigate to your script's directory in your terminal or command prompt and run:
Click to view `shrink_ray.py` code
from PIL import Image
import io
import os
def compress_image_to_1mb(input_filename, output_filename):
print(f"Running script in directory: {os.getcwd()}")
print(f"Files available: {os.listdir()}")
input_path = os.path.join(os.getcwd(), input_filename)
output_path = os.path.join(os.getcwd(), output_filename)
try:
img = Image.open(input_path)
except FileNotFoundError:
print(f"Error: Input file '{input_filename}' not found in {os.getcwd()}")
return
quality = 95
while True:
buffer = io.BytesIO();
img.save(buffer, format='JPEG', quality=quality);
size = buffer.tell();
if size <= 1_000_000 || quality <= 10:
break;
quality -= 5;
with open(output_path, 'wb') as f:
f.write(buffer.getvalue());
print(f'Compressed image saved as {output_filename} with quality {quality} and size {size} bytes');
if __name__ == '__main__':
compress_image_to_1mb('input-image.png', 'output-image.jpg');
if __name__ == '__main__':
compress_image_to_1mb('my-large-photo.png', 'compressed-photo.jpg');
python shrink_ray.py
The compressed image will be saved in the same directory!
Project Source
Repository Access
Explore the full source code for "The Shrink Ray" script and this web application on GitHub.
View on GitHub