Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Summer 2004 Homework 4 Questions

Questions on building link pages?


it says "Given a folder with images and sounds in it..." - is it posted, or do we pick our own?
Make you own. Larry Olson

I'd like to see an example of a page where the image tag
height="50%"
is used. Everywhere I look,
height="x",
where x is an integer and not a percentage.
Try experimenting around with this, if you cannot get anywhere with that, then I can post one of my old webpages that has some pics with that specific tag in it Larry Olson

um - if we're to create our own - how many pics and sounds do we use? there's no specs on what is expected.
If there are no specs, then you should figure out what they should be based on the assignment. You should have enough of each thing for you to test that everything is working properly. I would say that at least two pics (more like three) and at least two sounds( one which is less than two seconds and one which is longer). Your code should function for anything though. Also, you might want to throw in odd files like .bmps, .gifs, another html file, or text files and see if they are handled properly. Larry Olson

umm...when you write the function def linksPage(directory), how do you choose a directory to run this function? obviously, pickAFile() won't work....

nm, i got it

I'm still unable to figure out how to make the height 50%. Any suggestions?


The HTML specification put out by the World Wide Web consortium does -NOT- include a height="xx%" specification to be used as requested for this homework. You can indeed give a height="xx%", however,

"...lengths expressed as percentages are based on the horizontal or vertical space currently available, not on the natural size of the image, object, or applet." (http://www.w3.org/TR/html4/struct/objects.html#adef-height-IMG)

In other words, the image will not resize to 50% unless there is some other object restricting the size. Perhaps a pixel size would be better for this assignment, for instance height="200", and the width will adjust properly on its own. -jm
That summary is not quite right, stick with the quoted original. The image tag when used with a percent will resize the picture to be that percent of the space it has available (See the html lab for more details). However, yes you can use the height tag with a pixel value to get the image resized properly. Larry Olson

do we use the writeTo... function on the part to save the sounds? i am confused about that part along with the height to 50% question as it seems that everyone else is having problems with as well.
1) You need to use writeSoundTo to write out a sound. 2) Calculate what half the height of the picture is and then use the height = tag to set that to be the height of the images on the generated webpage. Larry Olson

here is my code, I was wondering if you could help me out because all of my thumbnail pictures are not showing the correct heights, but when i print the height(as seen in my code) it appears to be working. also, my sound links are playing the previously listed sound. can you please please please help me larry?
import os

def linkspage(directory):
  samplesfile = open(directory+"//index.html","wt")

  samplesfile.write(doctype())
  samplesfile.write(title("Samples from "+ directory))
  samples = "<h1> Samples from "+directory+"</h1>"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      picture = makePicture(directory+'/'+file)
      heightOfFile = int((getHeight(picture))/2)
      print heightOfFile
      samples = samples +"<p>Filename: "+file
      samples = samples +'<a href= "' +file+'"><img src="'+ file+'"height="heightOfFile"></a>'
  for sound in os.listdir(directory):
    if sound.endswith(".wav"):
      samples = samples +"<p>Soundname:"+sound
      samples = samples +'<a href="' +sound+'"length="2"/></p>'
  samplesfile.write(body(samples))
  samplesfile.close()


def doctype():
  return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition//EN""http://wwww.w3.org/TR/html4/loose.dtd">'

def title(titlestring):
  return "<html><head><title>"+titlestring+"</title></head>"

def body(bodystring):
  return"<body>"+bodystring+"</body></html>"

1) pictures: If you look at your code, you have heightOfFile being calculated which is as it should be. However, when you place heightOfFile in your HTML page though, you have written code that literally enters the value "heightOfFile". Also, you may want to look at the loop for pictures that you have used, I do not believe that it will get all of the pictures it should. 2) sound: In your sound loop you are opening a bunch of anchor tags but are not closing them. Remember that in HTML almost everything that you open you must close. Thus, <a href="blah"> link here </a> Larry Olson

whoa, what happened to my code when i did that


when i use code like the one above that i got from the lecture slides...the html page is just a few of my pics...i don't understand why it does this since they all end with ".jpg". Can you explain what the problem is?

The image sizing is not working in your code because you're including JES code inside your raw HTML code. The part where you wrote
<img src="'+ file+'"height="heightOfFile">
is purely HTML, therefore the browser does not understand what "heightOfFile" means. It would understand if you simply put a number there. Perhaps there is another way you could do it. Nicole Seekely


do we have to make the shortened version of the sound with JES?
Yes. Think about a 'manual' alternative; you would have to go to every computer that you ever wanted to run this program on, and make short versions of every single .wav file.Dannon Baker



if i am trying to make a function not do something...like print this if a file.endswith, but does not file.startswith....how do i do that? i know the startswith thing works, but I can't figure out how to say not. i tried... and (not sound.startswith()), but that didn't work even though "not" turned blue
Here's an example of how 'not' can work.
def foo():
  for i in range (1, 20):
    if not(i > 10):
      print i
    else:
      print i + 30
The function prints the numbers 1 - 10, and then 41-49. Dannon Baker

also...i still don't understand why my code is only grabbing a few og my pics b/c i use...if file.endswith(".jpg") but for some reason it only pics up three of my pictures when I have like 10 in the folder
You said that you were sure they were all jpegs. Are you sure they are all lowercase ".jpg". Maybe some of them are ".JPG".Dannon Baker

ok so i changed my if statement to...if file.endswith(".jpg") or(.JPG")...and then it turned everything into pic links including sounds and html files. did i do something wrong? thanks for all your help dannon...sorry i am having s many problems!
You changed it to
if file.endswith(".jpg") or file.endswith(".JPG"):
And not
if file.endswith(".jpg") or (".JPG"):
Right?

If you did the second one, then JES evaluates the statement and just says "Ok, so the file doesn't end with ".jpg", but it sees the ".JPG" and since there isn't a comparison there, JES just evaluates the expression, and says "Well, that must mean true, because it isn't zero". An example would be
if (1):
That block will always get executed. It looks at the value and decides that it isn't zero, so it must be true. Just like
 if ("JPG"):
Dannon Baker

having a little problem trying to figure out how to make the length of the sound 2, wanting to know if there is some kind of function such as img src... that works for sound so we can do something similar to height=2 but instead length =2
The issue with that approach is that you're not actually embedding the sound in the page.
You are actually making a link(anchor) TO the sound(s).
You need to create a 2 second sound in JES, and save it to the folder, for the short version.

As a hint,
makeEmptySound(2)
returns an empty sound, two seconds long.Dannon Baker

ok, I am the one that posted the code earlier and now i fixed those problems but i can't figure out how to make the short sound 2 seconds long and i don't know how to save that version, please help. here is what i have
import os

def linkspage(directory):
  samplesfile = open(directory+"//index.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Samples from "+ directory))
  samples = "<h1> Samples from "+directory+"</h1>"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      picture = makePicture(directory+'/'+file)
      heightOfFile = str(int((getHeight(picture))/2))
      print heightOfFile
      samples = samples +"<p>Filename: "+file
      samples = samples +'<a href= "' +file+'"><img src="'+ file+'"height="'+heightOfFile+'"></a>'
  for sound in os.listdir(directory):
    if sound.endswith(".wav"):
      samples = samples + '<p><a href="' +sound+'"/>full:"'+sound+'"</a></p>'
      samples = samples +'<p><a href="' +sound+'"Length="2"/>short:"'+sound+'"</a></p>'
      print sound
  samplesfile.write(body(samples))
  samplesfile.close()


def doctype():
  return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition//EN""http://wwww.w3.org/TR/html4/loose.dtd">'

def title(titlestring):
  return "<html><head><title>"+titlestring+"</title></head>"

def body(bodystring):
  return"<body>"+bodystring+"</body></html>"


Please post code in code tags:

<code> YOUR CODE HERE </code>
Otherwise it's near impossible to read.

And really, it isn't necessary to post your entire program for a question like that. We don't want to just fix your code, rather we want to teach you how to fix it, and answer questions about what you need to do. For the part of the program you are asking about, you need to have some bit of code that opens a JES sound, and if it is longer than 2 seconds, creates a new sound, of just the first two seconds. You already learned how to copy samples from one sound to another, now you need to apply that knowledge and copy just the first 2 seconds worth of samples from the big file to the little one. And then save the little file..Dannon Baker


My code is able to display the reduced version of each picture and are working links to the full size picture displayed in the same browser, does that meet the requirements of this assignment?

Dannon- can you elaborate more on this
makeEmptySound(2) function and exactly how to create the body of this function? I can't find any sample syntax in the textbook. Thanks.

The makeEmptySound(x) function simply makes a sound that is "x" seconds long. For example, if you wanted to copy a sound onto a new "canvas" or "target," you could insert something into your code like target=makeEmptySound(5). This will create an empty sound file that is 5 seconds long, and you can use it to copy samples from a different sound file to. If that wasn't exactly what you were asking for, then Dannon can probably answer it better. :) Here's an example for syntax purposes: Nicole Seekely
def copy():
  canvas=makeEmptySound(3)
  for sourceIndex in range(1,20000):
    setSampleValueAt(canvas,sourceIndex,1000+sourceIndex)
    sourceIndex=sourceIndex+1
  return(canvas)


Nicole's example should help you out, but if you did want to look in the book, makeEmptySound is referenced on pages 170 and 188 (straight out of the index..).




two questions and I feel kinda dumb asking them... 1. are we writing something that our TA can use any pics and sounds for that they have thrown somewhere or are we doing something specific and submitting the sounds and stuff? 2. the directory part... i know a directory is a folder, but I'm very confused about this part

There's no need to feel dumb! You are certainly not the first person to ask either of those questions. You're to write a program that anyone can use on their own computer. So, you will not be submitting your own sounds or pictures. The function should take in a directory as input, therefore your TA will be able to tell JES what folder to look in on their computer to make the html page. Yes, the directory is the same thing as the folder - what exactly is your question in regards to this? The directory/folder is simply the place where your index.html page will be made and where JES will look in for the .jpg and .wav files. Nicole Seekely

I (person above) figured out what I was confused about. I wasn't thinking and forgot that putting directory inside of the () would just be like a place holder

making the pictures smaller is killig me... if I put in the percentage exactly like the way it says in the lab for percentages then I get an error, and I've tried virtually every variation of quotations that I can think of to fix it and I one does work but it doesn't alter the size of the picture (plus I think this just makes it 50% of the total screen anyways) The other way to do it would be to create an entire function to halve the picture outside of the HTML, which I've tried repeatedly how to do, both by creating a completely separate function and by inserting stuff within my function. . Christine Dehn

Christine, I think you've got the right idea. The way to go is to create a variable outside the HTML for the height of the picture like you said. Are you getting an error that says:
An attempt was made to call a function with a parameter of an invalid type. This means that you did 
something such as trying to pass a string to a method that is expecting an integer.
If so, then you probably need to convert the height number into its string representation by using the str() function so that you can use it. Try looking at that part of the person's code above for guidance. Nicole Seekely


How would you get how many samples that 2 seconds has? Is it 2 secs times the sampling rate?
Correct. Sample rate is samples/second, so for the number of samples in a 2 second sound: samples/second x 2seconds Dannon Baker

If I am getting an "input is not a picture" error, would my error be in the code itself or the execution of the function?
My best guess(without seeing your code is that you are trying to make a picture out of a sound or out of a file that is misnamed. These errors are both likely linked to your code not you input Larry Olson.

def copy():
  homer=makeSound(getMediaPath("homer.wav"))
  target=makeEmptySound(2)
  index=1
  for source in range(1,44100):
    value=getSampleValueAt(homer,source)
    setSampleValueAt(target,index,value)
    index=index+1
  for source in range(1,getLength(target)):
    setSampleValueAt(target,index,value)
    index=index+1
  play(target)
  return target

This follows the procedure in the book, but I still can't get it to play anything more than noise or silence? Is this not the right way to copy a sound?
This code should have an error everytime it is run. As soon as you hit the second for loop, your index is going to be 44101. Which will give you an array out of bounds. If you take just the first loop, it seems that should work. Alternately, make a modification to the second for loop and it should work as well. (Using the second for loop would be considered the "better" way. Larry Olson

I am having trouble testing what I program into jes. Can you explain how to test what I program. For example...accessing the directory. Thanks

Generally what I do is I run pickAFile() and then choose a file that's in that directory. JES will print out the full path to that file. I then just copy the directory path (but exclude the filename) and paste that into my function as input. Also, for the purpose of testing, I would make a separate folder that includes just a few .jpg, .JPG. and .wav files. I wouldn't use anything large like your entire mediasources folder. Nicole Seekely

Everything works except for the 50% thing. Actually it's weird cause the code works on another person's computer but it's not on mine.
for file in os.listdir(dir):
    if file.endswith(".jpg") or file.endswith(".JPG"):
      indexpage.write("<p>Pictures")
      pic=makePicture(dir+"\\"+file)
      if pic:
         halfheight=str(int(getHeight(pic))/2)
         indexpage.write('\n<a href="'+file+'">\n')
         indexpage.write('   <image src="'+file+'" alt="'+file+'" height='+halfheight+'>\n</a><br>\n')

Please help

your () are in the wrong places also remember to add str because you want it to be a string Christine Dehn

well so far i've got this... but it's not working... why not...
def shortSound(sound):
  file=sound
  source=makeSound(file)
  target=makeEmptySound(2)
  targetIndex=1
  value=getSamplingRate(source)
  print value
  value2= getLength(source)
  print value2
 
  value3=int(value2/value)
  print value3
  for sourceIndex in range (1, value2): 
    setSampleValueAt(target,targetIndex, getSampleValueAt(source,sourceIndex))
    targetIndex=targetIndex + 1 
  play(target)
  return(target)



import os

def linkspage(directory):
  samplefile = open(directory+"//index.html","wt")
  samplefile.write(doctype())
  samplefile.write(title("Pictures & Sound from"+ directory))
  samples="<h1> Pictures & Sound from<br>"+directory+"</h1>"
  for file in os.listdir(directory):
    if file.endswith(".jpg") or file.endswith(".JPG"):
      picture=makePicture(directory+'/'+file)
      height=str(int((getHeight(picture))/2))
      print height
      samples=samples +"<p>Filename: "+file
      samples=samples +'<a href= "'+file+'"><img src="'+file+'"height="'+height+'"></a>'
  for sound in os.listdir(directory):
    if sound.endswith(".wav"):
      soundFile=makeSound(directory+"/"+file)
      length=str(getLength(soundFile))
      samples=samples+'<p><a href="'+sound+'"/>full:"'+sound+'"</a></p>'      
      if length > 2:
        twoSecondSound(directory+'/'+file) 
        samples=samples+'<p><a href="'+sound+'"/>short:"'+sound+'"</a></p>'
      print soundFile
  samplefile.write(body(samples))
  samplefile.close()

def doctype():
  return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition//EN""http://wwww.w3.org/TR/html4/loose.dtd">'

def title(titlestring):
  return "<html><head><title>"+titlestring+"</title></head>"

def body(bodystring):
  return"<body>"+bodystring+"</body></html>"

def twoSecondSound(path):
  origSound=makeSound(path)
  emptySound=makeEmptySound(2)
  for i in range(1,getLength(emptySound)):
    srcSample=getSampleValueAt(origSound, i)
    setSampleValueAt(emptySound,i,srcSample)
  return emptySound



I'm getting this error message:
85
272
147
176
Filename C:\Documents and Settings\Default\Desktop\Hw4/index.html could not be read.
Are you sure it's a valid sound file?
Filename C:\Documents and Settings\Default\Desktop\Hw4/index.html could not be read.
Are you sure it's a valid sound file?
Sound of length 66150
Filename C:\Documents and Settings\Default\Desktop\Hw4/index.html could not be read.
Are you sure it's a valid sound file?
Filename C:\Documents and Settings\Default\Desktop\Hw4/index.html could not be read.
Are you sure it's a valid sound file?
Sound of length 66150
Filename C:\Documents and Settings\Default\Desktop\Hw4/index.html could not be read.
Are you sure it's a valid sound file?
Filename C:\Documents and Settings\Default\Desktop\Hw4/index.html could not be read.
Are you sure it's a valid sound file?
Sound of length 66150
Filename C:\Documents and Settings\Default\Desktop\Hw4/index.html could not be read.
Are you sure it's a valid sound file?
Filename C:\Documents and Settings\Default\Desktop\Hw4/index.html could not be read.
Are you sure it's a valid sound file?
Sound of length 66150
Filename C:\Documents and Settings\Default\Desktop\Hw4/index.html could not be read.
Are you sure it's a valid sound file?
Filename C:\Documents and Settings\Default\Desktop\Hw4/index.html could not be read.
Are you sure it's a valid sound file?
Sound of length 66150
The answer is in the error message. Look at the directory structure. Desktop\Hw4/index.html. You must have a directory structure that is all the same. In otherwords, you shouldnt have a / instead it should be a \ Larry Olson




Link to this Page