Comprehending List Comprehensions

Slides:



Advertisements
Similar presentations
Internet Basics & Way Beyond!
Advertisements

Image Processing … computing with and about data, … where "data" includes the values and relative locations of the colors that make up an image.
1 RTL Example: Video Compression – Sum of Absolute Differences Video is a series of frames (e.g., 30 per second) Most frames similar to previous frame.
Computer Science 111 Fundamentals of Programming I More Digital Image Processing.
HW 3: Problems 2&3. HW 3 Prob 2:Encipher encipher( S, n ) takes as input a string S and a non-negative integer n between 0 and 25. This function returns.
Lecture 6 Sept 15, 09 Goals: two-dimensional arrays matrix operations circuit analysis using Matlab image processing – simple examples.
First Bytes - LabVIEW. Today’s Session Introduction to LabVIEW Colors and computers Lab to create a color picker Lab to manipulate an image Visual ProgrammingImage.
Manipulating 2D arrays in Java
Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing.
CSc 461/561 CSc 461/561 Multimedia Systems Part A: 2. Image.
Bitmapped Images. Bitmap Images Today’s Objectives Identify characteristics of bitmap images Resolution, bit depth, color mode, pixels Determine the most.
Spring  Evolving Lists & Lights On!  Caesar Cipher  Looks Good!
Image Processing & Perception Sec 9-11 Web Design.
Objective Understand concepts used to create digital graphics. Course Weight : 15% Part Three : Concepts of Digital Graphics.
Chapter 6: Modifying Pixels by Position. Chapter Learning Goals.
EECS 110: Lec 8: Lists of Lists Aleksandar Kuzmanovic Northwestern University
Number Systems CIT Network Math
Computer Science 111 Fundamentals of Programming I Introduction to Graphics.
Computer Science 112 Fundamentals of Programming II Graphics Programming.
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
8 For-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 1.
CSC Computing with Images
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
3. Image Sampling & Quantisation 3.1 Basic Concepts To create a digital image, we need to convert continuous sensed data into digital form. This involves.
Computer Science 111 Fundamentals of Programming I Introduction to Digital Image Processing.
Picture Lab. Manipulating Pictures Discussion and activities with java.awt.Color introduce students to megapixels, pixels, the RGB color model, binary.
EECS 110: Lec 8: Lists of Lists Aleksandar Kuzmanovic Northwestern University
INTRO2CS Tirgul 6 1. What we will cover today  Algorithms and their analysis  Image  Command Line Arguments  Debugger 2.
Python Programming in Context Chapter 6. Objectives To understand pixel based image processing To use nested iteration To use and understand tuples To.
Loops ! We've seen variables change in-place before: [ x*6 for x in range(8) ] [ 0, 6, 12, 18, 24, 30, 36, 42 ] remember range ?
Compsci 101.2, Fall Plan for October l Review Catchup and Midterm and Future  Make sure everyone understand options l Review Assignment.
CS 101 – Sept. 14 Review Huffman code Image representation –B/W and color schemes –File size issues.
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 9: Review for the Midterm Exam Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
Review_5 Graphics. The visual representation of images is known as…
EECS 110: Lec 8: Lists of Lists Aleksandar Kuzmanovic Northwestern University
Representing Sound and Image. Representing images One mean of representing an image it to interpret the image as a collection of dots, each is called.
More Digital Representation Discrete information is represented in binary (PandA), and “continuous” information is made discrete.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
CompSci 101 Introduction to Computer Science March 8, 2016 Prof. Rodger.
EECS 110: Lec 8: Lists of Lists
Unit 2.6 Data Representation Lesson 3 ‒ Images
EECS 110: Lec 10: Definite Loops and User Input
Image Processing Objectives To understand pixel based image processing
Pixels, Colors and Shapes
EECS 110: Lec 12: Mutable Data
Spring 2010 EECS 110: Homework III.
EECS 110: Lec 9: Review for the Midterm Exam
Algorithmic complexity: Speed of algorithms
Multimedia Summer Camp
Fundamentals of Programming I Introduction to Digital Image Processing
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
CS320n –Visual Programming
More Loop Examples Functions and Parameters
EECS 110: Lec 10: Definite Loops and User Input
Gray Scale picture def pixBW(pixel): # given a pixel, change to BW
CS 106A, Lecture 18 Practice with 1D and 2D Arrays
Practice with loops! What is the output of each function below?
Fundamentals of Programming I Introduction to Digital Image Processing
Spring 2015.
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
Digital Image Processing
Basic Concepts of Digital Imaging
Non-numeric Data Representation
EECS 110: Lec 12: Mutable Data
CSC 221: Introduction to Programming Fall 2018
More 2D Array and Loop Examples Functions and Parameters
ECE 596 HW 2 Notes.
Presentation transcript:

Comprehending List Comprehensions def runGenerations( L ): """ runGenerations keeps running evolve... """ print( L ) # display the list, L time.sleep(0.5) # pause a bit newL = evolve( L ) # evolve L into newL runGenerations( newL ) # recurse def evolve( L ): """ evolve takes in a list of integers, L, and returns a new list of integers considered to be the "next generation" N = len(L) # N now holds the size of the list L return [ setNewElement( L, i ) for i in range(N) ] def setNewElement( L, i, x=0 ): """ setNewElement returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use return L[i] + 1

Comprehending List Comprehensions def evolve( L ): """ evolve takes in a list of integers, L, and returns a new list of integers considered to be the "next generation" """ N = len(L) # N now holds the size of the list L return [ setNewElement( L, i ) for i in range(N) ] def setNewElement( L, i, x=0 ): """ setNewElement returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use return L[i] + 1 >>> L = [42, 43, 44, 45, 46] >>> evolve(L) L 42 43 44 45 46 47 1 2 3 4 5 N 5 (i.e., len(L))

Comprehending List Comprehensions def evolve( L ): """ evolve takes in a list of integers, L, and returns a new list of integers considered to be the "next generation" """ N = len(L) # N now holds the size of the list L return [ setNewElement( L, i ) for i in range(N) ] def setNewElement( L, i, x=0 ): """ setNewElement returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use return L[i] + 1 L [42, 43, 44, 45, 46] 1 2 3 4 >>> L = [42, 43, 44, 45, 46] >>> evolve(L) N 5 (i.e., len(L)) [ setNewElement( L, i ) for i in range(5) ] [0, 1, 2, 3, 4] [ , , , , ] i 1 2 3 4

""" returns the # in L closest to 42 """ abs( x ) is built-in to Python Write this function using max or min: def bestNumber( L ): """ returns the # in L closest to 42 """

""" returns the element appearing most often in L """ Write this function however you like: Hint: Consider defining a helper function ! def mode( L ): """ returns the element appearing most often in L """

Comprehending List Comprehensions def evolve( L ): """ evolve takes in a list of integers, L, and returns a new list of integers considered to be the "next generation" """ N = len(L) # N now holds the size of the list L return [ setNewElement( L, i ) for i in range(N) ] def setNewElement( L, i, x=0 ): """ setNewElement returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use return L[i] + 1 L [[42, 43], [44, 45]] 1 >>> L = [[42, 43], [44, 45]] >>> evolve(L) [[43, 44], [45, 46]] N 2 (i.e., len(L)) [ setNewElement( L, i ) for i in range(2) ]

Comprehending List Comprehensions def evolve( L ): """ evolve takes in a list of integers, L, and returns a new list of integers considered to be the "next generation" """ N = len(L) # N now holds the size of the list L return [ setNewElement( L, i ) for i in range(N) ] def setNewElement( L, i, x=0 ): """ setNewElement returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use return L[i] + 1 Going deeper L [[42, 43], [44, 45]] 1 >>> L = [[42, 43], [44, 45]] >>> evolve(L) [[43, 44], [45, 46]] N 2 (i.e., len(L)) [ setNewElement( L, i ) for i in range(2) ] What is i? What is L[i]?

Comprehending List Comprehensions [ L[i][0] for i in range(2) ] L [[42, 43], [44, 45]] [ [L[i][0]] for i in range(2) ] [ [ L[j][i]+1 for i in range(2) ] for j in range(2) ]

Comprehending List Comprehensions [[42, 43], [44, 45]] [[ setNewElement2d( L, i, j ) for i in range(2) ] for j in range(2) ] def setNewElement2d( L, i, j, x=0, y=0 ): """ setNewElement returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use """ return L[j][i] + 1

Representing Pictures

Digital representations of pictures Grid of Pixels—each Pixel has a color But how is color represented?

What are the primary colors? When you mix all of them what color do you get?

RGB Model for Representing Color Most popular, but not only one Each pixel represented in three parts So every Pixel has some amount of Red some amount of Green and some amount of Blue HOW MUCH?

Color “levels” Each color component or “channel” is represented with a single byte 1 byte = 8 bits; which can represent numbers from 0 to 255 (2^8 – 1) Each RGB value is between 0 and 255 Examples…

Brightening a Picture def modify(pic): """ modify modifies an image to make it brighter """ pixels = getPixels(pic) if len(pixels) == 0: return newPixels = [ [setNewPixel( pixels, row, col ) for col in range(len(pixels[0]))] for row in range(len(pixels))] setPixels(pic, newPixels) def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW imanges (row, col) (r,g,b) value input pixels: a 2D list containing RGB information in the pixels in a picture input row: the row of the pixel in question input col: the column of the pixel in question """ rval= min(pixels[row][col][0]+30, 255) gval = min(pixels[row][col][1]+30, 255) bval = min(pixels[row][col][2]+30, 255) return (rval, gval, bval)

Representing the Pixels in a Picture [ [(3, 10, 100), (3, 11, 110)], [(3, 10, 200), (10, 110, 290)] ] What's that funny ( ) notation?

Tuples vs. Lists Tuples use ( ); lists use [ ] [ [(3, 10, 100), (3, 11, 110)], [(3, 10, 200), (10, 110, 290)] ] What's that funny ( ) notation? Tuples use ( ); lists use [ ] But otherwise, they are the same… (for now, almost) >>> t = (1, 2, 3) >>> t[1] 2 >>> t[1:] (2, 3) >>> (x, y, z) = t >>> x 1 >>> y

Brightening a Picture def modify(pic): """ modify modifies an image to make it brighter """ pixels = getPixels(pic) if len(pixels) == 0: return newPixels = [ [setNewPixel( pixels, row, col ) for col in range(len(pixels[0]))] for row in range(len(pixels))] setPixels(pic, newPixels) def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW imanges (row, col) (r,g,b) value input pixels: a 2D list containing RGB information in the pixels in a picture input row: the row of the pixel in question input col: the column of the pixel in question """ rval= min(pixels[row][col][0]+30, 255) gval = min(pixels[row][col][1]+30, 255) bval = min(pixels[row][col][2]+30, 255) return (rval, gval, bval) How could we brighten only the top half?

Write a function that tints the top half of the picture red: def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW image's (row, col) (r,g,b) value """ Write a function that copies the top half of an image to the bottom half. def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW image's (row, col) (r,g,b) value """ Want more? How would you turn only the sky red?