CS 101: Introduction to Computing

Slides:



Advertisements
Similar presentations
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.
Advertisements

CS2984: Introduction to Media Computation Drawing directly on images.
Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function.
Python: Making colors and Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function.
CSC 160 Computer Programming for Non-Majors Section 1.2: Drawing a UFO Prof. Adam M. Wittenstein
CS 1 with Robots Image Manipulation Institute for Personal Robots in Education (IPRE)‏
TOPIC 9 MODIFYING PIXELS IN A MATRIX: COPYING, CROPPING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
Computer Science 101 Introduction to Programming with Pictures.
+ Introduction to Programming My first red-eye removal.
CS 102 Computers In Context (Multimedia)‏ 01 / 28 / 2009 Instructor: Michael Eckmann.
CS 101: Introduction to Computing Programming picture manipulations Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by.
TOPIC 7 MODIFYING PIXELS IN A MATRIX NESTED FOR LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by.
CS1315: Introduction to Media Computation Picture encoding and manipulation.
CS1315: Introduction to Media Computation Picture encoding and manipulation.
Chapter 6: Modifying Pixels by Position. Chapter Learning Goals.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
CSC1401 Viewing a picture as a 2D image - 1. Review from the last week We used a getPixels() to get all of the pixels of a picture But this has been somewhat.
02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures.
CS1315: Introduction to Media Computation Referencing pixels directly by index number.
Manipulating Pixels by Range and More on Functions.
CS2984: Introduction to Media Computation Using Loops for Pictures Conditionals Copying images.
CS1315: Introduction to Media Computation Picture encoding and manipulation.
1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.
CS 101: Introduction to Computing Rotating and Blurring Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan,
Chapter 4: Modifying Pixels in a Range (partial slide deck)
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 4: Modifying Pixels in a Range.
CS 101: Introduction to Computing Color replacements and targeted color replacement (if statement) Developed by Mark Guzdial, Georgia Institute of Technology,
03-ConditionalsInPictures Barb Ericson Georgia Institute of Technology Feb 2010 Working with ranges in pictures.
Chapter 5: Advanced Picture Techniques (partial deck)
CS1315: Introduction to Media Computation Color replacements and targeted color replacement (IF)
Integer Operations Integers are the set of whole numbers and their opposites. The set of whole numbers starts with zero! 0,1,2,3,4,5,… Integers …-1,-2,0,1,2…
A Media Computation Cookbook Manipulating Images and Sounds for Use in Alice Part 1: Image Manipulations Part 2: Advanced Image Manipulations, e.g., changing.
CSC 112Introduction to Media Computation 1 Rotating Images.
CS 1114: Finding things Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
CS1315: Introduction to Media Computation Transforming pictures by index number.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 4: Modifying Pixels in a Range.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 2 Barb Ericson Georgia Institute of Technology August 2005.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
CompSci 101 Introduction to Computer Science March 8, 2016 Prof. Rodger.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
CS 101: Introduction to Computing Referencing pixels directly by index number Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified.
Chapter 5: Picture Techniques with Selection
Introduction to Computing Science and Programming I
Whatcha doin'? Aims: To start using Python. To understand loops.
Topic 9 Modifying Pixels in a Matrix: Copying, Cropping
Image Manipulation Institute for Personal Robots in Education (IPRE)‏
Week 2.
CS1315: Introduction to Media Computation
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
Georgia Institute of Technology
Working with ranges in pictures
Barb Ericson Georgia Institute of Technology August 2005
File Handling Programming Guides.
Test 2 on Wed, 11/9 On image processing
Workshop for Programming And Systems Management Teachers
CSE 8A Lecture 6 Reading for next class:
Gray Scale picture def pixBW(pixel): # given a pixel, change to BW
Chapter 4: Modifying Pixels in a Range
Image Manipulation Institute for Personal Robots in Education (IPRE)‏
Coding Concepts (Data- Types)
Two-Dimensional Arrays and Nested Loops – part 2
Image Manipulation Institute for Personal Robots in Education (IPRE)‏
CS 177 Week 3 Recitation Slides
CS1315: Introduction to Media Computation
Barb Ericson Georgia Institute of Technology May 2006
CS1315: Introduction to Media Computation
Chapter 4: Modifying Pixels in a Range
CS1315: Introduction to Media Computation
Chapter 9 Using Decisions to
Presentation transcript:

CS 101: Introduction to Computing More referencing pixels directly by index number: Red eye and mirroring Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan, University of Illinois at Chicago, 2005, for educational use.

Removing “Red Eye” When the flash of the camera catches the eye just right (especially with light colored eyes), we get bounce back from the back of the retina. This results in “red eye” We can replace the “red” with a color of our choosing. First, we figure out where the eyes are (x,y) using MediaTools

Removing Red Eye def removeRedEye(pic,startX,startY,endX,endY,replacementcolor): red = makeColor(255,0,0) for x in range(startX,endX): for y in range(startY,endY): currentPixel = getPixel(pic,x,y) if (distance(red,getColor(currentPixel)) < 165): setColor(currentPixel,replacementcolor) Why use a range? Because we don’t want to replace her red dress! What we’re doing here: Within the rectangle of pixels (startX,startY) to (endX, endY) Find pixels close to red, then replace them with a new color

“Fixing” it: Changing red to black removeRedEye(jenny, 109, 91, 202, 107, makeColor(0,0,0)) Jenny’s eyes are actually not black—could fix that Eye are also not mono-color A better function would handle gradations of red and replace with gradations of the right eye color Now ask for a student who knows their picture number and that they’ve got red eye. Pict 19 Spring 2005 will do.

If you know where the pixels are: Mirroring Imagine a mirror horizontally across the picture, or vertically What would we see? How do generate that digitally? We simply copy the colors of pixels from one place to another

Mirroring a picture Slicing a picture down the middle and sticking a mirror on the slice Do it by using a loop to measure a difference The index variable is actually measuring distance from the mirrorpoint Then reference to either side of the mirror point using the difference

Program for mirroring converts a real number to an integer. e.g. if width=5, then 5/2 = 2.5 and int(5/2) = 2 def mirrorVertical(source): mirrorpoint = int(getWidth(source)/2) for y in range(1,getHeight(source)): for xOffset in range(1,mirrorpoint): pright = getPixel(source, xOffset+mirrorpoint,y) pleft = getPixel(source, mirrorpoint-xOffset,y) c = getColor(pleft) setColor(pright,c)

How does that work? Compute the half-way horizontal index The y value travels the height of the picture The xOffset value is an offset It’s not actually an index It’s the amount to add or subtract We copy the color at mirrorpoint-offset to mirrorpoint+offset def mirrorVertical(source): mirrorpoint = int(getWidth(source)/2) for y in range(1,getHeight(source)): for xOffset in range(1,mirrorpoint): pright = getPixel(source, xOffset+mirrorpoint,y) pleft = getPixel(source, mirrorpoint-xOffset,y) c = getColor(pleft) setColor(pright,c)

Can we do it with a horizontal mirror? def mirrorHorizontal(source): mirrorpoint = int(getHeight(source)/2) for yOffset in range(1,mirrorpoint): for x in range(1,getWidth(source)): pbottom = getPixel(source,x,yOffset+mirrorpoint) ptop = getPixel(source,x,mirrorpoint-yOffset) setColor(pbottom,getColor(ptop))

Of course!

What if we wanted to copy bottom to top? Very simple: Swap the order of pixels in the bottom line def mirrorHorizontal(source): mirrorpoint = int(getHeight(source)/2) for yOffset in range(1,mirrorpoint): for x in range(1,getWidth(source)): pbottom = getPixel(source,x,yOffset+mirrorpoint) ptop = getPixel(source,x,mirrorpoint-yOffset) setColor(ptop,getColor(pbottom))

Messing with Santa some more

Doing something useful with mirroring Mirroring can be used to create interesting effects, but it can also be used to create realistic effects. Consider this image that from a trip to Athens, Greece. Can we “repair” the temple by mirroring the complete part onto the broken part?

Figuring out where to mirror Use MediaTools to find the mirror point and the range that we want to copy

Writing functions for specific files…generally The function to mirror the temple needs to work for one and only one file. But we still don’t want to write out the whole path. setMediaPath() allows us to pick a directory where our media will be stored. getMediaPath(filename) will generate the entire path for us to the filename in the media directory THIS ONLY WORKS WHEN WE’RE ACCESSING FILES IN THE MEDIA DIRECTORY AND WHERE WE HAVE SET THE PATH FIRST!

Program to mirror the temple def mirrorTemple(): source = makePicture(getMediaPath("temple.jpg")) mirrorpoint = 277 lengthToCopy = mirrorpoint - 14 for x in range(1,lengthToCopy): for y in range(28,98): p = getPixel(source,mirrorpoint-x,y) p2 = getPixel(source,mirrorpoint+x,y) setColor(p2,getColor(p)) show(source) return source

Did it really work? It clearly did the mirroring, but that doesn’t create a 100% realistic image. Check out the shadows: Which direction is the sun coming from? Consider other reasons why this worked as well as it did. Clouds in the sky?

Understanding the Temple Fix What is the very first transfer of pixels from and to? Which (x,y) pixel from? Which (x,y) pixel to? What is the second set of pixels? How many pixels get copied? Try to get people to make guesses here, first, before going on.

Adding print statements to see what’s happening def mirrorTemple(): source = makePicture(getMediaPath("temple.jpg")) mirrorpoint = 277 lengthToCopy = mirrorpoint - 14 for x in range(1,lengthToCopy): for y in range(28,98): print "Copying color from",mirrorpoint-x,y print "to",mirrorpoint+x,y p = getPixel(source,mirrorpoint-x,y) p2 = getPixel(source,mirrorpoint+x,y) setColor(p2,getColor(p)) show(source) return source

First pixels are either side of the mirrorpoint, then moving down >>> p2=mirrorTemple() Copying color from 276 28 to 278 28 Copying color from 276 29 to 278 29 Copying color from 276 30 to 278 30

Counting pixels def mirrorTemple(): source = makePicture(getMediaPath("temple.jpg")) mirrorpoint = 277 lengthToCopy = mirrorpoint - 14 count = 0 for x in range(1,lengthToCopy): for y in range(28,98): p = getPixel(source,mirrorpoint-x,y) p2 = getPixel(source,mirrorpoint+x,y) setColor(p2,getColor(p)) count = count + 1 show(source) print "We copied",count,"pixels" return source

Counting pixels >>> p2=mirrorTemple() We copied 18340 pixels Where did that come from? How many rows? Y goes from 28 to 98 = 70 rows of pixels How many columns? X goes from 1 to 277-14=263 = 262 columns of pixels 70 * 262 = 18340

Some Utility Functions and other random stuff about JES If you know the name of the file, searching for it with pickAFile() feels tedious You can set and get a media folder (path) for remembering a place where your media will be coming from (or going to) setMediaPath() lets you pick a file in your media folder getMediaPath(basefilename) lets you generate a complete filename out of only the last part This is useful if all your images are in a single directory

Example >>> setMediaPath() #And then I get a file picker to make the choice with New media folder: /Users/sloan/Mediasources/ >>> getMediaPath("tonks.jpg") '/Users/sloan/Mediasources/tonks.jpg' >>> tonks = makePicture("tonks.jpg") >>> print tonks Picture, filename /Users/sloan/Mediasources/tonks.jpg height 296 width 224

“Driver” utility functions def hw(picture): for eggs in spam spam spam spam: def driver(): file = pickAFile() show(makePicture(file)) # Show the “before” picture picture = makePicture(file) hw(picture) show(picture) # Show the “after” picture writePictureTo(picture, r“myFunkyPicture.jpg”) return(picture) Sometimes you want to break up your code into multiple functions like this to make it easier to read and understand.

Comments – You should know what these are by now, but if not... Python ignores from “#” through the rest of the line If you start a line with “#”, the whole line is ignored Why do we want lines to be ignored? To be able to leave notes to ourselves or someone else about how the program works