Coloring Images

Coloring images can be done in a variety of ways. This portion focuses on color to greyscale, color to black and white, and adding a color hue over the image, all with matrix-vector multiplication. Something to note during this project was that Matplotlib.pyplot reads in images on a [0,1] scale, whereas PIL.Image reads in images on a [0,255] scale. For example red would be represented as (1,0,0,1) versus (255,0,0,255). Many early functions that were created during the project were based on the [0,255] scale, thus resulting in having to multiply an Image input by 1/255 when using Matplotlib.pyplot.

Process

    1. Read in the image using PIL, convert to numpy array, and scale to speed up the process
    2. For grey-scale: find image dimensions, initialize grey scale matrix, iterate through image and compute matrix-vector multiplication between the greyscale matrix and the rgb color vector at that pixels location, compute the sum of elements in the resulting vector, create new rgb touple with each element being equal to the computed sum, replace original pixels color value with greyscale one
    3. For black and white: read in image, convert to greyscale, find dimensions, calculate the average intensity of all pixels, iterate through image while checking if each pixel is less than or greater than the average intensity, set pixel color to black if it is less than avearage intensity, and finally set pixel color to white if it is greater than the aveage intensity.
    4. For color hues: Read in and scale our image, initialize a diagonal matrix similair to the greyscale matrix where different color intensities lie on the diagonal, iterate through the image and compute matrix-vector multiplication at each location, override original image pixel values with those of the products computed
    -note: this can also be done using pointwise vector-vector multiplication by initializing a color vector ( of the form (r,g,b,1) instead of a diagonal matrix (as shown above) )
    6. For Matrix-Matrix Multiplication between Images and Matrices: Read in image, initialize Identity matrix, compute matrix-matrix multiplication, save final result
    - note: Multiplying an image by an identity matrix will leave it unchanged, however setting the diagonal entries equal to a scalar not equal to 1 will yeild interesting color effects (rendering an image much brighter/duller as the rgb values will be outside of the [0,255] range)
    7. For each section, save the final result using plt.imsave from matplotlib.pyplot

Examples

  • The rightmost picture was achieved using PIL as it seems to have more flexibility with color channels not being in the optimal range [0,255]. The result is then brighter, unorthodox colors being visible. Of course multiplying an image by an identity matrix will yeild the identical image as the product. In this example, the original picture was multiplied by an identity matrix with 3 on the diagonal rather than 1.
  • Tools Used

  • Python
  • numpy
  • PIL
  • Math Library
  • matplotlib.pyplot