ASCII Images
Purpose
The purpose of this lab is to help you develop your knowledge
of the NumPy library while practicing previously learned Python skills.
Preliminaries
Create a project named ASCII Images on your lab computer.
Place the following files into this project:
Assignment
In this assignment, you will use the NumPy Python library to modify the given
array according to directions. Successful completion of the methods will write the array
to a file which will contain the Python logo in ASCII characters as shown
here.
For your convenience, the main function is provided so that the decoding functions are called in the correct order.
Remember to remove the word pass from each of the functions once you add a body.
In order to decode the file, you will write each of the following functions:
- read_file(file_name) This function takes in the name of the file and returns a 1 dimensional NumPy
array of integer values.
- write_file(file_name, array) This function takes in the name of the file to write to and the 2
dimensional array that you are writing. It writes the array to the file as a string and does not return
anything.
- int_to_ascii(array_int) This function takes in a 2 dimensional integer array and converts it into an
array where each entry is a string of characters representing one of the rows of the image.
- reshape(array, dim) This function takes in a 1 dimensional array and returns it resized into a square 2
dimensional array with the dimensions inputted.
- edit_numbers(array) This function takes in the 2 dimensional array and returns the array after editing
each of the individual numbers according to the following:
- Every entry of 16 or 48 is multiplied by 2
- Every entry of 4 is multiplied by 11
- Every entry of 40 gets 18 added to it
- Every entry of 30 gets 29 added to it
- Every entry of 3 is multiplied by 13
- Every entry of 50 gets 4 subtracted from it
- reverse_random(array) This function takes the seeminly random numbers in the inputted array and corrects them as follows:
- Every entry greater than 11100 and less than or equal to 11200 becomes 16
- Every entry greater than 11200 and less than or equal to 11300 becomes 48
- Every entry greater than 11300 and less than or equal to 11400 becomes 4
- Every entry greater than 11400 and less than or equal to 11500 becomes 40
- Every entry greater than 11500 and less than or equal to 11600 becomes 30
- Every entry greater than 11600 and less than or equal to 11700 becomes 3
- Every entry greater than 11700 becomes 50
- squareroot_odd_indices(array) This function takes the square root of all the values at odd indices and returns the new array.
- subtract_identity(array) This function takes in the 2 dimensional array and subtracts an identity matrix
of the same size from it. Then it returns the array. An identity matrix is a square 2 dimensional array of
zeros with ones across the diagonal from the upper left to the lower right.
Here is a link if you would like to learn more.
- subtract_rev_diagonal(array) This function creates an array of all zeros with numbers ranging from 0 to 66
along the diagonal from upper right to bottom left. It the subtracts it from the inputted array and returns it.
- helper_array(array) This function creates a helper array of length 67 (values range from 0 to 66)
and modifies specific rows and columns as follows:
- Add the helper array to the row of the array at index 7
- Reverse the helper array and add it to the row of the array at index 8
- Add the reversed helper array to the column of the array at index 1
- reverse_rows(array) This function takes in the 2 dimensional array and reverses each row so it flips
the entire image horizontally over the vertical axis and returns the array.
- reverse_columns(array) This function takes in the 2 dimensional array and reverses each column so it flips
the entire image vertically over the horizontal axis and returns the array.
Note that all the functions you write will return the modified NumPy array except for write_file_char() which does
not return anything.
Resources
Here is a link to the NumPy documentation
Submission
Before lab ends, e-mail a copy of to your
lab TA. The subject of the e-mail should be CSCI 111, ASCII Images,
your lab time, your name, your partner's name.
To receive credit, this e-mail must be sent before your
lab period finishes. Partial credit can be earned, but late
assignments will not be accepted.
Grading - 100 points possible
- 72 points. You will receive 6 points for each of the functions that you implement properly
and efficiently. For full credit, you should take full advantage of the NumPy library functions.
- 14 points. You will receive 10 points if your solution produces the proper Python logo image.
- 14 points. Use the appropriate formatting throughout your code. Your code should be easy to read with the appropriate
Python comments preceding each of the functions and at the beginning of the file with your name, section, date, etc.