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

Spring 2004 Homework 4 Questions

Questions? Comments? Issues?

"Given a folder with images in it, create an index HTML page with links to each image." When will we be getting this folder for homework 4?

No – you make one up for yourself. It needs to work for ANY folder with pictures in it. Mark Guzdial




This homwork is a little bit too difficult, if you ask me.
I did 75% of it in breakout yesterday in about five minutes. It's not all that hard. Mark Guzdial

Couldn't it have been a little less demanding, technically? After all, we're just learning html. Doing so much so soon seems a little much to ask.


After you go over what you'll be going over in breakout, it should be much, much easier sounding...

I had breakout today and I still feel like I have no clue what to do. The html index will have links to large pictures, which will have links to the scaled images? Is that what is meant by the comment about the anchor?
Other way around. The href= will point at the large picture. Put the image src="qtr_..." reference where you'd put the text to click. Mark Guzdial

I don't even know where to start...this thing is way too hard
Try starting by editing Recipe 92 on page 250. Mark Guzdial

I absolutely agree. It's just very, very daunting, and I feel like there is no way I can do it, I've been staring at JES for hours and have seen very little accomplished, and it just makes me want to give up. What's sad is that my lowest grade so far in this course on anything is a 97, so you'd figure I'd have some grasp of what to do. But I don't, and it's aggravating, and I think I'm done with this until the questions page sheds some more insight on how to do it.
You might check out the Powerpoint slides at http://coweb.cc.gatech.edu:8888/uploads/cs1315/910/functions.ppt. Mark Guzdial

I agree. It would be helpful if the contents of the hw were explained in lecture on Monday in class, before we tried to attempt it. It's just too difficult. And why is Lab5 due next Friday(the same day as Midterm2)?
We're explaining it in breakout this week. I agree about the Lab5 deadline – that's why it's been pushed off until the following Monday. Mark Guzdial

Why don't we have our quiz 2 grades up yet? You would think they would be graded since Lab4 was due the Monday RIGHT AFTER SPRING BREAK!!
They're due from the TA's today. The Institute was closed last week, so the TA's took a break, too. Mark Guzdial

try looking at fall 2003 hw4 questions. the assignment is not the same but it's a start. it's helped me a little bit
Excellent idea! Mark Guzdial

a better question is why isn't my grade up yet for our first take home exam...we just got our hwk 3 grades right after spring break. sorry to rant and yes this assignment is hard.
If you don't have your take home exam grade yet, something is seriously wrong. Please send me email and contact your TA. Thanks! Mark Guzdial


So, if the program should work for any picture, what picture names do we use when we are scaling down the pictures and putting them in the HTML page? Should we just come up with generic names like pic1, pic2, etc. or do we use pickAFile() in the program to get the pictures?
That's in the assignment. Use the filename (which you get from os.listdir()) and stick "qtr_" in front of it, then save it back to that directory. Mark Guzdial



I copied this from one of our lecture slides(from breakout) and I cannot get it to work. what am i doing wrong?

def makePage(): 
file=open("generated.html","wt") 
file.write("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition//EN" "http://wwww.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<title>The Simplest Possible Web Page</title> 
</head> 
<body> 
<h1>A Simple Heading</h1> 
<p>Some simple text.</p> 
</body> 
</html>""") 
file.close() 



i went to the command area and typed : makePage()
I even created a blank html file named "generated.html"
For some reason i am finding this recent stuff kind of difficult
Amelia Cipolla


Well, you will have to go to edit to see what I REALLY copied out of the slide....coweb is reading it and displaying it!
Amelia Cipolla
Amelia, you didn't indent the body of the function – you have to have all the statements indented by at least one space for Python to realize that makePage() is all of that. And read the FAQ on how to post program code here so that it's readable in the CoWeb–it's harder to do when there is HTML inside your programs. You can edit this page and see how I modified yours to work. Mark Guzdial

This is more of a mini-pep talk than the answer to any question. I hadn't looked at this page before giving my breakout lecture today, but found myself using exactly the same phrases that many of you have. "It looks more daunting than it is"; "If you clear all this html boilerplate into separate functions, you'll be able to make some sense of the structure of the function" etc., etc. It seems that several (i.e. many) of you are naturally being put off by the apparent scope of this problem, not knowing where to start. You high-achievers often have this problem. It's perfectionism: you don't want to start something that you don't already know how to finish. Well, consider this advice from a former perfectionist turned slob: Break the problem as stated into little problems and get those working one at a time, even though you're not actually getting the "right" answer. For example, before resizing anything, display the images as you find them in the folder. Creating a page that contains a bunch of images is a part of this problem, so solve it isolation. Then think about how you create the new files and file names (with the qtr_ prefix) and create and save them without any resizing taking place. Prove to yourself that the files exist and contain the correct images. Then resize them. Alternatively, do the subproblems in a different order that makes more sense to you. If I tried to solve this problem in one go, I would freak out – either that or play solitaire for hours in a state of numb denial. Really. Divide and conquer. It's more fun, and less work in the long run. Colin Potts


I am bothered that a PhD and professor of CS who wrote the text and designed the course would use how long it took him to do 75% of what is required to do this assignment as some indication of the complexity of the task asked of his non-CS undergraduate students who in many cases have never had a formal CS course.
I think there was a misunderstanding of what he was saying. He was just trying to hint that maybe you should come to breakout as he went over it pretty extensively. Yes, quickly (5 minutes), but extensively, and what he went over would definitely help for this homework and make it not so difficult. Ashley Coker

These homeworks are supposed to be real homeworks, you know. We're not in high school anymore, and he IS the professor. So when he says it took him "about 5 minutes", that probably means it will only take us an hour or two, which is pretty good, especially for Tech. I agree that it sounds really hard; I don't even know where to start, but I don't think that this comment was sensible.

People need to stop whining and post some code so that they can give us some tangible help with the homework.

I'm sorry that I was confusing: I meant that we did it in about five minutes in class. With explanation. I meant that as encouragement to come to breakout, that we explain much of HW4 and that it doesn't even take long to do 75% of the HW. Mark Guzdial

So...we're sitting here trying to work this baby out, and everything lines up except for the text links to the pictures. We put up 3 pictures (scaled down very nicely), along with filenames next to each one. The problem is that the text link next to the first picture ISN'T A LINK; the text link beside the second picture links to the first pic (fullsize); and the text link beside the third picture links up to the second pic (fullsize). Is there something wrong with this part of our code?...
#write the picture out
    filename= directory+"//"+"qtr_"+file 
    writePictureTo(canvas, filename)
    # Now generate the HTML
    samples=samples+"<p>Filename: " +file
    samples = samples+'<a href="'+file+'">'
    samples=samples+'<image src="'"qtr_"+file+'" /></p>\n'
  samplesfile.write(body(samples))
  samplesfile.close()


Asma Rifay
Rizwan Ladha

I think I have most everything working except I need it to only list the files that aren't quarter files instead of all of what it does now which is list all of the files. ps :: to the question above, i'm not sure, but maybe its because you never closed out the <a href> bracket??
Exactly right! Mark Guzdial

for file in os.listdir(directory):
      if file.endswith(".jpg"):
         samples=samples+"&ltp>Filename: " +file
         samples = samples+'&lta href="'+file+'">'
         samples=samples+'&ltimage src="'"qrt_"+file+'" />&lt/a>&lt/p>\n'
   samplesfile.write(body(samples))
   samplesfile.close()

Jessica Lord


I went in and indented as you said....I am still getting "A local or global name could not be found. You need to define the function or variable before you try to use it in any way." And i DID load it. what is the deal?

def makeHomePage(name, interest):
  file=open("generated.html","wt")
  file.write("""&lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01    Transition//EN" "http://wwww.w3.org/TR/html4/loose.dtd">
  &lt;html>
  &lt;head>
  &lt;title>"""+name+"""'s Home Page&lt/title>
  &lt;/head>
  &lt;body>
  &lt;h1>Welcome to """+name+"""'s Home Page&lt/h1>
  &lt;p>Hi!  I am """+name+""".  This is my home page!
  &lt;I am interested in """+interest+"""&lt/p>
  &lt;/body>
  &lt;/html>""")
  file.close()


And in the command area i typed makeHomePage(Amelia, cooking)

Amelia Cipolla
Amelia, try makeHomePage("Amelia","cooking"). Mark Guzdial

Still not working. Do you think it is not finding the file generated.html??? I have it in my JES folder. Do I need to write out a more complete path?
Amelia Cipolla

No, that should be fine. Try turning on Expert mode (under Edit, Options) and see what is the name that it can't find. Mark Guzdial


can someone explain how to reduce an image to the quarter of its size then link it to the full image??? i have no idea how to do this in jes. would i have to write another function telling it to reduce the picture 1/4 of its original size??? then how would i incorporate that into the rest of the function and html? I looked at recipe 92 and that just makes the picture's height all to 100 with no links or anything. and plus my break out didn't explain the hw at all. thanks to anyone that responds.
Do look at the scaling code I posted below. Mark Guzdial

its not -that- hard people...stop complaining

is there problem with adam wilson's section? the grades for quiz 2 are all zero's. i checked mine, and a few others, and everyone is a zero. i know i did not get a zero, so what is the mix up? thanks...

Here's the program that I was using in breakout:
import os

def makeSamplePage(directory):
  samplesfile=open(directory+"//samples.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Samples from "+directory))
  # Now, let's make up the string that will be the body.
  samples="<h1>Samples from "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      samples=samples+'<p>Filename: <a href="'+file+'">'+file+'</a>\n'
      pic = makePicture(directory + "//"+file)
      samples=samples+'<image src="'+file+'" height="100" />\n'
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"
  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>"



Mark Guzdial


The index.html page is not an additional page that we are creating seperate from our HW4, correct? It is a part of the function, right?¿ Lauren Patton
Part of the function? HTML isn't part of a Python function. You are supposed to GENERATE the HTML from your function. Mark Guzdial

to scale down each picture in our directories that means that we have to create a "def" to do that, right? (like def title(), and def body() above) well, the function in the book that scales down a picture uses getMediaPath()...should we?...and if we are supposed to use makeEmptyPicture aren't we supposed to specify the demensions that we want that picture to be? we don't know what pictures will be imported...
thanks
~Sk
Yes, you'll need a function to do the scaling. You can do it in your linksPage function, if you want, but it's easier to do it in a separate function. Mark Guzdial



When using the command "makeEmptyPicture" in JES, how does one indicate the dimensions of the empty picture? should it be something like "makeEmptyPicture(40, 60)" to make a picture 40 pixels by 60 pixels?
Removed at RW's request
Yes, Robert. "makeEmptyPicture(40,60)" will make a picture that is 40 pixels by 60 pixels. Ashley Coker



Ok, all I am doing right now is copying the MakeSamplePage(directory) example from lecture and trying to get it to work with a directory of my own, a folder I created including JPEG files specifically for this HW. I cut and pasted the code and entered this into the command area:

>>> linksPage("C:\Documents and Settings\Catherine Covington\My Documents\CSHW4\")

And get the following error: Your code contains at least one syntax error, meaning it is not legal jython

What could be my syntax error? Thanks! Can't do much if I can't get this to work!

Catherine Covington

To the question above, I think, if you are using a PC you have to put -r"C:\Documents and Settings\Catherine Covington\My Documents\CSHW4. That might fix your problem. Emily Malone
Like Emily said! Mark Guzdial


<pre>
import os

def linksPage(directory):
  samplesfile=open(directory+"//index.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Samples from "+directory))
  # Now, let's make up the string that will be the body.
  samples="<h1>Samples from "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      samples=samples+"<p>Filename: "+file
      samples=samples+'<image src="'+file+'" height="100" /></p>\n'
  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>"
</pre>


This is the generic code from the lecture slides. We have tried to format it so we could create the new html page in multiple ways. Could you give us any suggestions?
SURE! See Below. Mark Guzdial
import os

def linksPage(directory):
  samplesfile=open(directory+"//index.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Samples from "+directory))
  # Now, let's make up the string that will be the body.
  samples="<h1>Samples from "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      samples=samples+"<p>Filename: "+file
      samples=samples+'<mage src="'+file+'" height="100" /></p>\n'
      #INSERT HERE SOMETHING LIKE smaller = makeSmallerPicture(directory+"//"+file)
      #INSERT HERE SOMETHING LIKE writePictureTo(smaller,directory+"//qtr"+file)
  samplesfile.write(body(samples))
  samplesfile.close()



I think there was a question here at the end that was posted that I accidentally deleted...from Stephen someone or something? SORRY!!! Ashley Coker

What will the TA put in the command area when they check our HW? I have a code, but I keep getting error messages when I try to check it. So I believe I am doing something wrong.


They will put
>>>linksPage(r'C://My Documents...')
What goes inside the parentheses is the path to the file folder they have created that will hold all your pictures. Ashley Coker |

I have copied the code exactly from the breakout it won't work at all! What is wrong?Deborah Stephens
What exactly won't work? You're going to have to be a little more specific on that one... Ashley Coker

I am having the same problem. "A local or global name could not be found. You need to define the function or variable before you try to use it in any way." I am running the one from the breakout as well as Recipe 92. I know that I get this error all of the time, but it has just been too long since I have used it that I can't remember how to fix it... Where do I go? Meredith Harman
Turn on Expert mode in the Edit menu (under the Options item) and tell us WHAT NAME can't be found. Mark Guzdial

I have tried the following code in order to attempt to create a thumbnail but each time I run it I keep getting an error in my "linkpage" where I inserted my the function.

def createThumb():
pic = makePicture(file)
thumb = makeEmptyPicture
sourceX = 1
for targetX in range((1,getWidth)/2):
sourceY = 1
for targetY in range((1, getHeight(pic))/2):
color = getColor(getPixels(pic,sourceX,sourceY))
setColor(getPixel(thumb,targetX,targetY), color)
sourceY = sourceY + 1
sourceX = sourceX + 1
writePictureTo("qrt_"+file)
return

I keep getting an error here...
for file in os.listdir(directory):
if file.endswith(".jpg"):
createThumb(file) ———this line
samples=samples+"

Filename: "+file
samples=samples+'a href = " '+fi.........

It doesn't look like your "createThumb()" function takes in any parameters (input), yet on that line you pass it one (file). Dannon Baker

Ok so what I really dont understand is how to see if this program works. What exactly is supposed to be the result of it? If we dont have pictures how do we tell if its worked
Even in a directory with no files, your program should create an html file with a header, etc. named "index.html" You could always just throw a few pictures in a test directory, if that'd help you see the results. Dannon Baker

Okay, so index.html is supposed to have the pictures' filenames, then a quarter size image that is linked to the full size image, and then the height and width of the image? Is that right? Kristin Noell

Right, but you don't have to write out the height and width. Summer McWilliams
Right – the heigth and width was just part of my example. Mark Guzdial



I am confused...is the code that Professor Guzdial posted above from breakout part of the code that we will use for this hw or is it different?

What is missing from his code that we need to include
Well said! :-) The code that I show does the makePicture at the point that you need to generate the thumbnail. Use that as a starting point. Mark Guzdial


When TAs examine HW4, do they use "setMediaPath" funtion for "def qurSize():" part? Or are we supposed to get a file without "getMediaPath" funtion?
NO, do not use setMediaPath nor getMediaPath! Remember what those are for: To set a pre-defined directory for accessing media. You don't have to do that here! You KNOW where the media is – in the input directory! Mark Guzdial

This is my fuction so far. The only thing i am having problems with is making the links page show the quarter sized (1/8 total area) thumbnails. I have a function to make the images 1/8 the size but i don't know where to put it in the original fuction. I tried puting the qtrSize(file) function under the "if file.endswith(".jpg"):" line but it 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." Am I supposed to use the word "file" inside the qtrSize() fuction? If file is supposed to be in there then what am I doing wrong? If i'm not supposed to use "file" then does anyone have any suggestions? thanks in advance.

Dong Kim
Doug, you have written your code so that qtrSize takes NO inputs, but you expect the variable file to exist. Remember that variables don't exist outside of their function contexts. If you want the variable file to be in qtrSize, you're going to have to pass it in as input. Mark Guzdial

<HTML><PRE>
import os

def linksPage(directory):
  samplesfile=open(directory+"//Index.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Samples from "+directory))
  # Now, let's make up the string that will be the body.
  samples="&lth1>Samples from "+directory+" &lt/h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      samples=samples+'&ltp>Filename: &lta href="'+file+'">'+file+'&lt/a>\n'
      samples = samples+'&lta href="'+file+'">'
      samples=samples+'&ltimage src="'"qtr_"+file+'" />&lt/a>&lt/p>\n'
  samplesfile.write(body(samples))
  samplesfile.close()

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

def title(titlestring):
  return "&lthtml>&lthead>&lttitle>"+titlestring+"&lt/title>&lt/head>"

def body(bodystring):
  return "&ltbody>"+bodystring+"&lt/body>&lt/html>"

def qtrSize():
  qtrf = makePicture(file)
  canvas = makeEmptyPicture(getWidth(qtrf)/4, getHeight(qtrf)/4)
  sourceX = 1
  for targetX in range(1,getWidth(qtrf)/4):
    sourceY = 1
    for targetY in range(1,getHeight(qtrf)/4):
      color = getColor(getPixel(qtrf, sourceX, sourceY))
      setColor(getPixel(canvas, targetX, targetY), color)
      sourceY = sourceY + 1
    sourceX = sourceX + 1
  writePictureTo(directory+"//"+"qtr_"+file)
  return canvas
</PRE></HTML>





I copied this sample into JES, loaded it, and then created a directory with a few pictures in it. the directory is C:\Documents and Settings\Amelia\My Documents\JESall\JES\directoryimages. then in JES command area i wrote
makeSamplePage(r'C:\Documents and Settings\Amelia\My Documents\JESall\JES\directoryimages)

I an error saying
Your code contains at least one syntax error, meaning it is not legal jython.

why is this?
Because you didn't close the quote on your makeSamplePage. Try this: makeSamplePage(r'C:\Documents and Settings\Amelia\My Documents\JESall\JES\directoryimages') (notice the final quote). Mark Guzdial
this is the example i copied:
<html><pre>
import os

def makeSamplePage(directory):
  samplesfile=open(directory+"//samples.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Samples from "+directory))
  # Now, let's make up the string that will be the body.
  samples="&lt;h1>Samples from "+directory+" &lt;/h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      samples=samples+'&lt;p>Filename: &lt;a href="'+file+'">'+file+'&lt;/a>\n'
      pic = makePicture(directory + "//"+file)
      samples=samples+'&lt;image src="'+file+'" height="100" />\n'
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"&lt;/p>\n"
  samplesfile.write(body(samples))
  samplesfile.close()

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

def title(titlestring):
  return "&lt;html>&lt;head>&lt;title>"+titlestring+"&lt;/title>&lt;/head>"

def body(bodystring):
  return "&lt;body>"+bodystring+"&lt;/body>&lt;/html>"
</pre></html>


Amelia Cipolla


I am getting an error when i try to define the file as a picture but if i take it out it doesnt work either....?
Think through what you're doing. The os.listdir returns base file names – you need a complete path. makePicture returns a picture that you'll need to use. Go take a look at my example from breakout again. Mark Guzdial
import os
def linksPage(directory):
#make thumbnails
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      makePicture(file)
      targetX=1
      for sourceX in range(1,getWidth(file)):
        targetY=1
        for sourceY in range(1,getHeight(file)):
          color=getColor(getPixel(file,sourceX,sourceY))
          setColor(getPixel(canvas,targetX,targetY),color)
        targetY=targetY+4
      targetX=targetX+4


so this is my whole code...
<html><pre>
import os

def linksPage(directory):
  samplesfile=open(directory+"//samples.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Samples from "+directory))
  samples="<h1>Samples from "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      createThumbnail(file)
      samples=samples+'<p>Filename: <ahref="'+file+'">'+file+'</a>\n'
      pic=makePicture(directory + "//"+file)
      samples=samples+'<image src="'+"qtr_"+file+'"/></p>\n'
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"
  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>"

def createThumbnail():
  pic = makePicture(file)
  thumb = makeEmptyPicture
  sourceX = 1
  for xOffset in range((1,getWidth)/2):
    sourceY = 1
    for yOffset in range((1, getHeight(pic))/2):
      color = getColor(getPixels(pic,sourceX,sourceY))
      setColor(getPixel(thumb,xOffset,yOffset), color)
      sourceY = sourceY + 1
    sourceX = sourceX + 1 
  writePictureTo("qrt_"+file)
  return()

</pre></html> 

I don't get an error or anything, but my pictures show up as red x's...what am I doing wrong? Thanks! Oh, and sorry about the spacing and stuff...I don't know how that happened. Karin Bowman
Doublecheck what you're naming those thumbnail files. qrt!=qtr :) Dannon Baker
Do View Source to see what you're actually storing in the HTML file. Mark Guzdial

I keep getting a syntax error as well. Lauren Patton
Lauren, what is the syntax message and where is it saying that the error is? (The error messages do tell us something.) Mark Guzdial

<html>
<pre>

import os

def linksPage(directory):
  samplesfile=open(directory+"//index.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Index from "+directory))
  # Now, let's make up the string that will be the body.
  samples="<h1>Samples from "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      samples=samples+"<p>Filename: "+file
      samples=samples+'<image src="'+file+'" height="100" /></p>\n'
  samplesfile.write(body(samples))
  samplesfile.close()
</pre>
</html>

A local or global name could not be found. You need to define the function or variable before you try to use it in any way.

Above are the code and error message I received by writing this function in JES. Earlier today, this exact code was correct. I was able to create a directory and a new HTML page. I have not modified the code, but I'm still getting the error message. Where is my mistake? Lindsey Richardson
What error message and where, Lindsey? Mark Guzdial
def doctype():
return ''

def title(titlestring):
return ""+titlestring+"" def body(bodystring): return ""+bodystring+""


Kyla LeCroy
I put the code that Mark posted above from breakout and loaded it, directed jes to my specific file in the command area. When I went to open the created HTML file, the text showed up, but absolutely no pictures...
The file is created in a directory that has pictures in it, correct? If there are no pictures in the directory, it'll show up just as you described. Dannon Baker
Followup- which text did show up?Dannon Baker
You don't want to direct JES to a specific FILE. The input should be a DIRECTORY, like C:\Documents and Settings\Mark Guzdial\My Documents\mediasources\pics. Mark Guzdial

I'm using the code Prof Guzdial posted above, yet every time I run it, and then open the sample page, I get the text indicating where the page is, but no sample pictures show up! What am I doing wrong!
Bryan W. Grant
Same as above, are there any pictures in the output directory? Dannon Baker
I think Dannon means the input directory – the one you give as input. Mark Guzdial

Yes there are, and I have them named with say for example "hat.jpg"

Bryan W. Grant
Delving deeper into troubleshooting... Opening up the .html file (in notepad or JES or something), is there any of the text at all referring to the images, or is it as if the program did not see any? Does it appear that your program is 1) not finding images, or 2) just not displaying the findings properly?Dannon Baker



how are we supposed to make our anchor in the link half-size of the image, but our picture is 1/4 of that. And, specifically, what is an anchor?
The thumbnail image should be 1/4 of the size of the image, not 1/2. An anchor is the "a"
<a href="something.html"> text and/or image </a>
tag. It is used to define a link, the "href" part of that being the destination for the link. Dannon Baker
Actually 1/8 of the size of the image, 1/4 of each side. Houman Khalili
Perhaps we should update the instructions, because 1/4 dimension on each side is 1/16 of the total area. Not 1/8. Dannon Baker
hehe, oops! Houman Khalili

i'm having a problem, but i think i'm so close! all the images on my index page show up, but the FILE NAME is the link to the picture and not the actual thumbnail. could someone tell me what's wrong with my code? thanks!
Marggee Godbee

<html><pre>
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>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      samples=samples+'<p>Filename: <a href="'+file+'">'+file+'</a>\n'
      pic = makePicture(directory + "//"+file)
      samples=samples+'<image src="'+file+'" height="100" />\n'
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"
  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>"


def createThumbnail():
  pic = makePicture(file)
  thumb = makeEmptyPicture
  sourceX = 1
  for xOffset in range((1,getWidth)/2):
    sourceY = 1
    for yOffset in range((1, getHeight(pic))/2):
      color = getColor(getPixels(pic,sourceX,sourceY))
      setColor(getPixel(thumb,xOffset,yOffset), color)
      sourceY = sourceY + 1
    sourceX = sourceX + 1 
  writePictureTo("qrt_"+file)
  return()
</pre></html>

The reason your formatting is off is that you were not using the <code> </code> tags to enclose your code. I fixed the above code, please use :)
As far as the filename being the link, and not the image. The link is the
 <a href="link target"> PUT LINKED OBJECT HERE </a> 

So... You would need to modify your code to close the <a> tags around your image. Dannon Baker
Right – so in Dannon's example, the PUT LINKED OBJECT HERE needs to have your <img...> tag for the thumbnailed image. Mark Guzdial

Scaling at different factors


You might want to recall this code from Sp2004 Midterm 1 Review which allows you to scale at any factor, e.g., "0.25"

def copyBarbsFaceScaled(factor):
  # Set up the source and target pictures
  barbf=getMediaPath("barbara.jpg")
  barb = makePicture(barbf)
  canvasf = getMediaPath("7inX95in.jpg")
  canvas = makePicture(canvasf)
  # Now, do the actual copying
  sourceX = 45
  for targetX in range(100,100+int((200-45)*factor)):
    sourceY = 25
    for targetY in range(100,100+int((200-25)*factor)):
      print "Copying from ",sourceX,sourceY," to ",targetX,targetY
      color = getColor(getPixel(barb,int(sourceX),int(sourceY)))
      setColor(getPixel(canvas,targetX,targetY), color)
      sourceY = sourceY + (1.0/factor)
    sourceX = sourceX + (1.0/factor)
  show(barb)
  show(canvas)
  return canvas

Mark Guzdial



import os

def linksPage(directory):
  samplesfile=open(directory+"//samples.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Samples from "+directory))
  samples="<h1>Samples from "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      createThumbnail(file)
      samples=samples+'<p>Filename: <a href="'+file+'">'+file+'</a>\n'
      pic=makePicture(directory + "//"+file)
      samples=samples+'<image src="'+"qtr_"+file+'"/></p>\n'
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"
  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>"

def createThumbnail():
  pic = makePicture(file)
  thumb = makeEmptyPicture
  sourceX = 1
  for xOffset in range((1,getWidth)/2):
    sourceY = 1
    for yOffset in range((1, getHeight(pic))/2):
      color = getColor(getPixels(pic,sourceX,sourceY))
      setColor(getPixel(thumb,xOffset,yOffset), color)
      sourceY = sourceY + 1
    sourceX = sourceX + 1 
  writePictureTo("qrt_"+file)
  return()


I keep getting the error message:
I tried to read a file, and couldn't.  Are you sure that file exists? If it does exist, did you specify the correct directory/folder?
 in file C:\JES\hw4.py, on line 4, in function linksPage
IOError: File not found - C:\Documents and Setting\Karin\Desktop\HW 4\samples.html (The system cannot find the path specified)
Please check line 4 of C:\JES\hw4.py

The thing is, this is the EXACT code I used yesterday and I used the SAME address to set my directories to...directory=r"C:\Documents and Setting\Karin\Desktop\HW 4"...I made a folder and put it on my desktop and called it "HW 4", then I looked up it's full address in "search" under the start menu, just to make sure it was the right directory...and now I'm getting this error message and no website and I still don't understand what what wrong with my code from yesterday. I looked at my html (view source) and it says...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition//EN" "http://wwww.w3.org/TR/html4/loose.dtd"><html><head><title>Samples from C:\Documents and Settings\Karin\Desktop\HW 4</title></head><body><h1>Samples from C:\Documents and Settings\Karin\Desktop\HW 4 </h1>
<p>Filename: Grad2.jpg<image src="qtr_Grad2.jpg"/></p>
<p>Filename: JLKJ.jpg<image src="qtr_JLKJ.jpg"/></p>
<p>Filename: LindsAndKarin.jpg<image src="qtr_LindsAndKarin.jpg"/></p>
<p>Filename: TheGuys.jpg<image src="qtr_TheGuys.jpg"/></p>
</body></html>

So what in the world am I doing wrong? Karin Bowman
Ok, so I got that problem fixed (spelling) sorry...but now I have a new dilemma...
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.
 in file C:\JES\hw4.py, on line 10, in function linksPage
TypeError: createThumbnail() too many arguments; expected 0 got 1
Please check line 10 of C:\JES\hw4.py

Everything about my actual code is the same...I'm not even sure what this means. Please help! Karin Bowman
Your createThumbNail function takes no inputs, but you're giving it one. Try accepting "file" as input. Mark Guzdial

I am having trouble with calling the thumbnail function. It has to accept a file as input right? The line I'm having trouble with is makePicture(file). It says I'm trying to pass an argument to a function. What am I doing wrong here?
 for file in os.listdir(directory):
    if file.endswith (".jpg"):
      sample = samples + '<p>filename: <a href = "'+file+'"> '+file+' </a>\n'
      createThumbnail(file)
      samples=samples+'&ltp>Filename: &lta href="'+file+'">'+file+'&lt/a>\n'
      samples = samples+'&lta href="'+file+'">'
      samples=samples+'&ltimage src="'"qtr_"+file+'" />&lt/a>&lt/p>\n'
  samplesfile. write(body(samples))
  samplesfile. close()


def createThumbnail(file):
  pic = makePicture(file) 
  thumb = makeEmptyPicture
  sourceX = 1
  for targetX in range((1,getWidth(pic))*0.25):
    sourceY = 1
    for targetY in range((1, getHeight(pic))*0.25):
      color = getColor(getPixel(pic,int(sourceX),int(sourceY)))
      setColor(getPixel(thumb,targetX,targetY), color)
      sourceY = sourceY + (1.0/0.25)
    sourceX = sourceX + (1.0/0.25)
  writePictureTo("qtr_"+file)
  return 

Heather Symon
You're calling createThumbnail with one of the filenames, but not with a complete PATH to the file. Try directory + "//"+file. Mark Guzdial
Where would she put that extension? In the "file" within "createthumbnail" or with a different "file"?



How are the resultant images supposed to look? Are they supposed to all be 1/4 of the original image? Or do they incrementally become smaller?
each image should be 1/4 the height of the original and 1/4 of the width of the original. Mark Guzdial

Are we supposed to make the thumbnail pictures one quarter of the original size, or one quarter of the original height AND one quarter of the original width? Because if we do the first, then we'd have to divide certain things by 2...but if we do the latter, then we have to divide that stuff by 4 and the total area would be 1/16, not 1/8, but correct me if I'm wrong...could someone clarify the directions? Thanks.
You're correct. The interpretation should be 1/16 of the original area. Divide by four.Dannon Baker

Sounds right – sorry! Mark Guzdial


In this line:
for xOffset in range(1,(getWidth(pic))/4):

I get this error:
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.
 in file C:\JES\Sources\JESDebugger.py, on line 142, in function run
 in file C:\JES-SA1a\jython\Lib\bdb.py, on line 351, in function run
 in file <input>, on line 1, in function ?
 in file C:\JES\hw4.py, on line 10, in function linksPage
 in file C:\JES\hw4.py, on line 31, in function createThumbnail
TypeError: __div__ nor __rdiv__ defined for these operands
Please check line 31 of C:\JES\hw4.py


What does this mean? Karin Bowman
Answered in lecture (nice to meet you, Karin!). Make sure that pic is a picture. If it's a file, getWidth won't work, and you can't divide nothing by 4. Mark Guzdial

I tried executing the file after using setMediaPath with the following command:

>>> linksPage(r'C:\Documents and Settings\Anuradha Koppikar.BUZZ\My Documents\Previous Courses\Bio\Pics\ppl\')

but it gives an error message - "Your code contains at least one syntax error, meaning it is not legal jython."
JES loads the code, so the syntax error can't be in the code, can it?
Could you help?
Thanks,
Anu Koppikar
You don't need to set the media path. If you're using getMediaPath, you're doing it wrong. It's weird to get that error after loading the code, but I guess it's possible. You could try email me the code to look at. Mark Guzdial

I was actually wondering how we can make a page our main page so that when we type www.prism.gatech/~gtxxxx it will go straight to the lab4 page.
Two main options, you can either create an index.html and have that use a javascript redirect (google it, pretty easy) to your lab4.html; OR (and I'd recommend this one), just rename lab4.html to index.html, and it'll work great. Dannon Baker




import os

def linksPage(directory):
  samplesfile=open(directory+"//samples.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Samples from "+directory))
  samples="<h1>Samples from "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      createThumbnail(file)
      samples=samples+'<p>Filename: <a href="'+file+'">'+file+'</a>\n'
      pic=makePicture(directory + "//"+file)
      samples=samples+'<image src="'+"qtr_"+file+'"/></p>\n'
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"
  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>"

def createThumbnail(file):
  pic = makePicture(file)
  thumbnail = makeEmptyPicture
  sourceX = 1
  for x in range((1,getWidth)/2):
    sourceY = 1
    for y in range((1, getHeight(pic))/2):
      color = getColor(getPixels(pic,sourceX,sourceY))
      setColor(getPixel(thumbnail,x,y), color)
      sourceY = sourceY + 1
    sourceX = sourceX + 1 
  writePictureTo("qtr_"+file)
  return pic


When I try to run linksPage in the command area, it says that there is at least one syntax error and it isn't legal jython. I typed in linksPage(r'C:\My Documents\David's Pictures'). What am I doing wrong, or is my code so completely off?

David, same error that came up earlier. (BTW, you're not getting syntax error. You're getting 'There is no file at...' I ran your code.) You're calling createThumbnail with file, but there is no file with that name in the JES directory, which is where makePicture looks if it isn't given a path. You need a complete path to the file. Use directory+"//"+file. Mark Guzdial

FYI: I won't be on-line much tonight or tomorrow. My newly-9-year-old daughter is having a sleepover birthday party tonight. Mark Guzdial


Is it alright if the filename of the image AND the thumbnail are both links to the actual image??? Or does it have to be just the thumbnail?
Marggee Godbee
It's supposed to be just the thumbnail. I doubt that you'd lose points for doing both, but it's REALLY easy to fix. Mark Guzdial


import os

def linksPage(directory):
  samplesfile=open(directory+"//Index.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Index from "+directory))
  samples="<h1>Index from "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      createThumbnail(file)
      samples=samples+'<p>Filename: <ahref="'+file+'">'+file+'</a>\n'
      pic=makePicture(directory + "//"+file)
      samples=samples+'<image src="'+"qtr_"+file+'"/></p>\n'
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"
  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>"

def createThumbnail(file):
  picture = makePicture(file)
  thumb = makeEmptyPicture
  sourceX = 1
  for x in range((1,getWidth)/4):
    sourceY = 1
    for y in range((1, getHeight(picture))/4):
      color = getColor(getPixels(picture,sourceX,sourceY))
      setColor(getPixel(thumb,x,y), color)
      sourceY = sourceY + 1
    sourceX = sourceX + 1 
  writePictureTo("qtr_"+file)
  return picture


Above is my code. It works up to the point in the makeThumbnail function–the first for loop. What's the problem?

Look at the code in that line. You have range((1,getWidth)/4) Maybe you meant something like range(1,getWidth(picture)/4)? Mark Guzdial


OVERALL HINT!

Note that the same bugs are starting to repeat themselves. Read this page! I'll bet that everything that you need is here by now. Mark Guzdial|


import os

def linksPage(directory):
  samplesfile=open(directory+"//samples.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Samples from "+directory))
  samples="<h1>Samples from "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      createThumbnail(file)
      samples=samples+'<p>Filename: <a href="'+file+'">'+file+'</a>\n'
      pic=makePicture(directory + "//"+file)
      samples=samples+'<image src="'+"qtr_"+file+'"/></p>\n'
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"
  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>"

def createThumbnail(directory+"//"+file):
  pic = makePicture(directory+"//"+file)
  thumbnail = makeEmptyPicture
  sourceX = 1
  for x in range((1,getWidth)/4):
    sourceY = 1
    for y in range((1, getHeight(pic))/4):
      color = getColor(getPixels(pic,sourceX,sourceY))
      setColor(getPixel(thumbnail,x,y), color)
      sourceY = sourceY + 1
    sourceX = sourceX + 1 
  writePictureTo("qtr_"+directory+"//"+file)
  return pic


I changed what you asked, at least, Ithink. It still says that I have a syntax error. I don't know what else to do.
Look at the error JES spits out. It tells you that the error is in the def createThumbnail(directory+"//"+file): line.
This line is the header to a function, right? You want that function to take in one value, a string.
JES doesn't understand what you're trying to take in there, the way you have the function declared. You want to call the function, with that as the parameter, yes, but as the actual function definition (the def.... line), you just need to take in a single string (just one word). Something like def createThumbnail(filepath) seems appropriate
Change the input parameter on that definition, and call the function from your code with the string you had there (directory+"//"+file).

IM me (SirYoplait) for more explanation. Dannon Baker

WHAT ARE WE SUPPOSE TO DEFINE OUR PIC=MAKEPICTURE(directory+"//"+file)???? UNDER DEF CREATETHUMBNAIL???
CAN ANYONE TELL ME WHAT I AM DOING WRONG??
If you are passing as input directory+"//"+file, you shouldn't do it in createThumbnail TOO. In fact, it won't work in the thumbnail function because the directory variable won't exist in that context (unless you pass it in as input). Mark Guzdial
import os

def linksPage(directory):
  samplesfile=open(directory+"//Index.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Indexes from "+directory))
  samples="<h1>Indexes from "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      createThumbnail(directory + "//"+file)
      samples=samples+'<p>Filename: <a href="'+file+'">'+file+'</a>\n'
      pic=makePicture(directory + "//"+file)
      samples=samples+'<image src="'+"qtr_"+file+'"/></p>\n'
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"
  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>"

def createThumbnail(filepath):
  pic=makePicture(directory + "//"+file)
  thumbnail=makeEmptyPicture
  sourceX = 1
  for x in range((1,int(getWidth(pic)/4)):
    sourceY = 1
    for y in range((1, int(getHeight(pic))/4)):
      color = getColor(getPixels(pic,int(sourceX),int(sourceY))
      setColor(getPixel(thumbnail,x,y), color)
      sourceY = sourceY + (1.0/0.25)
    sourceX = sourceX + (1.0/0.25) 
  writePictureTo("qtr_"+directory+"//"+file)
  return pic
_
We've got the links to work, but we get the little red Xs mocking us instead of the thumbnail images. This is the code we oh-so-painstakingly put together:

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\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      samples=samples+'<p>File: <a href="'+file+'"><img src = "+file+"></a>\n'
      pic = makePicture(directory + "//"+file)
      createThumbnail(file)
  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>"

def createThumbnail(file):
  pic = makePicture(file) 
  thumb = makeEmptyPicture(getWidth(pic)/4,getHeight(pic)/4)
  sourceX = 1
  for targetX in range(1,(int)(getWidth(pic)*0.25)):
    sourceY = 1
    for targetY in range(1,(int) (getHeight(pic)*0.25)):
      color = getColor(getPixel(pic,int(sourceX),int(sourceY)))
      setColor(getPixel(thumb,targetX,targetY), color)
      sourceY = sourceY + (1.0/0.25)
    sourceX = sourceX + (1.0/0.25)
  writePictureTo(thumb,"qtr_"+file)
  return 


What have we done wrong, for JES to treat us so poorly despite all our best efforts? Can anyone help us?

~Erin & Anu
It looks like you've passed in JUST the filename to your thumbnail function. You would need the directory structure as well, in order to open the original file, and to write write the qtr_filename out to the proper location, right? Perhaps looking into having your thumbnail function take in two parameters, directory AND filename, and then use the both of them to get the right paths/names? IM me for more explanation, these things are easier in a conversation. Dannon Baker
Or pass in directory+"//"+file as input to your thumbnail function. Mark Guzdial

So are the thumbnails themselves the link to the bigger pictures, or are we supposed to have the file names next to the thumbnails that link to the bigger pictures? (Sorry if this has been clarified before...)
The thumbnails (qtr_...) are the links to the bigger pictures. Mark Guzdial


I have similar code to the lectures...however, my pictures are not showing at all on the webpage...only the title! Do you know what's wrong?
Look at the HTML you're generating – your file for the image tag is "+file+". That's not a valid image. Mark Guzdial

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>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      samples=samples+'<p>File: <a href="'+file+'"><img src="+file+"></a>/n'
      pic=makePicture(directory+"//"+file)
      thumbnail(directory+"//"+file)
  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>"

def thumbnail(file):
  pic = makePicture(file)
  canvas = makeEmptyPicture(getWidth(pic)/4, getHeight(pic)/4)
  sourceX = 1
  for targetX in range(1,getWidth(pic)*0.25):
    sourceY = 1
    for targetY in range(1,getHeight(pic)*0.25):
      color = getColor(getPixel(pic,sourceX,sourceY))
      setColor(getPixel(canvas, targetX, targetY), color)
      sourceY = sourceY + 1.0/0.25
    sourceX = sourceX + 1.0/0.25
  writePictureTo(canvas,directory+"//"+"qtr_"+file)
  return canvas



At the beginning of my creation of a quarterfile, I am trying to tell jes to pull out the origianl picture. I believe I am suppose to use the ENTIRE path to it...which would be like this:

def quarterFile(file):
  pic = makePicture(directory + "//"+file)


but i guess not because I am getting an error saying:
A local or global name could not be found. You need to define the function or variable before you try to use it in any way.
Whats the problem??
Amelia Cipolla
Amelia, directory does not exist in quarterFile. My suggestion is to pass in as input the complete path (directory+"//"+file), then just open that input path in quarterFile. Mark Guzdial
Prof Guzdial, what does what you just said really mean? I'm a little confused about how to fix the problem but I'm having the same issues Amelia had...


I keep getting an error message saying that my pic in the makeThumbnail(file) is not an image. How can I make sure that the images in my folder are images? They all end with .jpg What so I do?
Deborah Stepehens

makePicture(filename): There is no file at .\DCP_0360.JPG
An error occurred attempting to pass an argument to a function.
in file C:\Documents and Settings\SabrinaH\Desktop\Sabrina\JES\hw4.py, on line 27, in function linksPage
in file C:\Documents and Settings\SabrinaH\Desktop\Sabrina\JES\hw4.py, on line 32, in function qtrSize
in file C:\Documents and Settings\SabrinaH\Desktop\Sabrina\JES\JES\Sources\media.py, on line 735, in function makePicture
ValueError:
Please check line 32 of C:\Documents and Settings\SabrinaH\Desktop\Sabrina\JES\hw4.py

I keep getting this message on this line
qtrf = makePicture(directory + "//"+file)
What should I do? it seemes like it doens't recognize my jpg as a file?
Sabrina Hassaanali
i'm having the same problem, only after
def createThumbnail(file):
pic = makePicture(file)
Arseni Zaitsev

i just want to be clear on what we have to do. We are just suppose to make a program that creates an html file that contains links to all the images in a folder and the links are quater sized versions that link to the original images. So we are actually not required to have text like the filenames and just have the scaled down images link directly to the originals?
You must also generate scaled down images, make them appear in the HTML page, and clicking on the scaled down images should take you to the original images. Mark Guzdial

I am having the same problem that Meredith Harman mentioned about 1/4 way down this page. I've copied the code from breakouts, but it's saying "A local or global name could not be found" when I pass hw4fldr (where I put the pictures I want to use) as input to makeSamplePage. In Expert Mode, it's saying: NameError: hw4fldr. What's my problem? And how do I open the generated HTML page?
Jonathan Laing
Where is hw4fldr defined? Where do you set it? To open the generated HTML page, double click on it. Mark Guzdial


import os

def makeSamplePage(directory):
  samplesfile=open(directory+"//samples.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Samples from "+directory))
  # Now, let's make up the string that will be the body.
  samples="<h1>Samples from "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      samples=samples+'<p>Filename:<a href="'+file+'">'+file+"</a>\n"
      pic=makePicture(directory+"//"+file)
      samples=samples+'<a href="'+file+'"><img src="'+"qtr_"+file+'"/></a></p>\n'
      samples=samples+"Height:"+str(getHeight(pic))+"Width:"+str(getWidth(pic))+"</p>\n"
  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>"

def quarter(file):
  qtrf = makePicture(file)
  canvas = makeEmptyPicture(getWidth(qtrf)/4, getHeight(qtrf)/4)
  sourceX = 1
  for targetX in range(1,getWidth(qtrf)/4):
    sourceY = 1
    for targetY in range(1,getHeight(qtrf)/4):
      color = getColor(getPixel(qtrf, sourceX, sourceY))
      setColor(getPixel(canvas, targetX, targetY), color)
      sourceY = sourceY + 4
    sourceX = sourceX + 4
  writePictureTo(directory+"//qtr_"+file)
  return canvas

Paula Verden
everything is working but i have red x's for pictures. The pictures lead to the large scale picture but the quarter scales are not showing please help.
Paula, you never call the quarter function, so no thumbnails are ever created. Mark Guzdial

import os

def linksPage(directory):
  linksfile=open(directory+"//index.html","wt")
  linksfile.write(doctype())
  linksfile.write(title("Index of "+directory))
  # Now, let's make up the string that will be the body.
  links="<h1>Index of "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg") or file.endswith(".JPG"):
      createThumbnail(directory+"//"+file)
      links=links+"<p>Filename: "+file
      links=links+'<image src="'+file+'" height="100" /></p>\n'
  linksfile.write(body(links))
  linksfile.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 createThumbnail(file):
  qtrf = makePicture(file)
  canvas = makeEmptyPicture(getWidth(qtrf)/4, getHeight(qtrf)/4)
  sourceX = 1
  for targetX in range(1,getWidth(qtrf)/4):
    sourceY = 1
    for targetY in range(1,getHeight(qtrf)/4):
      color = getColor(getPixel(qtrf, sourceX, sourceY))
      setColor(getPixel(canvas, targetX, targetY), color)
      sourceY = sourceY + 1
    sourceX = sourceX + 1
  writePictureTo(directory+"//qtr_"+file)
  return canvas


Here is the code I'm working with, it's pretty much the same as all of the above, but I keep getting an error on the second to last line of the thumbnail function it tells me:

A local or global name could not be found. You need to define the function or variable before you try to use it in any way.
Please check line 37 of C:\Documents and Settings\Bryan Grant\My Documents\JES\Homeworks\hw4.py why won't it write my qtr pictures?
Because directory doesn't exist in your createThumbnail function. Mark Guzdial

Ok when viewing the source on my webpage it seems that my program works the only problem is getting the quarter picture to write to the directory please look at my code above and tell me what is wrong with my quarter program if that is the problem.Thank You
Paula Verden

How would i fix if JES spits out "ARRAY of INDEX error"????
Means that your quarter-size image function is going beyond the end of the picture. Mark Guzdial

I got my program to work..but i don't understand the part about anchoring...so, is if we click on our original .jpg picture..is it suppose to show the 1/16 picture??? how do we go about doing that??? because what my program does is display originally but underneath all my original pics...is "qrt_.jpg" pics....?? but my pictures don't show....PLEASE HELP!!


If you click the qrt picture (which should be visible with an image tag), you should see the original picture. Mark Guzdial

Jonathan Laing Checking back in. Prof. Guzdial, to answer your question, I have hw4fldr saved in the JES directory.
When you say to double click on the HTML page, where should I go to click on it (where is it being generated/saved)? Thanks for your help on a Saturday!
In the directory that you passed in as input. Mark Guzdial

Jonathan, what VALUE do you set to the hw4fldr VARIABLE? That's what JES is complaining about. Mark Guzdial


I can't seem to see my qrt pict...do you know how to find our qrt pict...writePictureTo(??????) so, our qrt image...if we click on it shows our original pics?? then right next to our pic should be labeled "qrt...jpg" or "+file+.jpg"
Your filename should be named something like "qrt+fred.jpg". If you don't put it in a specific directory, it will end up in the JES directory. Mark Guzdial

CAN anyone see what my problem is....i can't seem to see my thumb pics...what should i do...???
My suggestion is to stop playing with the code and just read it, away from the computer. It looks like you're just throwing quote marks in now. Here's what you have '<image src="'"qtr_"+file+'"/>' That won't even run because it's not valid Python. If you use single quotes to delimit the string, you can put double quotes in the middle for HTML. But then you need + to put strings together. You want something like '<image src="'+"qtr_"+file+'"/>' Mark Guzdial

import os

def linksPage(directory):
  samplesfile=open(directory+"//Index.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Indexes.html "+directory))
  # Now, let's make up the string that will be the body.
  samples="<h1>Indexes from "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      createThumbnail(directory,file)
      samples=samples+'<p>Filename:<a href="'+file+'">'"qtr_"+file+'</a>\n'
      samples=samples+'<a href="'+file+'"><image src="'"qtr_"+file+'"/></a></p>\n'
      pic=makePicture(directory + "//"+file)
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"
  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>"

def createThumbnail(directory,file):
  pic=makePicture(directory+"//"+file)
  thumb=makeEmptyPicture(getWidth(pic)/4,getHeight(pic)/4)
  sourceX = 1
  for targetX in range(1,int(getWidth(pic)*0.25)):
    sourceY = 1
    for targetY in range(1,int(getHeight(pic)*0.25)):
      color = getColor(getPixel(pic,int(sourceX),int(sourceY)))
      setColor(getPixel(thumb,targetX,targetY), color)
      sourceY = sourceY + (1.0/0.25)
    sourceX = sourceX + (1.0/0.25)
  writePictureTo(pic,"qrt_"+file)
  return thumb
 



Prof Guzdial, I thought I understood what the solution to the question I asked in lecture was but apparently I do not and I can't figure out what is going wrong here...I'm not too sure where directory+"//"file" is supposed to go and I've tried it in all sorts of places, including def createThumbnail(directory+"//"+file), but all it tells me is that there's an error with the name "directory"...I set directory equal to directory=r"C:\Documents and Settings\Karin\Desktop\HW 4" which I know works. I tried looking at what your suggestions were with all the other people with this problem, but nothing's clicking to me...I've been working on this for over an hour dealing with the same problem...anyone's help is appreciated! Below is my code.
Karin Bowman
import os

def linksPage(directory):
  samplesfile=open(directory+"//samples.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Samples from "+directory))
  samples="<h1>Samples from "+directory+" </h1>\n"
  for file in os.listdir(directory):
    filepath=directory+"//"+file
    if file.endswith(".jpg"): 
      createThumbnail(filepath)
      samples=samples+'<p>Filename: <a href="'+file+'">'+file+'</a>\n'
      pic=makePicture(directory + "//"+file)
      samples=samples+'<image src="'+"qtr_"+file+'"/></p>\n'
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"
  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>"

def createThumbnail(filepath):
  filepath=directory+"//"+file
  pic = makePicture(file)
  thumb = makeEmptyPicture(getHeight(pic)/4, getWidth(pic)/4)
  sourceX = 1
  for targetX in range(1, (getWidth(pic))/4):
    sourceY = 1
    for targetY in range(1, (getHeight(pic))/4):
      color = getColor(getPixels(pic, sourceX, sourceY))
      setColor(getPixel(thumb, targetX, targetY), color)
      sourceY = sourceY + 1
    sourceX = sourceX + 1 
  writePictureTo(file)
  return()

Karin, you need to end up writing the thumbnail picture to directory + "//qtr_" + file. You could do it in your thumbnail function, but you don't have the variable directory there. You have two options: (1) Pass in TWO inputs like this def createThumbnail(file,directory): and do it in your thumbnail program or (2) pass in only the filepath for reading the file def createThumbnail(filepath):, but then RETURN the scaled down picture, and writePictureTo in the linksPage function. Your current code won't work – you pass in filePath as the input, but the very first thing you do is to SET filePath, thus losing whatever input you had. (And since you're referring to directory and file, which don't exist in the thumbnail function, it won't work at all.) def createThumbnail(directory+"//"+file): will not work because that's not even correct Python – inside the parentheses can only go names of variables that you want to take input in. Mark Guzdial

if we have a picture thats say 640x480 do we have to get it to 160x120 or is 161x121 ok?
Stephen Rudolph

I don't care about one-off. Mark Guzdial


I'm sure you're getting sick and tired of seeing this question, but I really don't understand why it isn't working, so I felt the need to ask. I have tried many different ways to get the thumbnails to show up, but they still won't show. This is what I currently have, because it doesn't shout any errors at me and everything else looks nice, except that there are no thumbnails. Just the red x's.




I'm sure you're getting sick and tired of seeing this question, but I really don't understand why it isn't working, so I felt the need to ask. I have tried many different ways to get the thumbnails to show up, but they still won't show. This is what I currently have, because it doesn't shout any errors at me and everything else looks nice, except that there are no thumbnails. Just the red x's.

import os

def linksPage(directory):
  samplesfile=open(directory+"//Index2.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Index.html "+directory))
  # Body String...
  samples="<h1>Index for "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      createThumbnail(directory, file)
      samples=samples+'<p>Filename:<a href="'+file+'">'+"qtr_"+file+'</a>\n'
      samples=samples+'<a href="'+file+'"><image src="'+"qtr_"+file+'"/></a></p>\n'
      pic=makePicture(directory + "//"+file)
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"
  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>"

def createThumbnail(directory, file):
  pic=makePicture(directory+"//"+file)
  thumb=makeEmptyPicture((getWidth(pic))/4,(getHeight(pic))/4)
  sourceX = 1
  for targetX in range(1,int(getWidth(pic)/4)):
    sourceY = 1
    for targetY in range(1,int(getHeight(pic)/4)):
      color = getColor(getPixel(pic,(sourceX),(sourceY)))
      setColor(getPixel(thumb,targetX,targetY), color)
      sourceY = sourceY + 1
    sourceX = sourceX + 1 
  writePictureTo(pic,"qrt_"+file)
  return thumb


Jennifer Garrett
Look at where you're actually writing the picture out to. writePictureOutTo requires a full filepath. you're only giving it qrt_somefilename.jpg. Try appending your directory string to the beginning of that second parameter, like directory + "//" + "qrt_" + file. Dannon Baker

Right – like Dannon said. You're writing all the qrt_ files into your JES directory. Check and you'll find them there. (Notice that you're doing a makePicture in the linksPage function that you're also doing in createThumbnail. You don't need the one in the linksPage, unless you really care about putting out the height and the width. You don't have to do that for the HW. Mark Guzdial









I coped Jennifer's code to help me with mine because ours was very similar and then I made the changes that both Dannon and Mark suggested, but when I opened the html page question marks showed up as the thumbnails instead of the pictures....why? The thumbnails worked (they took me to the original size of the pictures) but the thumbnails didn't have pictures there...also, is it okay if I have a link and a thumbnail that takes you to the original sized picture? Kyla LeCroy

I don't quite grok your question, Kyla. If question marks (typical for Mac Safari browser) or red X's (Internet Explorer) show up, it means that the image link is broken. Either you didn't write out the qtr_ files to the right place, or your <image> tags don't point at the right file names. The link should go from the thumbnail to the original image. You should be able to fix it so that the filename isn't a link, too. Mark Guzdial


import os

def linksPage(directory):
  samplesfile=open(directory+"//samples.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Samples from "+directory))
  samples="<h1>Samples from "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      thumbnail(samples)
      samples=samples+'<p>Filename: <ahref="'+file+'">'+file+'</a>\n'
      pic=makePicture(directory + "//"+file)
      samples=samples+'<image src="'+"qtr_"+file+'"/></p>\n'
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"
  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>"

def thumbnail():
  qtrf = makePicture(file)
  canvas = makeEmptyPicture(getWidth(qtrf)/4, getHeight(qtrf)/4)
  sourceX = 1
  for targetX in range(1,getWidth(qtrf)/4):
    sourceY = 1
    for targetY in range(1,getHeight(qtrf)/4):
      color = getColor(getPixel(qtrf, sourceX, sourceY))
      setColor(getPixel(canvas, targetX, targetY), color)
      sourceY = sourceY + 4
    sourceX = sourceX + 4
  writePictureTo("qtr_"+file)
  return canvas

I keep getting the error: "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.
Please check line 10 of C:\JES\hw4.py" so i know the problem occurs when the thumbnail program is called, but i can't figure out how to fix it. Meghan Quinlan
Meghan, you're calling the thumbnail function WITH an input. But you've DEFined your thumbnail function as NOT taking any input. Maybe you meant to accept an input of "file"?Mark Guzdial

Kyla, post your code and maybe we will be better able to help!

Mark, We're having a similar problem that Karin and Jennifer and Kyla are having. We've tried to make the changes that you and Dannon suggested, but our code is still giving an error with line:samplesfile=open(directory+"//index.html","wt"). It reads: I tried to read a file, and couldn't. Are you sure that file exists? If it does exist, did you specify the correct directory/folder? Please check line 4 of C:\JES\sampleHW4.py. Unlike Kayla though, when we open our index.html file in our folder the page opens, looks, and works exactly like it should! Can you please look through our code and point us in the right direction? Is it perhaps something we have yet to do with the actual computer maybe? We're lost!
I just tried to run your code, and I don't get any error on that line at all! The error comes later. You're calling createThumbnail(file), but you've defined def createThumbnail(directory, file):. Maybe you meant to pass in the directory, too? Mark Guzdial
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>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      createThumbnail(file)
      samples=samples+'<p>Filename: <ahref="'+file+'">'+file+'</a>\n'
      samples=samples+'<image src="'+"qtr_"+file+'"/></p>\n'
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"
  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>"

def createThumbnail(directory, file):

  pic = makePicture(directory+"//"+file)
  thumb = makeEmptyPicture(getHeight(pic)/2, getWidth(pic)/2)
  sourceX = 1
  for xOffset in range((1,getWidth)/2):
    sourceY = 1
    for yOffset in range((1, getHeight(pic))/2):
      color = getColor(getPixels(pic,sourceX,sourceY))
      setColor(getPixel(thumb,xOffset,yOffset), color)
      sourceY = sourceY + 1
    sourceX = sourceX + 1 
  writePictureTo(directory+"//qrt_"+file)
  return()

Christina Sedor Jennifer Sigg Katy Ficke



Okay, so here's my code so far. I finally got the program to sort of work...in other words it puts thumbnails on the webpage, along with the appropriate header, BUT the thumbnails aren't being sized down, the text link next to the picture read "qtr_filename.jpg" (according to the filename of the picture, and the supposedly qtr thumbnail files are actually larger than the originals, HELP! Below is my code: Kyla LeCroy

import os

def linksPage(directory):
  samplesfile=open(directory+"//Index2.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Index.html "+directory))
  # Body String...
  samples="<h1>Index for "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      createThumbnail(directory, file)
      samples=samples+'<p>Filename:<a href="'+file+'">'+"qtr_"+file+'</a>\n'
      samples=samples+'<a href="'+file+'"><image src="'+"qtr_" + file+'"/></a></p>\n'
      pic=makePicture(directory + "//"+file)
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"
  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>"

def createThumbnail(directory, file):
  pic=makePicture(directory+"//"+file)
  thumbnail=makeEmptyPicture((getWidth(pic))/4,(getHeight(pic))/4)
  sourceX = 1
  for targetX in range(1,int(getWidth(pic)/4)):
    sourceY = 1
    for targetY in range(1,int(getHeight(pic)/4)):
      color = getColor(getPixel(pic,(sourceX),(sourceY)))
      setColor(getPixel(thumb,targetX,targetY), color)
      sourceY = sourceY + 1
    sourceX = sourceX + 1
  writePictureTo(pic,directory + "//" + "qtr_" + file)
  return thumbnail


I'll keep trying to fix it, but all the help that is possible is much needed! :)

Kyla LeCroy okay, i have no idea why the text showed up that way, I did as directed per FAQ!! :(
Kyla, after you run your program, do you delete the qtr_ pictures that your program created? If not, the NEXT time you run it, you are trying to index those as well! Mark Guzdial

The homework says to "Use makeEmptyPicture to create a blank picture of the right size." How do we know what the 'right size' is? Should I just make a 640x680?
If you're scaling down each side by 4, how about getHeight(pic)/4 and getWidth(pic)/4? Mark Guzdial

Mark, are you refering to my code? I thought I did that.... thanks for all your help! Kyla LeCroy

Oh, and I did delete those....but it's still doing all those problems i mentioned before: the thumbnail is larger than the original, the text next to the pic is still saying "qtr+file"..blah blah.... Kyla LeCroy

Kyla LeCroy, I just got yours working, but there are a bunch of things I had to change to make it work. Some tips:
  • Don't you want to increment sourceX and sourceY by 4, not 1?
  • Why are you writing the pic out to the file when you just went to all that effort to create the thumb picture?
  • You are currently putting "qtr_" in front of the filename in the index file. That's why you're seeing "qtr_" – you're putting it there! Look again at line 12.
But it's ALMOST there, Kyla! Mark Guzdial


Christina, Jennifer, and Katy–use Kyla's code as a model. Your thumbnail function won't run at all – your range functions aren't valid Python. (Check the parentheses and where you don't have any inputs.) Mark Guzdial


I don't think this question has been asked yet. If it has, I don't see it, and I apologize. My code creates an Index where I see the quarter images, and I see the little boxes with red x's. The quarter images are not clickable, but the red x boxes are. What is the problem with my code? Lindsey Richardson
If you have clickable red X's, then your anchor tags are working (clickable) but your image tags are not (red X's). Mark Guzdial

Kyla LeCroy Thanks Mark for all your help!! You and the TA's have been incredible when it comes to getting help with our assignments, THANK YOU SOO MUCH!! I finally got my code to work, YAY! But, one last thing...on the HTML page it says "filename:" to the left of my thumbnails...that doesn't need to be there, correct?

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>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      thumbnail(directory, file)
      samples=samples+'<p>Filename:<a href="'+file+'">'+"qtr_"+file+'</a>\n'
      samples=samples+'<a href="'+file+'"><image src="'+"qtr_" + file+'"/></a></p>\n'
      pic=makePicture(directory + "//"+file)
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"  
  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>"

def thumbnail(directory,file):
  pic = makePicture(directory+"//"+file)
  thumbnail = makeEmptyPicture(getWidth(pic)/4, getHeight(pic)/4)
  sourceX = 1
  for targetX in range(1,getWidth(pic)/4):
    sourceY = 1
    for targetY in range(1,getHeight(pic)/4):
      color = getColor(getPixel(pic, sourceX, sourceY))
      setColor(getPixel(thumbnail, targetX, targetY), color)
      sourceY = sourceY + 4
    sourceX = sourceX + 4
  writePictureTo(thumbnail, directory+ "//" + "qtr_" +file)
  return thumbnail

It says I have a syntax error, but it doesn't tell me where, and I can't find it. Otherwise, it should work. I think. Also, how do I turn the blocking back on?

I think many photos might not be showing up because they aren't .jpg but instead .jpeg files. I can't get my .jpeg files to show up obviously with my .jpg files. My question is, how do I call up for both of them, i.e. for a line like "if file.endswith(".jpg"):" i want to include .jpeg files, how do I do that? thanks!Melody Pugh
You could also modify the qualifier in your 'if' statement.
Something like this:
     if (file.endswith(".jpg") or file.endswith(".jpeg")):
not necessary to incorporate, but that would do what you're looking for. Dannon Baker

Melody,, why don't you just rename the files?

there are lots of questions above so if this has been asked, my apologies. I know we turn in our HW4.py through JES, but my question is if we have to attach the photos that we used? Or will the TA just use their own pictures? Kyla LeCroy
The function should work on any directory, so we'll be using our own to grade with. Dannon Baker

Two questions...
1) is it okay if the page lists height and width of our pictures? I know the directions don't say so, but is it a bad thing?
Yeah, it's okay. But why not just remove it? Mark Guzdial
2) do all the pictures need to be the same size (as in the same size thumbnails and enlargements), or should they simply be 1/4 their original size period (i.e., if all the pictures have different dimensions, should we make them all have the same?) Thanks, Karin Bowman
Each thumbnail picture should be 1/4 of its original height and width, not all the same size. Mark Guzdial



Is the text beside the thumbnail supposed to say qtr_file or just the filename?
Just the filename, no "qtr_". Dannon Baker


I am getting an array out of bounds error for the "SetColor" line of my thumbnail function, and I have no idea why??
Jonathan Longhitano
I was getting it when testing Kyla's code. I ended up adding 1 to the width and height of my thumbnail, and that cleared it up. Mark Guzdial


def makeThumbnail(directory, file):
  pic = makePicture(directory+"//"+file)
  thumbnail = makeEmptyPicture((getHeight(pic))/4, (getWidth(pic))/4)
  sourceX = 1
  for targetX in range(1,int(getWidth(pic)/4)):
    sourceY = 1
    for targetY in range(1,int(getHeight(pic)/4)):

      color = getColor(getPixel(pic,(sourceX), (sourceY)))
      setColor(getPixel(thumbnail, targetX, targetY), color)
      sourceY = sourceY + 4
    sourceX = sourceX + 4 
  writePictureTo(file)
  return()

Jonathon, I had the same problem until a ta pointed out that getWidth should come before getHeight. Karin Bowman

okay I don't know why that just happened, but the code should look like this
def makeThumbnail(directory, file):
  pic = makePicture(directory+"//"+file)
  thumbnail = makeEmptyPicture((getHeight(pic))/4, (getWidth(pic))/4)
  sourceX = 1
  for targetX in range(1,int(getWidth(pic)/4)):
    sourceY = 1
    for targetY in range(1,int(getHeight(pic)/4)):
      color = getColor(getPixel(pic,(sourceX), (sourceY)))
      setColor(getPixel(thumbnail, targetX, targetY), color)
      sourceY = sourceY + 4
    sourceX = sourceX + 4 
  writePictureTo(file)
  return()


Random question, why does the web address have "wwww" and not "www"? Does this matter?

Also, does the function need to begin and end with "html" and "pre" tags?

What would an example finished code look like?
I'm sorry, but I'm not grokking any of these questions. Try again, please? Mark Guzdial

When I run this program in JES, it gives me an error: There was a spacing error in the program.It might be from a previous line, but I think the error is in line 33 .... line 33 is sourceY=1. Maryam
import os

def linksPage(directory):
  samplesfile=open(directory+"//index2.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Samples from "+directory))
  # Now, we make up the string... the body:
  samples="

Index for "+directory+"

\n" for file in os.listdir(directory): if file.endswith(".jpg"): createThumbnail(directory, file): samples=samples+'

Filename:'+file+'\n' samples=samples+'

\n' pic = makePicture(directory+"//"+file) samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"

\n" samplesfile.write(body(samples)) samplesfile.close() def doctype(): return '' def title(titlestring): return ""+titlestring+"" def body(bodystring): return ""+bodystring+""

def createThumbnail(directory, file):
pic = makePicture(directory+"//"+file)
emptyCanvasGeneration= makeEmptyPicture(getWidth(pic))/4 , (getHeight(pic))/4)
sourceX = 1
for x in range(1, int(getWidth(pic))/4):
sourceY = 1
for y in range((1, int(getHeight(pic)/4):
color = getColor(getPixel(pic,sourceX ,sourceY))
setColor(getPixel(emptyCanvasGeneration, x, y), color)
sourceY = sourceY + 4
sourceX = sourceX + 4
writePictureTo(emptyCanvasGeneration, directory+ "//"+ "qtr_" +file)
return emptyCanvasGeneration




Sorry, I don't know what happened, let me paste it again.

My program works and I get quarter sized images that link to the big ones, but I wasn't sure about the writePictureTo part. Does what I did look ok?

def createThumbnail(directory, file):
  pic = makePicture(directory+"//"+file) 
  thumb = makeEmptyPicture ((getWidth(pic))/4,(getHeight(pic))/4)
  sourceX = 1
  for targetX in range (1,int(getWidth(pic)/4)):
    sourceY = 1
    for targetY in range(1,int(getHeight(pic)/4)):
      color = getColor(getPixel(pic,int(sourceX),int(sourceY)))
      setColor(getPixel(thumb,targetX,targetY), color)
      sourceY = sourceY + 4
    sourceX = sourceX + 4
  writePictureTo(thumb, directory+ "//" + "qtr_" +file)
  return thumb


Heather Symon

import os

def linksPage(directory):
samplesfile=open(directory+"//index2.html","wt")
samplesfile.write(doctype())
samplesfile.write(title("Index.html "+directory))
# Now, we make up the string... the body:
samples="

Index for "+directory+"

\n"
for file in os.listdir(directory):
if file.endswith(".jpg"):
createThumbnail(directory, file):
samples=samples+'

Filename:'+file+'\n'
samples=samples+'

\n'
pic = makePicture(directory+"//"+file)
samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"

\n"
samplesfile.write(body(samples))
samplesfile.close()

def doctype():
return ''

def title(titlestring):
return ""+titlestring+"" def body(bodystring): return ""+bodystring+""

def createThumbnail(directory, file):
pic = makePicture(directory+"//"+file)
emptyCanvasGeneration= makeEmptyPicture(getWidth(pic))/4 , (getHeight(pic))/4)
sourceX = 1
for x in range(1, int(getWidth(pic))/4):
sourceY = 1
for y in range((1, int(getHeight(pic)/4):
color = getColor(getPixel(pic,sourceX ,sourceY))
setColor(getPixel(emptyCanvasGeneration, x, y), color)
sourceY = sourceY + 4
sourceX = sourceX + 4
writePictureTo(emptyCanvasGeneration, directory+ "//"+ "qtr_" +file)
return emptyCanvasGeneration



can some one tell me how to put my code here???
I'd be more than happy to. You need to put it in <code> YOUR CODE HERE </code> Dannon Baker



On my links page, it gives the height and width in pixels of the picture along with its filename, should this be there or no?

No. Mark Guzdial

Maryam, you indented your entire program inside of linkspage. All of the def's should be flush against the left margin – no indentation at all. Mark Guzdial


I am still getting that error Maryam
"
import os

def linksPage(directory):
samplesfile=open(directory+"//index2.html","wt")
samplesfile.write(doctype())
samplesfile.write(title("Index.html "+directory))
# Now, we make up the string... the body:
samples="

Index for "+directory+"

\n"
for file in os.listdir(directory):
if file.endswith(".jpg"):
createThumbnail(directory, file):
samples=samples+'

Filename:'+file+'\n'
samples=samples+'

\n'
pic = makePicture(directory+"//"+file)
samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"

\n"
samplesfile.write(body(samples))
samplesfile.close()
def doctype():
return ''

def title(titlestring):
return ""+titlestring+"" def body(bodystring): return ""+bodystring+""

def createThumbnail(directory, file):
pic = makePicture(directory+"//"+file)
emptyCanvasGeneration= makeEmptyPicture(getWidth(pic))/4 , (getHeight(pic))/4)
sourceX = 1
for x in range(1, int(getWidth(pic))/4):
sourceY = 1
for y in range((1, int(getHeight(pic)/4):
color = getColor(getPixel(pic,sourceX ,sourceY))
setColor(getPixel(emptyCanvasGeneration, x, y), color)
sourceY = sourceY + 4
sourceX = sourceX + 4
writePictureTo(emptyCanvasGeneration, directory+ "//"+ "qtr_" +file)
return emptyCanvasGeneration

"

im having trouble could you please run this for me and tell me if its right or wrong...

import os

def linksPage(directory):
samplesfile=open(directory+"//Index.html","wt")
samplesfile.write(doctype())
samplesfile.write(title("Samples from "+directory))
# Body String...
samples="

Samples for "+directory+"

\n"
for file in os.listdir(directory):
if file.endswith(".jpg"):
samples=samples+'

Filename:">\n'
samples=samples+'

\n'
pic=makePicture(directory + "//"+file)
createThumbnail(directory, file)
samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"

\n"
samplesfile.write(body(samples))
samplesfile.close()

def doctype():
return ''

def title(titlestring):
return ""+titlestring+"" def body(bodystring): return ""+bodystring+""


def createThumbnail(directory, file):
pic=makePicture(directory+"//"+file)
thumb=makeEmptyPicture((getWidth(pic))/4,(getHeight(pic))/4)
sourceX=1
for targetX in range(1,int(getWidth(pic)/4)):
sourceY=1
for targetY in range(1,int(getHeight(pic)/4)):
color=getColor(getPixel(pic,(sourceX),(sourceY)))
setColor(getPixel(thumb,targetX,targetY), color)
sourceY=sourceY + 1
sourceX=sourceX + 1
return thumb

Mark, i fixed the indentations but still it gives me that error. Maryam
Check your parentheses and quotes, then. Something isn't matching. Mark Guzdial

import os

def linksPage(directory):
  samplesfile=open(directory+"//index2.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Index.html "+directory))
  # Now, we make up the string... the body:
  samples="<h1>Index for "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      createThumbnail(directory, file):
      samples=samples+'<p>Filename:<a href="'+file+'">'+file+'</a>\n'
      samples=samples+'<a href="'+file+'"><image src="'+"qtr_"+file+'"/></a></p>\n'
      pic = makePicture(directory+"//"+file)
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"
  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>"

def createThumbnail(directory, file):
  pic = makePicture(directory+"//"+file)
  emptyCanvasGeneration= makeEmptyPicture(getWidth(pic))/4 , (getHeight(pic))/4)
  sourceX = 1
  for x in range(1,  int(getWidth(pic))/4):
    sourceY = 1
    for y in range((1,  int(getHeight(pic)/4):
      color = getColor(getPixel(pic,sourceX ,sourceY))
      setColor(getPixel(emptyCanvasGeneration, x, y), color)
      sourceY = sourceY + 4
    sourceX = sourceX + 4 
  writePictureTo(emptyCanvasGeneration, directory+ "//"+ "qtr_" +file) 
  return emptyCanvasGeneration



"The anchor in the link to each full-size picture should be the quarter-size image."

does this mean we have the filename, then the 1/4 size image(which is a link to the fullsize image)?

James Lytle
Yes. Mark Guzdial

Okay, I am almost there, but am still having some problems. First off, in the command area I am typing in r'C:\Documents and
> Settings\Jonathan Longhitano\My Documents\My Pictures\DBZPics'), then I run my program, and it works, but when I go to the DBZPics folder there is a file called "index" there. So I assume this is all right? But then I click on the index file, and it displays the normal pictures and not thumbnails!!? Moreover, the pictures are repeated in there numerous times, and I do not know why? Here is my entire code? Any help would be great, seeing as I have been at this all day! Jonathan Longhitano
Maybe each time you ran your program you created new copies in your directory? Try cleaning them out. You're using Kyla's old thumbnail code – write the thumb out, not the pic Mark Guzdial
 import os

def linksPage(directory):
  samplesfile=open(directory+"//index.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Index.html "+directory))
  samples="<h1>Samples from "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"): 
      makeThumbnail(directory, file)
      samples=samples+'<p>Filename: <a href="'+file+'">'+file+'</a>\n'
      pic=makePicture(directory + "//"+file)
      samples=samples+'<image src="'+"qtr_"+file+'"/></p>\n'
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"
  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>"

def makeThumbnail(directory, file):
  pic = makePicture(directory+"//"+file)
  thumbnail = makeEmptyPicture((getWidth(pic))/4, (getHeight(pic))/4)
  sourceX = 1
  for targetX in range(1,int(getWidth(pic)/4)):
    sourceY = 1
    for targetY in range(1,int(getHeight(pic)/4)):
      color = getColor(getPixel(pic,(sourceX), (sourceY)))
      setColor(getPixel(thumbnail, targetX, targetY), color)
      sourceY = sourceY + 4
    sourceX = sourceX + 4 
  writePictureTo(pic,directory+"//"+"qtr_"+file)
  return thumbnail




look at the part after your image source code...compare it to other posts James Lytle


Could someone please tell me why some of my pictures will show up and some of them won't? I tried to modifythe qualifier in my 'if' statement like Dannon suggested above.....

if (file.endswith(".jpg") or file.endswith(".jpeg")):

.....but still only two of my four images show up.

~CB
The pictures could be .JPEG, .JPG, or other formats. You would have to account for each of those cases; just look at what kind of files you have and work with it.Dannon Baker



it looks like to me we are not supposed to put the filename...only the 1/4 size image that is a link....is this true? James Lytle
You should have the filename (not qtr_filename, but just the filename)as text, but that text shouldn't be a link, only the image should link.. Dannon Baker

import os

def linksPage(directory):
  samplesfile=open(directory+"//index.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Index.html "+directory))
  samples="<h1>Index for "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
      makeThumbnail(directory, file)
      samples=samples+'<p>Filename:<a href="'+file+'">'+file+'</a>\n'
      pic=makePicture(directory +"//"+file)      
      samples=samples+'<a href="'+file+'"><image src="'+"qtr_"+file+'"/></a></p>\n'
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"  
  samplesfile.write(body(samples))
  samplesfile.close()

def doctype():
  return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition//EN" "http://wwwww.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 makeThumbnail(directory,file):
  pic = makePicture(directory+"//"+file)
  thumbnail = makeEmptyPicture(getWidth(pic)/4, getHeight(pic)/4)
  sourceX = 1
  for targetX in range(1,int(getWidth(pic)/4)):
    sourceY = 1
    for targetY in range(1,int(getHeight(pic)/4)):
      color = getColor(getPixel(pic, sourceX, sourceY))
      setColor(getPixel(thumbnail, targetX, targetY), color)
      sourceY = sourceY + 4
    sourceX = sourceX + 4
  writePictureTo(thumbnail, directory+ "//" + "qtr_" +file)
  return thumbnail

CAN ANYONE SEE WHY THIS DOESN'T WORK? Meghan Quinlan
What error are you getting, Meghan? Mark Guzdial
I get a syntax error, but my code is the same as everyone elses.Meghan Quinlan

once we have our code working, is that all we submit? or do we also attatch our directory of files? thanks...
No files, please. Just your code. Mark Guzdial

I am getting this strange error when I run my program, referring to these two lines:

def makeSmallerPicture(directory):
for file in os.listdir(directory):

>>> makeSamplePage("hw4fldr")
An error occurred while making an operating system call. Please tell a TA what you were doing when this happened, so we may correct it.
in file C:\JES\hw4.py, on line 17, in function makeSamplePage
in file C:\JES\hw4.py, on line 33, in function makeSmallerPicture
in file C:\JES-SA1a\jython\.\Lib\javaos.py, on line 61, in function listdir OSError: [Errno 0] No such directory: hw4fldr/Alexandra.jpg
Please check line 33 of C:\JES\hw4.py
What's going on here?
Jonathan Laing
Please disregard above question. The problem is fixed.



hint: realizing i needed 3 inputs for the thumbnail program, letting me preserver the original filename when saving the qtr_images, was what allowed me to solve this homework.

def thumbnail(directory,image,filename):

and

writePictureTo(thumb,directory+"//qtr_"+filename)

good luck my friends

Ok...so there are these random six digit number appearing next to our thumbnail pictures. Not really sure what they are or why they are there. Does this matter because all of the requirements for the homework have been filled other than the fact that there are random numbers beside the pictures. Thank you~
Lauren Patton Karin Bowman

Are we only supposed to send in the program, or should pictures, website,...anything else also be attached?

All I turned in was the program. The TA's should have a directory that they'll pass into your linksPage function. See the last sentence of the HW assignment for specific instructions on what to call the file, your function, etc.
Lauren and Karin, if you want to email me your code, I'll look at it to see why you're getting six digit numbers in there. (That's weird...) Mark Guzdial
ONLY submit your program. It should work on ANY file. Mark Guzdial


I'm having a problem with generating the thumbnail image. The whole program works, but it saves "qtr_file.jpg" and it's the same size as "file.jpg". Any idea what's wrong with this?

def makeThumbnail(directory, file):
  pic = makePicture(directory+"//"+file)
  thumbnail = makeEmptyPicture((getWidth(pic))/4, (getHeight(pic))/4)
  sourceX = 1
  for targetX in range(1,int(getWidth(pic)/4)):
    sourceY = 1
    for targetY in range(1,int(getHeight(pic)/4)):
      color = getColor(getPixel(pic,(sourceX), (sourceY)))
      setColor(getPixel(thumbnail, targetX, targetY), color)
      sourceY = sourceY + 4
    sourceX = sourceX + 4 
  writePictureTo(pic,directory+"//"+"qtr_"+file)
  return thumbnail


Kristin Noell
Take another look at what picture you're actually writing out. I don't think you actually meant 'pic'. You want to save the thumbnail at this point, I think. Dannon Baker

this assignment is way out of line, i feel like my head is spinning, i think im gonna be sick....

word.

Ok I thought I could figure this one out but I have spent countless hours read what other people have posted and tryed IMing the TA's but I have been unsuccessful.  I keep getting big red x boxes where the thunbnails should be.  I am going crazy trying to compare my code to others posted and still cannot figure out why. PLZ check this out and let me know what you think.  
When I go back to check that the Thumbs have been made they arent showing up.  Could I be saving them wrong??

import os

def linksPage(directory):
  samplesfile=open(directory+"//index.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Index.html "+directory))
  samples="<h1>Index for "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
#THUMBNAIL
      Thumbnail(directory, file)
#SAMPLES PAGE 
      samples=samples+'<p>Filename:<a href="'+file+'">'+file+'</a>\n'
      pic=makePicture(directory +"//"+file)      
      samples=samples+'<a href="'+file+'"><image src="'+"qtr_"+file+'"/></a></p>\n'
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"  
  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>"

###### THUMBNAIL
def Thumbnail(directory, file):
  pic = makePicture(directory+"//"+file) 
  thumb = makeEmptyPicture ((getWidth(pic))/4,(getHeight(pic))/4)
  sourceX = 1
  for targetX in range (1,int(getWidth(pic)/4)):
    sourceY = 1
    for targetY in range(1,int(getHeight(pic)/4)):
      color = getColor(getPixel(pic,int(sourceX),int(sourceY)))
      setColor(getPixel(thumb,targetX,targetY), color)
      sourceY = sourceY + 4
    sourceX = sourceX + 4
  writePictureTo(thumb,directory+"//"+"qtr_"+file)
  return thumb








Ok I thought I could figure this one out but I have spent countless hours read what other people have posted and tryed IMing the TA's but I have been unsuccessful. I keep getting big red x boxes where the thunbnails should be. I am going crazy trying to compare my code to others posted and still cannot figure out why. PLZ check this out and let me know what you think.
When I go back to check that the Thumbs have been made they arent showing up. Could I be saving them wrong??


1st time poster... sorry about that
Ok I thought I could figure this one out but I have spent countless hours read what other people have posted and tryed IMing the TA's but I have been unsuccessful. I keep getting big red x boxes where the thunbnails should be. I am going crazy trying to compare my code to others posted and still cannot figure out why. PLZ check this out and let me know what you think.
When I go back to check that the Thumbs have been made they arent showing up. Could I be saving them wrong??

Marc Duran
Marc, your code works REALLY WELL for me! No little red boxes at all! They're not linked right, but they're there. Mark Guzdial

import os

def linksPage(directory):
  samplesfile=open(directory+"//index.html","wt")
  samplesfile.write(doctype())
  samplesfile.write(title("Index.html "+directory))
  samples="<h1>Index for "+directory+" </h1>\n"
  for file in os.listdir(directory):
    if file.endswith(".jpg"):
#THUMBNAIL
      Thumbnail(directory, file)
#SAMPLES PAGE 
      samples=samples+'<p>Filename:<a href="'+file+'">'+file+'</a>\n'
      pic=makePicture(directory +"//"+file)      
      samples=samples+'<a href="'+file+'"><image src="'+"qtr_"+file+'"/></a></p>\n'
      samples=samples+"Width:"+str(getWidth(pic))+" Height:"+str(getHeight(pic))+"</p>\n"  
  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>"

###### THUMBNAIL
def Thumbnail(directory, file):
  pic = makePicture(directory+"//"+file) 
  thumb = makeEmptyPicture ((getWidth(pic))/4,(getHeight(pic))/4)
  sourceX = 1
  for targetX in range (1,int(getWidth(pic)/4)):
    sourceY = 1
    for targetY in range(1,int(getHeight(pic)/4)):
      color = getColor(getPixel(pic,int(sourceX),int(sourceY)))
      setColor(getPixel(thumb,targetX,targetY), color)
      sourceY = sourceY + 4
    sourceX = sourceX + 4
  writePictureTo(thumb,directory+"//"+"qtr_"+file)
  return thumb


This might seem weird but I also made the code look for .jpg and .jpeg as listed here: if (file.endswith(".jpg") or file.endswith(".jpeg")):
All of my files are .jpg but only 3 of 6 will show. I've noticed that all of the files that wont show are sized 640X480. Is it possible this has something to do with why they won't show up????

Sekou

i just noticed that on my thumbnail pics there is a small black shadow-like border on the right and bottom of the picture – does anyone else have this? Is it okay to leave it like that? It works beautifully and my code goes along the same lines as everyone's posted above... any insight?
I just did a bit of testing. My input image was 500 wide, 217. I called makeEmptyPicure with these parameters: ((getWidth(pic))/4,(getHeight(pic))/4). The output image was created having dimensions 55 pixels wide, 126 tall. When we look at the math of it, getWidth(pic)/4 comes out to be 54.25. The makeEmptyPicture function seems to automatically round up to the next available integer (perhaps to avoid arrayIndexOutOfBoundsException, which a few of you have run into). You could get around this by specifically calling the int() function as you pass in the value, thus removing any rounding effect by truncating. example: makeEmptyPicture(int(getWidth....

Another reason is our use of the 'range' function. Range does NOT include the right endpoint. Have a look at this::>
>>> range(1, 5)
[1, 2, 3, 4]
So, you lose a second pixel there by saying range(1,getWidth(pic)/4). getWidth(pic)/4 being 54.25, truncating to 54, and then not including the right endpoint(54). So you only have data getting put into pixels 1-53.

Essentially, to answer the question in short, you're creating a canvas that is 1 or 2 pixels larger than the information that you're putting into it. Dannon Baker

ya i have the shadow border... but i'm also having trouble with my thumbnails, instead of displaying a smaller size of the original picture, it's just displaying the top left 1/4 of the image. it's kind of thumbnail-ish but i have a feeling this isnt going to go over well with the TA...

here's my thumbnail code:
def createThumbnail(directory, file):
  pic=makePicture(directory+"//"+file)
  qtr=makeEmptyPicture((getWidth(pic))/4,(getHeight(pic))/4)
  sourceX = 1
  for targetX in range(1,int(getWidth(pic)/4)):
    sourceY = 1
    for targetY in range(1,int(getHeight(pic)/4)):
      color = getColor(getPixel(pic,(sourceX),(sourceY)))
      setColor(getPixel(qtr,targetX,targetY), color)
      sourceY = sourceY + 1
    sourceX = sourceX + 1 
  writePictureTo(qtr, directory+ "//" + "qtr_" +file)
  return qtr

John Lindsey
You're only incrementing across your picture by source + 1 each time. You probably want to skip over by about 4 each time, since we're making 1/4 size images. Dannon Baker





Just to be clear: Doing int(variable) is NOT "casting". int() is a function that returns the integer part of its input. Mark Guzdial
Mea culpa, Thanks for pointing that out; I updated my post so it isn't misleading. Dannon

so, is it okay to leave that small shadowing? I am afraid that by adding int or the like, there may bring about an error of a small canvas... I feel that having the two pixel shadow (or whatever it may be)is safer and actually has a nice looking effect. I understand completely why it is there now - may I leave it and not be penalized?

nevermind! question answered. (the shadow one)


my code is opening the page index.html, but the pictures aren't on the page. They are in the folder hw4 along with the index page. I don't get it!

gyddudjsukjiewkreijirro9re03

Hey, I don't have this specific class with the rest of you, I am a student at Gainesville College and I was just wondering since we have similar assignments if I could get some help. In the second half of my homework 4 I have to take any wave files that are in the directory with the pictures and create a link to the original wave then if it is longer than 3 seconds I have to create a 1.5 second clip of it and place a link to that on the page as well. Thanks, and I am bookmarking this site, it has been a great help.
Erik

Links to this Page