Media Computation in JES (Chapter 2) DM Rasanjalee Himali.

Slides:



Advertisements
Similar presentations
COMPUTER PROGRAMMING Task 1 LEVEL 6 PROGRAMMING: Be able to use a text based language like Python and JavaScript & correctly use procedures and functions.
Advertisements

Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function.
The Web Warrior Guide to Web Design Technologies
CS1315: Introduction to Media Computation Introduction to Programming.
Created by Mark Guzdial, Georgia Institute of Technology; modified by Robert H. Sloan, University of Illinois at Chicago, For Educational Use. CS.
IT151: Introduction to Programming
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Python: Making colors and Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function.
Chapter 2: Introduction to Programming. Chapter Learning Objectives.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Intro to Robots Robots are Everywhere. Intro to Robots Various robots in use today lawnmower pet baby seal for convalescents gutter cleaner home security.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
Python.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
A First Program Using C#
An Introduction to Textual Programming
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Introduction to Python
CS 101: Introduction to Computing Programming picture manipulations Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Lecture # 27 Python I. Python “Interpretive” language (vs. compiled) So is HTML, JavaScript. Python supports file I/O. JavaScript doesn’t Python is a.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
Python Basic Syntax. Basic Syntax - First Program 1 All python files will have extension.py put the following source code in a test.py file. print "Hello,
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Python programming Introduction to the JES environment and basics of Python Reading: Chapters 1, 2 from “Introduction to Computing and Programming in Python”
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
CS1315: Introduction to Media Computation Introduction to Programming.
CS 177 Week 4 Recitation Slides Variables, Files and Functions.
CS 102 Computers In Context (Multimedia)‏ 01 / 26 / 2009 Instructor: Michael Eckmann.
Introduction to programming in the Java programming language.
CS 100 Introduction to Computing Introduction to JES Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Python Let’s get started!.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
1 CS 177 Week 2 Recitation Slides Variables, Files and Functions.
CS1315: Introduction to Media Computation Introduction to JES.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Controlling Program Flow with Decision Structures.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
CS1315: Introduction to Media Computation Introduction to Programming.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Python programming Using the JES picture functions and defining new functions.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CS1315: Introduction to Media Computation Introduction to JES.
 Prepared by: Eng. Maryam Adel Abdel-Hady
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Learning to use a ‘For Loop’ and a ‘Variable’. Learning Objective To use a ‘For’ loop to build shapes within your program Use a variable to detect input.
Multimedia Summer Camp
Development Environment
Whatcha doin'? Aims: To start using Python. To understand loops.
CS1315: Introduction to Media Computation
Variables, Expressions, and IO
Chapter 2: Introduction to Programming
Chapter 2: Introduction to Programming
Topics Introduction to File Input and Output
Workshop for Programming And Systems Management Teachers
12th Computer Science – Unit 5
Running a Java Program using Blue Jay.
Topics Introduction to File Input and Output
Presentation transcript:

Media Computation in JES (Chapter 2) DM Rasanjalee Himali

JES Functions Some JES functions (NOT Python) A bunch of functions are pre-defined in JES for sound and picture manipulations – pickAFile() – makePicture() – makeSound() – show() – play() Some of these functions accept input values

What to do to show a picture 1. Find a file with a picture. 2. Pick it. 3. Get the bytes from that file into memory and label it as a type: “picture” 4. Show the picture

pickAFile() file = pickAFile() Displays list of directories and files in a window Accept no inputs Returns a string which is the name of file that you picked This does NOT load the file into memory Python is case sensitive: – pickafile() or PickAFile() will not work!

The pickAFile() function brings up a ‘file selector dialog’ You’ll choose a file and hit the ‘Open’ button When you pick your file, the pickAFile() function notes the ‘path name/ address’ (as a string) of your file.

We will stick to JPEG and WAV files to avoid too much complexity. >>> print file F:\UNG\Python\mediasources-py2ed\mediasources\beach.jpg Full file name Path seperator Path to the fileBase file name File extension File ExtensionTypeDescription.jpgJPEG fileContains a picture.WavWAV fileContains sounds

Showing a Picture pickAFile() does not load file into the memory. To load the file into the memory we should tell JES to read the file and make a picture from it using function makePicture: makePicture requeres one parmeter: the file name. >>> pic = makePicture(file) Picture, filename F:\UNG\Python\mediasources- py2ed\mediasources\beach.jpg height 480 width 640 Now we made a picture with hight 480 and width 640 How do we see the picture?

To see the picture you made we need another function: show show takes a parameter : picture (referenced by pic in our example) >>>show(pic)

To summarize, the code: >>>file = pickAFile() >>>pic = makePicture(file) >>>show(pic) Another way to do this is to do it all at once because output from one function can be used as input to another function: >>> show(makePicture(pickAFile)))

QUESTION What is the return type of function show? Try printing: >>> print show(pic) none The output is none, indicating that the function does not return an output. Functions in Python does not have to return a value

Playing a Sound We can replicate the entire process of showing picture with sounds. – We still use pickAFile() to find the file and get its file name. This time we pick a file with.wav extension. – makeSound(filename) creates and returns a sound object, from the WAV file at the filename – play(sound) makes the sound play (but doesn’t wait until it’s done). It takes file name as input. – blockingPlay(sound) waits for the sound to finish

Here are the same steps we saw with making pictures, this time used to play sound: >>>file = pickAFile() >>> print file F:\UNG\Python\mediasources-py2ed\mediasources\helloWorld.wav >>> sound = makeSound(file) >>> print sound Sound file: F:\UNG\Python\mediasources- py2ed\mediasources\helloWorld.wav number of samples: >>> play(sound)

QUESTION Download the mediasouce zip file at : teach#Python teach#Python Extract it to a folder. This contains sample.jpg,.wav etc files you can work with. Try the previous code in both.wav and.jpg files

Naming values As we saw in previous examples, we name values/data using = operator (E.g. file, pic) Computers do not remember things unless we name them Creating a name for a value is called “defining a variable” A variable is a unit of data with an identifier, which is held in your computer's memory It can be changed by putting a new value into it or modifying the value that is already there.

We can check the value of a variable using print: >>>myVariable = 12 >>>print myVariable 12 >>>anotherVariable = 34.5 >>>print anotherVariable 34.5 >>>myName = “Mark” >>>print mayName Mark

We can use names more than once: >>>print myVariable 12 >>> myVariable = “Hello” >>> print myVariable Hello The binding (or the association) between the names and the data only exists until – (a) the name gets assigned to something else OR – (b) you quit JES

QUESTION What is the output of the following? >>>myVariable = 12 >>>print myVariable*4 48 >>>myOtherVariable = “12” >>>print myOtherVariable Data have encodings/types. How data act in expressions depend on their types. E.g. integer 12 and the string 12 act differently for multiplication.

Choosing Good Identifiers Identifiers are the names used to identify things in your code. An identifier is just a name label So it could refer to more or less anything including commands

Python Keywords The following words are the keywords, which form the basis of the Python language. You are not allowed to use these words to name your variables, because these are the core commands of Python.

Naming Rules Variable names must begin with either a letter or an underscore. Although they can contain numbers, they must not start with one. You should not use anything other than letters, numbers, or underscores to identify your variables. Python is generally case-sensitive, which means that lowercase and uppercase letters are treated as being different characters; therefore, myvariable, MYVARIABLE, and MyVariable are interpreted as completely different entities.

We can assign names(i.e. variable names) to the results of a functions. E.g. >>> file = pickAFile() >>> print file F:\UNG\Python\mediasources-py2ed\mediasources\barbara.jpg >>>pic = makePicture(file) >>> print pic Picture, filename F:\UNG\Python\mediasources- py2ed\mediasources\barbara.jpg height 294 width 222 Note: its not necessary to use print every time we execute something. variables

Making a program Defining functions: – Just like naming values, we can name programs. – We can name a series of commands and use the name to whenever we want the commands to be executed. – In Python, the name we define is called a function. – A program in Python is a collection of functions that perform a useful task

You need to use the keyword def to define a new function. Syntax: def ( ): Then we create a collection of commands by defining a block: – The list of commands to be executed when function is called are listed after this line one after the other (indented to show they belong to this function)

Saving and Executing Your Programs The interactive interpreter makes it possible to test solutions and to experiment with the language in real time. However, everything you write in the interactive interpreter is lost when you quit. What you really want to do is write programs that both you and other people can run.

First, you need a text editor. If you are already using JES, you’re in luck: JES provides a nice text editor area for you to write our programs. Simply create a new editor window with File ➤ New Program. Whew! In CSCI1301 you’ll put your instructions in the definition of a function when writing programs.

Now select File ➤ Save Program As to save your program (which is, in fact, a plain text file). Be sure to put it somewhere where you can find it later on. Give your file any reasonable name, such as hello.py. The.py ending is important.

Got that? Don’t close the window with your program in it. If you did, just open it again (File ➤ Open Program). Now you need to load by pressing button “Load Program”.

You can execute/run your program by calling the method hello in command window:

Sometimes, you’ll have a clear purpose for the function, in which case you can name the function accordingly, like “triArea()” or “reverseText()”, but for experimenting or other general purposes, you can call it “main()”, as in “main function”. E.g. def main(): print "Hello world!"

Notice also that the print statement is indented. def main(): print "Hello world!" In Python, indenting specifies the “scope” of different chunks of your code. Consider it this way: everything indented after a first, unindented (or less indented) line ‘belongs’ to that line. So above, the print statement belongs to the definition of the function called ‘main()’. If you wanted ‘main()’ to accomplish multiple things, they’d all have the same amount of indent as the print statement.

QUESTION Write a program showPicture.py to allow user to pick a picture and show it. Call the function that does this pickAndShow. Answer: def pickAndShow(): myFile = pickAFile() myPict = makePicture(myFile) show(myPict) Box indicates a block in program You will know your indentation is Right when all the commands you expect to be in the block is in the box

Now save your program and load it using the “Load Program” button. Now you can call it in your command area: We can similarly define our second program to pick and play sound.

The Most Common JES Bug: Forgetting to Load Your function does NOT exist for JES until you load it Before you load it, the program is just a bunch of characters. Loading encodes it as an executable function Save and Save As You must Save before Loading You must Load before you can use your function An “Unloaded” function doesn’t exist yet.

What if you forget your variable names? showVars()

QUESTION Write a program playSound.py to allow user to pick a sound and play it. Call the function that does this pickAndPlay. Answer: def pickAndPlay(): myFile = pickAFile() mySound = makeSound(myFile) play(mySound)

Show a specific picture def showPicture(): myFile = “c:/mediasources/barbara.jpg” myPict = makePicture(myFile) show(myPict)

What to do about Windows filenames? Python doesn’t like you to use “\” in filenames, like “C:\mediasources\barbara.jpg” What to do? – Option #1: Put r in front of Windows filenames: r“C:\mediasources\pic.jpg” – Option #2: Use forward slashes. Python will translate it for you: “C:/mediasources/pic.jpg”

A function that takes input def playNamed(myfile): mysound = makeSound(myfile) play(mysound) def showNamed(myfile): mypict = makePicture(myfile) show(mypict) What functions do you need? What should be their input? In general, have enough to do what you want, easily, understandably, and in the fewest commands. We’ll talk more about what that means later. How do you call such a function?

QUESTION Write a program with a function playAndShow that plays a sound and shows a picture at the same time. The file names for sound file and picture files should be input parameters to the function def plaAndShow(sFile, pFile) mySound = makeSound(sFile) myPict = makePicture(pFile) play(mSound) show(myPict)

What can go wrong? Did you use the exact same names (case, spelling)? All the lines in the block must be indented, and indented the same amount. Variables in the command area don’t exist in your functions, and variables in your functions don’t exist in the command area. The computer can’t read your mind. – It will only do exactly what you tell it to do.

MOST IMPORTANT THING TO DO TO PASS THIS CLASS! DO THE EXAMPLES! Try them out for yourself. Try to replicate them. Understand them – EVERY WEEK, TYPE IN AT LEAST TWO OF THE EXAMPLES FROM CLASS To understand a program means that you know why each line is there. You will encounter all the simple-but- confusing errors early—BEFORE you are rushing to get homework done!!