1 / 33

CS1315: Introduction to Media Computation

CS1315: Introduction to Media Computation. Drawing directly on images, Strings, Modules, And the kitchen sink…. Drawing lines on Santa. def lineExample(): img = makePicture(pickAFile()) new = verticalLines(img) new2 = horizontalLines(new) show(new2) return new2

aleron
Download Presentation

CS1315: Introduction to Media Computation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1315: Introduction to Media Computation Drawing directly on images,Strings, Modules, And the kitchen sink…

  2. Drawing lines on Santa def lineExample(): img = makePicture(pickAFile()) new = verticalLines(img) new2 = horizontalLines(new) show(new2) return new2 def horizontalLines(src): for y in range(1,getHeight(src),5): for x in range(1,getWidth(src)): setColor(getPixel(src,x,y),black) return src def verticalLines(src): for x in range(1,getWidth(src),5): for y in range(1,getHeight(src)): setColor(getPixel(src,x,y),black) return src Remember, some colors arealready defined, like “black”, “red”…

  3. Functions to avoid the tedium of individual pixels • addText(pict,x,y,string) puts the string starting at position (x,y) in the picture • addLine(picture,x1,y1,x2,y2) draws a line from position (x1,y1) to (x2,y2) • addRect(pict,x1,y1,w,h) draws a black rectangle (unfilled) with the upper left hand corner of (x1,y1) and a width of w and height of h • addRectFilled(pict,x1,y1,w,h,color) draws a rectangle filled with the color of your choice with the upper left hand corner of (x1,y1) and a width of w and height of h

  4. Example picture def littlepicture(): canvas=makePicture(getMediaPath("640x480.jpg")) addText(canvas,10,50,"This is not a picture") addLine(canvas,10,20,300,50) addRectFilled(canvas,0,200,300,500,yellow) addRect(canvas,10,210,290,490) writePictureTo(canvas, "awesomePicture.jpg") return canvas

  5. A thought experiment • Consider the previous bit of code… • How big is the Python file? • How big is the image?

  6. Program and Picture Sizes • The Python file is less than 100 characters (100 bytes) • The picture is stored on disk at about 15,000 bytes • What does this imply?

  7. Instructions for Drawing Images • If we could tell JES to draw the picture, the files take less memory than if we drew the picture, then saved it as a JPEG file • Put another way, we can program pictures with JES • A programmed graphic is called a vector-based graphic • A graphic with every pixel saved is a Bitmap graphic

  8. Vector-based vs. Bitmap Graphical representations • Vector-based graphical representations are basically executable programs that generate the picture on demand. • Flash, Postscript, and AutoCAD use vector-based representations • Bitmap graphical representations (like JPEG, BMP, GIF) store individual pixels or representations of those pixels. • JPEG and GIF are actually compressed pictures that throw away some details to make the files smaller

  9. Vector-based representations can be smaller • Vector-based representations can be much smaller than bit-mapped representations • Smaller means faster transmission (Flash and Postscript) • But if you want all the detail of a complex picture… then it won’t be smaller

  10. But vector-based has more value than that • Imagine that you’re editing a picture with lines on it and you want to make a line longer • If you change the bits in a bitmap image, then you overwrite the pixels that were there before • There’s no way to really realize that you’ve extended or shrunk a line • If you edit a vector-based image, it’s possible to just change the specification • Change the numbers saying where the line is • Then it really is the same line • That’s important when the picture drives the creation of the product, like in automatic cutting machines

  11. Example: Building a programmed graphic • Left side seems lighter than the right side • In reality, the end quarters are actually the same colors.

  12. Danger! “Magic numbers” Why “100”? Building a programmed graphic def greyEffect(): file = getMediaPath("640x480.jpg") pic = makePicture(file) # First, 100 columns of 100-grey thisGrey = makeColor(100,100,100) for x in range(1,100): for y in range(1,100): setColor(getPixel(pic,x,y), thisGrey) # Second, 100 columns of increasing greyness greyLevel = 100 for x in range(100,200): thisGrey = makeColor(greyLevel, greyLevel, greyLevel) for y in range(1,100): setColor(getPixel(pic,x,y), thisGrey) greyLevel = greyLevel + 1 # Third, 100 columns of increasing greyness, from 0 greyLevel = 0 for x in range(200,300): thisGrey = makeColor(greyLevel, greyLevel, greyLevel) for y in range(1,100): setColor(getPixel(pic,x,y), thisGrey) greyLevel = greyLevel + 1 # Finally, 100 columns of 100-grey thisGrey = makeColor(100,100,100) for x in range(300,400): for y in range(1,100): setColor(getPixel(pic,x,y), thisGrey) return pic

  13. Removing the magic numbers def greyEffect(): … # First, width columns of grey greyLevel = 100 thisGrey = makeColor(greyLevel, greyLevel, greyLevel) width = 100 height = 100 for x in range(1, width): for y in range(1, height): setColor(getPixel(pic,x,y),thisGrey) # Second, width columns of increasing greyness for x in range(width, width+width): for y in range(1,height): setColor(getPixel(pic,x,y),thisGrey) greyLevel = greyLevel + 1

  14. Why do we write programs? • Could we make this picture in Photoshop or Flash? Sure, but… • It’s useful to know how to program this picture • The program provides an exact definition of the process of generating this picture • It works for anyone who can run the program, without needing to know Photoshop or Flash • The lines can be exact, without “jaggies” • Can make precise changes to the image by changing the code and regenerating the image

  15. Why write sound programs? • “Aren’t there audio tools that can do many of the things we’re doing with JES?” • Sure, and that’s good enough…if that’s good enough • If you just want to use a sound, then simply using tools to generate the noise/instrument/sound you want is fine • But what if you want to build on or change a sound? • Writing a program gives you exact control over the process

  16. Communicating process • What if you want to tell someone else how you got that sound, so that they can replicate the process, or even modify the sound in some way? • You could write down all the steps you used in a sound application tool, but… • Tedious, error prone • Or you could provide a program • A succinct, executable definition of a process

  17. We write programs to encapsulate and communicate process def howtodoit: if you can do something by hand: do it by hand if you need to teach someone else to do it: consider a program if you need to explain to lots of people how to do it: definitely use a program if you want lots of people to do it without having to teach them something first: definitely use a program else: ask an expert….

  18. Shifting gears:Useful string methods • startswith(prefix) returns true if the string starts with the given suffix • endswith(suffix) returns true if the string ends with the given suffix • find(findstring) and find(findstring,start) and find(findstring,start,end) finds the findstring in the object string and returns the index number where the string starts. You can tell it what index number to start from, and even where to stop looking. It returns -1 if it fails • There is also rfind(findstring) (and variations) that searches from the end of the string toward the front

  19. Demonstrating startswith >>> letter = “The 1315 Chef requests the pleasure of your company..." >>> print letter.startswith(“The") 1 >>> print letter.startswith(“Chef") 0 Remember that Python sees “0” as false and anything else (including “1”) as true

  20. Whoa… What was all that? >>> letter = “The 1315 Chef requests the pleasure of your company...” >>> print letter.startswith(“Chef”) 0 • Why did we type letter.startswith(“Chef”) instead of startswith(letter, “Chef”), or something else? • We are calling a method on the string • A method is a function tied to the string • Don’t worry about the details too much, just imitate

  21. Demonstrating endswith >>> filename="barbara.jpg" >>> if filename.endswith(".jpg"): ... print "It's a picture" ... It's a picture

  22. Demonstrating find >>> print letter The 1315 Chef requests the pleasure of your company... >>> print letter.find(“1315") 4 >>> print letter.find(“Chef") 9 >>> print len(“Chef") 4 >>> print letter[4:9+4] 1315 Chef >>> print letter.find("fred") -1

  23. Replace method >>> print letter 'The 1315 Chef requests the pleasure of your company...' >>> letter.replace(“e",“3") 'Th3 1315 Ch3f r3qu3sts th3 pl3asur3 of your company...' >>> print letter 'The 1315 Chef requests the pleasure of your company...'

  24. Useful methods to use with lists:But these don’t work with strings • append(something) puts something in the list at the end • remove(something) removes something from the list, if it’s there • sort() puts the list in alphabetical order • reverse() reverses the list

  25. More useful list methods • count(something) tells you the number of times that something is in the list • max() and min() are functions (we’ve seen them before) that take a list as input and give you the maximum and minimum value in the list

  26. We didn’t go beyond this point in breakout…

  27. Converting from strings to lists >>> print letter.split(" ") ['The', '1315', 'Chef', 'requests', 'the', 'pleasure', 'of', 'your', 'company...']

  28. Adding new capabilities: Modules • What we need to do is to add capabilities to Python that we haven’t seen so far • We do this by importing external modules • A module is a file with a bunch of additional functions and objects defined within it • Some kind of module capability exists in virtually every programming language. • By importing the module, we make the module’s capabilities available to our program • We access them with dot notation, like the methods used with strings and lists

  29. An interesting module: Random >>> import random >>> for i in range(1,10): ... print random.random() ... 0.8211369314193928 0.6354266779703246 0.9460060163520159 0.904615696559684 0.33500464463254187 0.08124982126940594 0.0711481376807015 0.7255217307346048 0.2920541211845866

  30. Randomly choosing words from a list >>> for i in range(1,5): ... print random.choice(["Here", "is", "a", "list", "of", "words", "in","random","order"]) ... list a Here list

  31. Randomly generating language • Given a list of nouns, verbs that agree in tense and number, and object phrases that all match the verb… • We can randomly take one from each to make sentences

  32. Random sentence generator import random def sentence(): nouns = ["Mark", "Adam", "Angela", "Larry", "Jose", "Matt", "Jim"] verbs = ["runs", "skips", "sings", "leaps", "jumps", "climbs", "argues", "giggles"] phrases = ["in a tree", "over a log", "very loudly", "around the bush", "while reading the Technique"] phrases = phrases + ["very badly", "while skipping","instead of grading", "while typing on the CoWeb."] print random.choice(nouns), random.choice(verbs), random.choice(phrases)

  33. Running the sentence generator >>> sentence() Jose leaps while reading the Technique >>> sentence() Jim skips while typing on the CoWeb. >>> sentence() Matt sings very loudly >>> sentence() Adam sings in a tree >>> sentence() Adam sings around the bush >>> sentence() Angela runs while typing on the CoWeb. >>> sentence() Angela sings around the bush >>> sentence() Jose runs very badly

More Related