Programming for Non-Programmers

Similar documents
In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

Dr. Scheme evaluates expressions so we will start by using the interactions window and asking Dr. Scheme to evaluate some expressions.

CMSC/BIOL 361: Emergence Cellular Automata: Introduction to NetLogo

CMSC 150 Lab 8, Part II: Little PhotoShop of Horrors, Part Deux 10 Nov 2015

6.001 Notes: Section 15.1

Week - 01 Lecture - 04 Downloading and installing Python

6.001 Notes: Section 17.5

(Refer Slide Time: 00:26)

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

L E S S O N 2 Background

(Ca...

: Intro Programming for Scientists and Engineers Final Exam

Decisions, Decisions. Testing, testing C H A P T E R 7

(Refer Slide Time: 1:27)

Art, Nature, and Patterns Introduction

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

CS1114: Matlab Introduction

1 Check it out! : Fundamentals of Programming and Computer Science, Fall Homework 3 Programming: Image Processing

6.001 Notes: Section 6.1

Introduction to Programming with JES

Would you like to put an image on your index page? There are several ways to do this and I will start with the easy way.

AGENT123. Full Q&A and Tutorials Table of Contents. Website IDX Agent Gallery Step-by-Step Tutorials

6.001 Notes: Section 1.1

DOING MORE WITH EXCEL: MICROSOFT OFFICE 2013

Select the ONE best answer to the question from the choices provided.

ARTIFICIAL INTELLIGENCE AND PYTHON

MCS 2514 Fall 2012 Programming Assignment 3 Image Processing Pointers, Class & Dynamic Data Due: Nov 25, 11:59 pm.

CISC 1600 Lecture 3.1 Introduction to Processing

CSC 101: Lab Manual#11 Programming Turtle Graphics in Python Lab due date: 5:00pm, day after lab session

CS Problem Solving and Object-Oriented Programming

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Introduction to Python Part 2

CME 193: Introduction to Scientific Python Lecture 1: Introduction

Once you define a new command, you can try it out by entering the command in IDLE:

CSS worksheet. JMC 105 Drake University

CS1114: Matlab Introduction

[ the academy_of_code] Senior Beginners

CS 465 Program 5: Ray II

c122sep2214.notebook September 22, 2014

DOING MORE WITH EXCEL: MICROSOFT OFFICE 2010

Making use of other Applications

Image Processing (1) Basic Concepts and Introduction of OpenCV

CMPSCI 119 LAB #2 Greebles / Anime Eyes Professor William T. Verts


CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

SPRITES Moving Two At the Same Using Game State

Using the API: Introductory Graphics Java Programming 1 Lesson 8

IMAGE PROCESSING OVERVIEW

Creating Your Web Site

There are four (4) skills every Drupal editor needs to master:

The first thing we ll need is some numbers. I m going to use the set of times and drug concentration levels in a patient s bloodstream given below.

Python for Earth Scientists

BASICS OF PIXLR EDITOR

Making Tables and Graphs with Excel. The Basics

CSC 101: PreLab Reading for Lab #4 More HTML (some of this reading on Tables and Images are based on previous writings of Prof William Turkett)

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Introduction to: Computers & Programming: Review prior to 1 st Midterm

O Hailey: Chapter 3 Bonus Materials

Introduction to Game Programming Lesson 4 Lecture Notes

CSE2003: System Programming Final Exam. (Spring 2009)

Day 1: Introduction to MATLAB and Colorizing Images CURIE Academy 2015: Computational Photography Sign-Off Sheet

nostarch.com/pfk For bulk orders, please contact us at

Introduction to Machine Learning Prof. Anirban Santara Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

(Refer Slide Time: 00:03:51)

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

CS61A Notes Week 1A: Basics, order of evaluation, special forms, recursion

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

Part 6b: The effect of scale on raster calculations mean local relief and slope

5 R1 The one green in the same place so either of these could be green.

CMPSCI 119 LAB #2 Anime Eyes Professor William T. Verts

E-Business Systems 1 INTE2047 Lab Exercises. Lab 5 Valid HTML, Home Page & Editor Tables

Languages. Solve problems using a computer, give the computer instructions. Remember our diaper-changing exercise?

Lab 1: Silver Dollar Game 1 CSCI 2101B Fall 2018

GETTING STARTED GUIDE

SETTING UP A. chapter

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

GLY Geostatistics Fall Lecture 2 Introduction to the Basics of MATLAB. Command Window & Environment

Computer and Programming: Lab 1

CS 100 Python commands, computing concepts, and algorithmic approaches for final Fall 2015

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Visual C# Program: Simple Game 3

If Statements, For Loops, Functions

Workshop with ROCKWOOL editors. Helle Jensen, Senior ux consultant

1 Getting started with Processing

EPISODE 23: HOW TO GET STARTED WITH MAILCHIMP

Solve a Maze via Search

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Chapter 2: Objects, classes and factories

Lab 1 Introduction to R

5.1. Examples: Going beyond Sequence

Module 10: Imperative Programming, Modularization, and The Future

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Simple Java Programming Constructs 4

CSE : Python Programming

HTML/CSS Lesson Plans

Term Definition Introduced in: This option, located within the View tab, provides a variety of options to choose when sorting and grouping Arrangement

InDesign Part II. Create a Library by selecting File, New, Library. Save the library with a unique file name.

Transcription:

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

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. 1

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. sudopipuninstallpil sudopipinstallpillow From the commandline, you may also use easy_install easy_installpillow If neither of those options work, you can download the.zip ( compressed archive from PyPI ) and then run in the commandline pythonsetup.pyinstall On Windows: Download and install the library from here: http://www.pythonware.com/products/pil/ 2

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. 3

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. 4

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. 5

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. 6

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. 7

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. 8

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 9

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

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

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

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