Junxiang Yao Homework1
%pylab inline
from __future__ import print_function
from __future__ import division
Populating the interactive namespace from numpy and matplotlib
img = imread('M.jpg')
imshow(img)
<matplotlib.image.AxesImage at 0x108594390>
The reason why I choose works from Mondrian is that I assumed that his painting is going to show at least one particular pattern since the picture is consist of heavy black lines and rectangles painted with different colors.
img.shape
(1024, 1024, 3)
I found the width and height of this picture and used these two value as arguments to create a 2D matrix. The value of each elements in this 2D matrix is the avarage number of the rgb value of the pixel at the corresponding positioin on the picture I loaded. In a nut shell, I changed 3D matrix into 2D matrix. Next, I checked the picture using data in the 2D matrix.
height = 1024
width = 1024
newimg = zeros([width,height],uint8)
for j in range(0,width):
for i in range(0,height):
newimg [i,j] = img[i,j,0]/3 + img[i,j,1]/3 + img[i,j,2]/3
imshow(newimg,cmap=cm.gray)
colorbar()
<matplotlib.colorbar.Colorbar at 0x10e7c0c90>
I transformed the 2D array into a 1D array.
pixs = zeros([width*height],uint8)
for j in range(0,width):
for i in range(0,height):
pixs[j+i*width] = newimg [i,j]
sig = pixs[:]
plot(sig)
sig.dtype
dtype('uint8')
sigout = list((sig.astype(int16) - 125) * 200)
plot(sigout)
[<matplotlib.lines.Line2D at 0x10f10dfd0>]
plot(sigout)
xlim((0, width))
# check the chart using data from the first rows of pixels in the picture.
(0, 1024)
from IPython.display import Audio
Audio(sigout,rate = 44100)