How to do it...

Let's see how to compress an image using vector quantization:

  1. The full code for this recipe is given in the vector_quantization.py file that has already been provided to you. Let's take a look at how it's built. We'll start by importing the required packages. Create a new Python file, and add the following lines:
import argparse 
 
import numpy as np 
from scipy import misc  
from sklearn import cluster 
import matplotlib.pyplot as plt
  1. Let's create a function to parse the input arguments. We will be able to pass the image and the number of bits per pixel as input arguments:
def build_arg_parser(): parser = argparse.ArgumentParser(description='Compress the input image \ using clustering') parser.add_argument("--input-file", dest="input_file", ...

Get Python Machine Learning Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.