In this problem, you will use K-means clustering for image compression. Two image files provided in the elearning. The assignment task is to display the images after data compression using K-means clustering for different values of K (2, 5, 10, 15, 20).
Is there a tradeoff between image quality and degree of compression? What would be a good value of K for each of the two images?
| from matplotlib import pyplot as io import numpy as np from PIL import Image
# Image to array img1 = io.imread(imagefilename) #image is saved as rows * columns * 3 array print (img1) #Array to image file array = np.zeros([10,20,3], dtype = np.uint8)
array[:,:10] = [255, 128, 0] # Orange left side array[:,10:] = [0,0,255] # Blue right side |
Note that your program must compile and we should be able to replicate your results.
Otherwise no credit will be given.
What to turn in:
- Your code and datasets
- A README for your compiling/using your code
- A report (pdf or doc file) containing answers to the questions posed.
The following code is for reading and writing Image Files: Image to RGB array or RGB array to image:
print(array) img2 = Image.fromarray(array) img2.save(‘testrg.png’)




