To get credit for the lab, please show the completed work to a lab TA. They will check you off.
Goals:
- Learn how to apply KMeans clustering for image compression.
- Implement basic image processing tasks.
- Practice working with external libraries such as
scikit-learn,numpy,Pillow, andFlask. - Understand how to manipulate images as NumPy arrays and perform color reduction.
- Build an interactive module using Flask to handle image processing through a web interface.
You can use the Makefile to install all dependencies. In your terminal, simply run:
make installThis will automatically install the necessary packages listed in requirements.txt, including:
- Flask
- Numpy
- Scikit-learn
- Pillow
- Complete the following functions in the provided script:
load_image(image_path): This function should load the image from a file and return it as a NumPy array. You will need to use thePillowlibrary for this task.image_compression(image_np, n_colors): Implement KMeans clustering usingscikit-learnto reduce the number of colors in the image ton_colors. The function should return a compressed version of the image.
- Save the Compressed Image: The function
save_resulthas been provided for you. Once the original and compressed images are prepared, the function will save them side by side in a single image file.
- If you prefer, you can also test the code locally by running the script directly and specifying an image path. Modify the
__main__()function to load the image, apply compression, and save the results. Make sure you provide paths for both input and output images. - Choose your favorite image as input and experiment with different values of
n_colors(e.g., 4, 8, 16) and observe the effect on image quality.
-
Select your favorite image file (e.g.,
'favorite_image.png') and place it in your working directory. -
In the
__main__()function:- Set
image_pathto your selected image file. - Set
output_pathto where you want to save the side-by-side result (e.g.,'compressed_image.png'). - Choose an appropriate number of colors (
n_colors), e.g., 8.
- Set
-
Run the script in your terminal:
python image_compression.py
-
Check the output image to see the original and compressed versions side by side.
Once the environment is set up, you can start the Flask application by running:
make runThis will start the Flask server and make the interactive application available locally at http://127.0.0.1:3000.
The interactive module allows you to upload an image and select the number of colors for compression through a web interface.
- Open your browser and go to
http://127.0.0.1:3000. - Upload your favorite image using the provided interface.
- Enter the number of colors for compression.
- Click the "Submit" button to perform the image compression.
- The original and compressed images will be displayed side by side on the page.
- Ensure that the image loaded is in RGB format.
- Test with your favorite image and experiment with different values for
n_colorsto observe the impact of compression. - If you encounter issues loading or saving images, check the installation of
Pillowand the file format compatibility. - If you encounter issues running the Flask server, ensure that
flaskis correctly installed and check the command line output for error messages.