14
Programming for Non-Programmers Python Chapter 2 Source: Dilbert Agenda 6:00pm Lesson Begins 6:15pm First Pillow example up and running 6:30pm First class built 6:45pm Food & Challenge Problem 7:15pm Wrap up and Class Picture

Programming for Non-Programmers - Tufts University · Writing Reusable (“Modular”) code with Python In this lesson, we are going to dive deeper into some fundamental ideas in

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Programming for Non-Programmers - Tufts University · Writing Reusable (“Modular”) code with Python In this lesson, we are going to dive deeper into some fundamental ideas in

Programming for Non-Programmers Python Chapter 2

 Source: Dilbert  

Agenda 6:00pm ­ Lesson Begins 

6:15pm ­ First Pillow example up and running 6:30pm ­ First class built 

6:45pm ­ Food & Challenge Problem 7:15pm Wrap up and Class Picture 

 

Page 2: Programming for Non-Programmers - Tufts University · Writing Reusable (“Modular”) code with Python In this lesson, we are going to dive deeper into some fundamental ideas in

Lesson Description In this lesson, we are going to take a closer look at functionality that makes our programs reusable. We are then going to create an application that can easily be extended upon.  What you will learn: 

● What is a package ● How to write a class ● Simple image processing 

Writing Reusable (“Modular”) code with Python In this lesson, we are going to dive deeper into some fundamental ideas in programming. We are going to concentrate on breaking our code into reusable chunks that make up a program. Remember in our first lesson we looked at functions as one way to do this.    The second way we will  reuse code is to build our own data structures, beyond using the primitive types we have used before (i.e., int, floats, strings). This allows not only us to reuse code, but to share these data structures with others who are trying to solve similar problems. The class structure also provides us a very nice way to organize our code.   Note: On Learning to Program: I encourage you to consult multiple sources. As I write these notes, I cannot possibly cover every way to solve a problem. I find that myself as a learner, it is useful if I can see the same problem from multiple angles, explained with different analogies, and then try to talk about to myself and solve the problem. Additionally, if I have stared at the computer monitor for too many hours, it is time to take a break, stretch, and then try to solve the problem on paper before returning back to the editor. 

Getting Setup  We are going to continue using the Idle editor provided by Python to run through some examples. We will continue to use some of the functionality given to us by the interactive Python interpreter, but will also be doing some bigger examples where we can type in code and be able to save our source file. That means we will want to be getting more comfortable with the Python Idle editor.  Go ahead and type in “idle&” in your terminal now.  

 If you are on a Windows machine, navigate to the start menu, and then Python/Idle.  

Page 3: Programming for Non-Programmers - Tufts University · Writing Reusable (“Modular”) code with Python In this lesson, we are going to dive deeper into some fundamental ideas in

Importing Packages (or “Libraries”) We are going to start this lesson by installing the Python Imaging Library (PIL) The first thing we 

are going to type into idle is “import PIL” and then hit enter   One of two things will now happen, you will either get a newline in the interpreter, or you will get 

some message along the lines of: 

 Because I already have PIL installed, I did not get this error message, and demonstrated with another package the error I received.  Note: What is a package? A package, which is also referred to as a library, is code that someone else has already written in Python or another programming language that we can import. This gives us additional functionality, and we are going to work on writing our own libraries that someone else could use in this lesson.  If you received an error message like that, then this is how we are going to install PIL.  There is a great set of instructions for installing here: http://pillow.readthedocs.org/en/latest/installation.html 

On Mac or Linux: From the commandline, you can try to use pip if you have it installed. 

sudo pip uninstall PIL

sudo pip install pillow

 From the commandline, you may also use easy_install 

easy_install Pillow

If neither of those options work, you can download the .zip (compressed archive from PyPI) and then run in the commandline 

python setup.py install

 

On Windows: Download and install the library from here: http://www.pythonware.com/products/pil/  

Page 4: Programming for Non-Programmers - Tufts University · Writing Reusable (“Modular”) code with Python In this lesson, we are going to dive deeper into some fundamental ideas in

Once PIL (Python Imaging Library) is installed, we can test again by typing ‘import PIL’, and we 

should get a newline.    

Our Project  We are going to be performing some image processing tasks with the Python Imaging Library (PIL).  Everyone’s camera these days has many cool filters. You may have even heard of Instagram. I have just downloaded it, and at this time I have zero followers or photos, so perhaps I am not using it correctly.   Lets use PIL to make a function that can perform an interesting transformation on the image. This will give us a small introduction to the Python Imaging Library (PIL), as well as a reminder of how to create functions.  

  In order to use this function, we then use the command: 

 Two test test.ppm images have been provided here for you to download: https://sites.tufts.edu/cslol/files/2015/01/images.zip  Note: There are also ‘bmp’ images, which are a more common format provided. If you are on Windows, you may like to use the bmp images.   If you’re having trouble viewing the images (i.e. imageBuffer.show() is not working), then we will just save the file and open it up manually. The amended version would look like this:  

  Now that we have created a function, lets run it on some of the sample images provided below.   

Page 5: Programming for Non-Programmers - Tufts University · Writing Reusable (“Modular”) code with Python In this lesson, we are going to dive deeper into some fundamental ideas in

Sample Input and Output 

 

Image Processing Image processing is a large field of study, and I want to motivate you with some samples of work on why it is interesting. To understand the function we wrote, we should also take a look at how color is represented on a computer. 

Image Processing examples in Science and Entertainment In order to motivate why image processing is cool, here are some examples in science and entertainment where it is used.  

● Microarrays in Biology ­  ● Paint editing tools ranging from MSPaint to Photoshop and Maya3D ● Computer Game Programming ● Instagram ● Face detection when you’re tagging photos on Facebook ● Image tracking, such as on an X­Box Kinect or on a security system ● Highlighting clusters in plots on a graph 

How does color work? Computers represent color with three primary colors that can be mixed to represent 255^3 total colors! The three components for graphical applications are usually red, green, and blue (although you may also be familiar with Cyan, Magenta, and Yellow as another color scheme).  For each individual pixel on the computer screen, we then determine on a scale of 0­255 how much we want that color channel (red, green, or blue) to contribute to the final color of that pixel.      

Page 6: Programming for Non-Programmers - Tufts University · Writing Reusable (“Modular”) code with Python In this lesson, we are going to dive deeper into some fundamental ideas in

For example: ● (255,0,0) is a tuple that makes a pixel all red, because we are filling in the red value to its 

maximum. ● (0,255,0) is a tuple that makes a pixel all green, because we are filling in the green value 

to its maximum. ● (0,0,255) is a tuple that makes a pixel all blue, because we are filling in the blue value to 

its maximum.  Now that we have some intuition about images, we can begin to build our own class. 

Building our First Class If we want to write more filters on our image, then we can write more functions. We can even group these together under what is called a class. 

Classes A class is Python’s way to let us build our own data types. Because we have defined our own data type, this is called a ‘user­defined data type’. More simply, we can think of a class as a collection of variables and functions that operate within an isolated environment. The variables and functions are stored into one spot and separated from other code (This is an example of encapsulation).  A class consists of three parts in Python: 

1. The name of the class ­ That is, the name of our new data type. 2. The member variables ­ Variables we need to store data and are local (belong and are 

accessible only to this class). 3. The member functions ­ Functions that allow us to operate on this object. 

 Important Programming Lingo: When one instantiates and makes use of the class, it is known as an ‘instance of a class’ or an object. This is an important distinguishment, because we can have multiple instances of a class.   

Motivation: A Short Note on Object-Oriented Programming Python is what is known as an Object­Oriented Programming language.  Object­Oriented programming languages evolved from procedural programming. Procedural programming is a way for writing programs such that they execute instructions in a fairly linear order from top to bottom. When we wrote our first challenge problem the guessing game, that was considered a procedural program, in that its execution was very linear, and there were no ‘objects’ or classes.  

Page 7: Programming for Non-Programmers - Tufts University · Writing Reusable (“Modular”) code with Python In this lesson, we are going to dive deeper into some fundamental ideas in

Our First Class Our first class is going to be named ‘CSLOLFilter’. That is the name of the data type we are creating. Every class needs what is called a constructor, so we are going to build that first. We are going to be working in Python’s idle editor for this example. Please open a new file up, and save it with a .py file extension (e.g. “myImageProcessing.py”).  First, we want to name our class. And we use the keyword ‘class’ and follow it with a name and a semi­colon. 

 Remember in our first lesson, we talked a lot about data representation. What this class is, is a way to represent data, with a new data type called ‘CSLOLFilter’.  Within our datatype, we can call methods. Earlier we saw in the Pillow library, we say the dot operator from the images.  We are going to make our own method, but first we must make an initial method that is called. This initial method sets up our object if there is any work that needs to be done. In this case, our initial method ‘__init__’ (with two underscores on both sides), we declare similar to how we have made our own functions.  

 Notice one thing that is different. The first argument is a keyword called ‘self’. This means that this method is acting on a specific instance of our class. It’s pythons way of saying, “okay, we made an object somewhere in our program, now I want to make sure this function specifically acts on the one object we made, and not all objects we made in the future’. Take a minute to let that sink in. The second argument will be for a file that we provide which will be an image.  The body of our __init__ function will read as below. 

 Note we once again see the word ‘self’ here. In this context this means in Python speak, “Hey Python, this object owns some variable called filepath, and we want to store it within this object.”   

Page 8: Programming for Non-Programmers - Tufts University · Writing Reusable (“Modular”) code with Python In this lesson, we are going to dive deeper into some fundamental ideas in

So anytime we want to refer to the variable filepath within our class, we once again use the word self.” The next lines open a file, store the original pixels, print out how the image is formatted, and then we display the image to the screen.  Here is the full code listing. 

  Once we have written this code, we can then test it out. Just to make the point clear that this is an instance of our class, we are going to create a variable called ‘object_instance’ and instantiate it with our CSLOLFilter which takes one argument (even though our constructor has two arguments, remember that one was ‘self’, and Python just uses this information internally). 

  We should once again get thes same input with our output. 

 Sample Input and Output 

 Okay, now we will move one step further and create a filter for this image. 

Page 9: Programming for Non-Programmers - Tufts University · Writing Reusable (“Modular”) code with Python In this lesson, we are going to dive deeper into some fundamental ideas in

 

Creating our first filter We are going to create a ‘redFilter’ that filters out all of the green and blue light from our image.  

 

 Within our CSLOLFilter class, we will define a new method just like our constructor.  We will then once again retrieve the images width and height.  

 Notice that we do this in one line. It is worth noting that the width and height are stored in imageBuffer (which is our name for the image we loaded,), and has a tuple called size that internally is (width, height). Because that itself is a tuple, or a list with two items, we can access them both at once in a single line.  

 Once we have got the dimensions to the image, we use those variables to loop through the images pixel data, and then we modify each individual pixels color. We haven’t previously seen a nested for loop, but this is a convient way to look through the images data. We are looking through one row of information (the width) at a time, and then incrementing along the y­axis of the image, and scanning that row of information.  The lines containing getpixel and putpixel are simply retrieving a tuple of color values (as we previously discussed, r, g, and b), and then modifying that tuple with a new value. Finally we show our image. 

Page 10: Programming for Non-Programmers - Tufts University · Writing Reusable (“Modular”) code with Python In this lesson, we are going to dive deeper into some fundamental ideas in

 Here is the full code listing for the method 

 

  Proceed to the next page to see how our class has formed in its entirety.  You can also download the full source from here: https://sites.tufts.edu/cslol/files/2015/01/imageProcessing.py_.zip    

Page 11: Programming for Non-Programmers - Tufts University · Writing Reusable (“Modular”) code with Python In this lesson, we are going to dive deeper into some fundamental ideas in

  Now lets call our image filter function, and you should get a new output. 

  

    

10 

Page 12: Programming for Non-Programmers - Tufts University · Writing Reusable (“Modular”) code with Python In this lesson, we are going to dive deeper into some fundamental ideas in

New Output 

 

Challenge Problem - Image Processing For our challenge problem, you will be adding new functions to our class to create your own filters. I have some suggestions for common filters and ideas provided below.  

● A blue color filter ● A green color filter ● Invert the colors ● Make the image a grayscale 

○ http://www.rapidtables.com/convert/image/rgb­to­grayscale.htm ○ Multiple r * 0.2989, g* 0.5870 and b * 0.1140 and then divide the sum by 3 

■ Will a straight multiplication work, or will you need to convert to integer? ○ Build another filter that makes the pixels black or white based on this threshold. 

● Create a border around the image ● Highlight colors in a certain color range ● Brighten the image by multiplying the r, g, and b values ● Darken the image by dividing the r, g, and b values 

  

11 

Page 13: Programming for Non-Programmers - Tufts University · Writing Reusable (“Modular”) code with Python In this lesson, we are going to dive deeper into some fundamental ideas in

Demystifying some of Python Image Library (PIL) As an exercise, it’s important to understand some of what is going on under the hood of the library. Something to note is that we can never make any assumptions about what a library is actually doing, however, as an exercise I want you to at least understand and get some new terminology under your belt that we have not previously discussed. It’s also a good exercise to do, if you think you can implement something better in this library.  Here is a look at some of the functions: http://pillow.readthedocs.org/en/latest/reference/index.html  

What about a Data Science Example? Image processing is a wide field as we have previously discussed. A library like Pillow is very low level, and can be built upon to do interesting things. One may imagine how we would build more kinds of filters on images for feature detection (e.g. face detection and object detection).  It will be worth it for us in our next sections to look at other Python libraries such as: 

● Matplotlib ­ Performs plots, useful for finding clusters in data ● Numpy ­ Perform more transformations on the data. ● Opencv ­ Performs image tracking 

 

Going further on the Image Processing Lets continue to combine the constructs we have used from previous labs.   

● Use conditionals to make thresholds on certain images. ● Try to find blocks that are the same color and fill them in with a new one (flood filter) ● (Advanced) Try to perform edge detection 

○ How would you try to tackle this problem? ○ Thinking like a computer scientist, think about how you would shrink your search 

space? ● (Super Advanced) Try to perform background elimination. 

    

12 

Page 14: Programming for Non-Programmers - Tufts University · Writing Reusable (“Modular”) code with Python In this lesson, we are going to dive deeper into some fundamental ideas in

What’s to come in the future of CSLOL? (Lessons below in Python) 

Tentative Lesson 3 ● Lists and List Comprehension fun ● Do something with loading csv file and matplotlib ● Classical searching algorithms 

Tentative Lesson 4 ● Building android apps in Python ● OpenCV image tracking, face detection 

Tentative Lesson 5 ● Do something with Pygame ● Do something with threading ● Building web apps ● Building a forms application with tk 

○ Potentially with SQL or a database   Cheat sheet for gray filter 

13