A fairly standard pattern for importing NumPy
, along with some other import
statements we will need later.
It's helpful to have all the import
s in a single cell at the top of the notebook, in order to be able to re-import everything if restarting the notebook.
Create a 1 dimensional array containing the numbers 0 through 5 and print out its its shape and its data type.
The fact that there's nothing after the comma indicates this is a 1 dimensional array. Print it out now.
Some computers have instructions specially designed to operate on multiple data items at once - these are called SIMD (Single Instruction Multiple Data) instructions.
Now create a 2D array containing the numbers 1 through 12 from list literals.
Get a subset of the rows using slicing. The first way is explicit that all columns should be included, while in the 2nd case it's implicit.
Now get a subset of both the rows and columns
Generate another array using the combination of arange
and reshape
.
And get a subset of the rows in the array. This time the row selector is a list
, so we can pull out just the first and last rows of arr
, ignoring the middle one.
Here's another way to do it, using a negative index to get the last row.
And of course we can do the same for the columns
I'm not 100% sure why the line below is doing what it's doing - maybe you can figure it out!
Part 2 - working with images
An image is just a w
x h
x 3
series of numbers, where each triplet of numbers represents an intensity of the base colors red, green, and blue. First, let's create a simple image made up completely of pixels using Hope College Orange, RGB (244,106,31)
As a quick check, look at the value of the pixel at (0, 0).
Now construct a PIL
Image
from that data, and display it.
Now let's draw a hope_blue
line through the middle of the image
And print out the first pixel in the 5 middle rows of the image
What does this image look like?
Working with the anchor logo
Let's load in the Hope anchor logo and create a tiled version of it.
First, use the requests
library to download an image from the web. We will explore the Response
object in first to explore how the requests
library works.
Now we can create an Image
object from the Response
object's content. We'll download the image again even though it's not really necessary to do so; we could use the response
object from above.
Now let's get the image as a numpy
array and look at its shape.
Counting the number of pixels that are the hope_orange
color.
We can use boolean indexing to find the pixels that are equal to our hope_orange
color.
First try it using a comparison to the whole color tuple.
This doesn't work, because NumPy will only compare two arrays that have the same size using the ==
operator.
Let's try it by doing the 3 color components separately.
Now we can combine the boolean arrays using the and
operators using the np.logical_and
function.
Now we can compute the sum of the values in all_match
.
Here's a way we could do it using ==
A brief look at using the axis
argument with np.sum
Let's revisit finding the number of pixels that match the red value of our hopeorange
color.
Now we can compute the number of elements in each row of pixels that match the red component of hopeorange
.
Tiling the image
Now let's create a version of the image that looks like this:
First, make an empty image big enough to hold some copies of the anchor
Copy the anchor data into the appropriate locations. While numpy
is optimized for avoiding loops, I'm not sure this can be done without a loop.
That's pretty big, let's make it a little smaller.
Creating a grayscale version of an image
Use the requests
library to download the file, create an Image
object from the Response
object's content, and then create a NumPy
array from the Image
.
Since these are 8 bit integers, and we will be multiplying them together to calculate the distance, we will need to convert the arrays into a type large enough to hold the result, which will be a uint16
Now it will be OK for us to multiply mcfall_16
by itself, sum up the values, and then take the square root of them
And a quick check manually
Get the square root of sum_squared
, and do a quick check on that as well.
Now we can:
create an empty array that's the same size as
mcfall
compute a value between 0 and 1 for the distance from 0
set the (R, G, B) values to be that normalized value multiplied by the maximum possible pixel brightness of 255
Some sanity checks: