Fall 2003 Homework 4 Questions
Questions about the linksPage project? Put 'em here!
I was trying out makeEmptyPicture, and it only does squares - if you tell it to make a picture that's 5 wide and 500 high (I was testing to make sure) it does a 5x5 square instead. So it works fine if you only want to use square pictures. And it would certainly be possible to work around it. But I thought probably that's not what you had in mind.
| Turns out that it DOES work, but for some reason, you only see a square black at the top. Try this: |
>>> p = makeEmptyPicture(100,500)
>>> addRect(p,1,1,100,500)
>>> show(p)
| It works. Don't know why it's only black at the top. Mark Guzdial |
Well, now I've tried to shrink a picture, and it only did the square. My program works fine for square pictures.
Also, it looks fine in JES (except for missing a chunk) but if I look at it outside JES the colors are all weird. Maybe that's just my computer, though. I've noticed problems in Paint too.
| We've tracked it down – it is a bug in JES. If you want to work on HW4 this weekend, let me know and I'll send you the file and tell you how to install it. If you're willing to wait until Monday, we should have a new updater available then. Mark Guzdial |
yeah...I tried to use writePictureTo function for an image made using makeEmptyPicture function, it's not viewable outside of JES. But interesting to note, if I try to open the same file in JES, it shows the image!
| There's a bug in the color model used in JES if you use makeEmptyPicture. It'll be fixed today–new update will be coming out. Mark Guzdial |
how do we start?
| Use the samples page example from the HTML lecture. Mark Guzdial |
I agree with the last post. I have no clue where to start. Will the lecture on Monday help?
| The 3 pm class saw the samples page example and we walked through how to do HW4. The 1 pm class will see it today. Mark Guzdial |
woohoo!
I can't get makePicture to work on a filename with "//", and jython apparently doesn't allow strings to end in "\". Any suggestions?
| Jython does allow strings to end in "\" – you just have to watch that you aren't saying \" which is how you embed a double quote in a string. It's another backslash escape. You can use "/" even in Windows. See below. Mark Guzdial |
>>> string = "ending\"
Your code contains at least one syntax error, meaning it is not legal jython.
>>> string = "ending\ "
>>> print string
ending\
>>> p=makePicture("C:/Documents and Settings/Mark Guzdial/My Documents/mediasources/barbara.jpg")
>>> show(p)
Examples from class
Here's the version of samples that gets the height and width:
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+" \n"
for file in os.listdir(directory):
print "NOW PROCESSING",file
if file.endswith(".jpg"):
picture = makePicture(directory+"/"+file)
height = getHeight(picture)
width = getWidth(picture)
samples=samples+"<p>Filename: "+file+'Height='+str(height)+' Width:'+str(width)+'\n'
samples = samples+'<a href="'+file+'">\n'
samples=samples+'<image src="'+file+'" height="100" />\n\n\t\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>"
And here's the version that's a schematic of how to do HW4:
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+" \n"
for file in os.listdir(directory):
if file.endswith(".jpg"):
makePicture(directory+"//"+file)
get Length, get Width, compute 1/2 of each
makeEmptyPicture(1/2size)
scale the file into the empty picture
write the picture out
# Now generate the HTML
samples=samples+"<p>Filename: "+file
samples = samples+'<a href="'+file+'">'
samples=samples+'<image src="half_'+file+'" />\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>"
- is this just supposed to be a guideline and not entirely actual code?
| YES! IT'S JUST A GUIDELINE! IT'S NOT ACTUAL CODE! Mark Guzdial |
import os
def linksPage(path):
file = open (path + "\\index.html", "wt")
file.write ("<html>Homework 4 ")
fileList = os.listdir(path)
for aFile in fileList:
if aFile.endswith (".jpg"):
picture = makePicture(path + aFile)
length = getLength(picture)/2
width = getWidth(picture)/2
emptyPict = makeEmptyPicture(length, width)
file.write ("<a href = \"" + aFile + "\">
")
file.write ("</body></html>")
file.close()
the above is what i have, kind of
and i am not sure what in the heck to do!
i don't know how to scale the picture down into the empty pict
| Yes, you do. Go back and look at how we scaled down Barbara's head. Remember skipping every other pixel? That's what you need to do. Your canvas is your new empty picture. Mark Guzdial |
are we supposed to check our results by uploading to coweb?.. is there another way?
| No, no need to upload anything to the CoWeb. Just double click on your HTML file and see if it looks right. Mark Guzdial |
I HATE THIS, I THINK I AM DOING IT RIGHT BUT WHEN I AM COPYING THE PIXELS IT KEEPS SAYING OUT OF BOUNDS
| Remember your strategies for dealing with this: Trace the code and see where it might be going beyond the height or width of your empty picture, add print statements to see what the values are when it gets the error, etc. Mark Guzdial |
by half the size, do you mean half of each side (which actually would be 1/4th the size), or do you mean sqrt(.5) of each side?
| Go for the 1/4th total area. I'll fix the homework assignment to make that clearer – thanks! Mark Guzdial |
i am still confused as to how to view the page, and what the directory is. is there a specific directory that we should use so that there are pictures in it, and since we do not specify a directory in our jes program, how does it know what the directory is?
| Take a look at the example above. You pass in directory as a parameter (specifically, a string which is a path to a directory). You should go ahead and make a directory with some pictures in it to test your program and pass in that directory as a parameter. Do not hard code in the directory. The TAs will make their own folder with images inside to grade the homework. Your recipe should produce a html document within that directory. Your TA will then open up the html file and be able to see the thumbnails of all the images in that directory. When your TA clicks on a thumbnail, he or she should be able to see the full image. Hope that makes sense. Angela Liang |
| I recommend making up a directory with just a few pictures in it. Maybe create a directory in your "My Documents" folder. Copy a few pictures from MediaSources into it. Use that one. ("Hard code" means setting the directory with an assignment inside the program. Your directory name should come from the input.) Mark Guzdial |
how do we download the updated JES? Every time i click on thlink on the website I get a offedr for a $29 WinZip? what am I supposed to do?
| What you are downloading is a zip file. A zip file is essentially a bunch of files and their directory structure stuffed into one container and compressed. To open the zip file that JES is in, you need a program that unzips the file. WinZip is one of these programs. It sounds like you have the evaluation version of WinZip, and it's throwing up an advertisement for the version you have to pay for. There should be a button somewhere on that screen to continue using the evaluation version. Just press that, and you'll open the main interface. Press the "extract" button and choose a place to put the files. This will take the files from the zip file and return them to their original structure. Now you'll have a new JES directory and you can click JES.exe to launch it. Adam Wilson |
Here is what we're working on, how can you test it, and does anyone see any problems with it. i'm really not sure if this will work
def linkspage(path):
file = open (path +"//index.html", "wt")
file.write("")
list = os.listdir(path)
for pic in list:
if pic.endswith(".jpg"):
picture = makePicture(path + list)
length = getLength(picture)
width = getWidth(picture)
emptypict = makeEmptyPicture(length,width)
"making thumbnails"
sourceX = 1
for targetX in range(1,width):
sourceY= 1
for targetY in range(1,height):
px=getPixel(picture,sourceX, sourceY)
ex=getPixel(emptypict,targetX,targetY)
"half_"pic = setColor(ex,getColor(px))
half_pic
sourceY = sourceY + 2
sourceX= source + 2
file.write("")
file.write("")
file.close("")
Well...- It's not valid Python, so it won't even run.
- Why do you need ME to test it? Run it yourself!
- I can't figure out what you're doing on some of these statements, e.g. – why are you writing "" to the file?
- Please read the FAQ page on how to post code so that it's readable to others.
Mark Guzdial |
When I run the following code I keep getting an java.lang.ArrayIndexOutOfBoundsException at the setColor line. I don't know what to do...?
for object in os.listdir(path):
if object.endswith (".jpg"):
pict = makePicture(path +"\\"+ object)
height = getHeight(pict)/2
width = getWidth(pict)/2
canvas = makeEmptyPicture(width,height)
sourceX = 1
for targetX in range(1,width):
sourceY = 1
for targetY in range(1,height):
color = getColor(getPixel(pict,sourceX,sourceY))
setColor(getPixel(canvas,targetX,targetY), color)
sourceY = sourceY + 2
sourceX = sourceX + 2
picts=picts+"Filename: "+object
picts=picts+'<a href="'+object+'">'
picts=picts+'<image src="half_'+object+'" />\n'
file.write(body(picts))
file.close()
| Please read the FAQ page on how to format your code. Have you updated your code to the latest JES? I think you have an off-by-one error that the latest JES will make go away. Mark Guzdial |
- I am getting the same error with code very similar to this and my JES is definitely updated. I even tried addding +1 on the width and height of the makeEmptyPicture function. Help please!
| Someone sent me their code, and I found that part of the problem is that the order of arguments to makeEmptyPicture is WIDTH then HEIGHT. If you put them the wrong way, your x and y won't fit. Mark Guzdial |
I tried to type in the first example program to see how it works, and I get an error with the last bit:
def body(bodystring):
return ""+bodystring+"</body></html>
That's the line I get the error on, and the error is: Your code contains at least one syntax error, meaning it is not legal jython.
The error is on line 28
Thanks!
Jeff Baker
Ok, so i got a program that i think works. the only question i have is does it matter where on the index.html page the thumbnails appear? all of my thumbnails are in a row across on one line (horizontally), is that ok?
A lot of the code that's posted is messed up because the html in it gets turned into links and images an stuff. Is there a way not to do that?
I think i have made a program, but when i try to run it, i'm having trouble. I made a folder and put 4 picts into it. I am putting the path into the function, so when i run the function i do : linksPath(the path of the folder)
is this correct?? i'm very confused as to how this works.
i have a question...is it ok if my index page has all the filenames labeled before the pictures like you showed in class? the HW4 description doesn't say we have to do this but i thought it was a fancy bonus
| Well, you won't get extra credit for it, but it's certainly not wrong, and it probably makes your page more readable. Sounds like a good idea to me! Mark Guzdial |
ok, so i got my program to work, it creates the html page, scales the picture, creates the anchor, and everything else, but the scaled picture wont show up on the page. all that shows up is a box with an X in it. how do i fix this?
| That means that your <image> tag isn't set up correctly. It's not referencing a valid file that can be shown Mark Guzdial |
import os
def linksPage(dir):
filepage = open(dir+"//index.html","wt")
filepage.write ("<body>")
for file in os.listdir(dir):
This is the beginning of my program. When I run it, I get this error:
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 3 of C:\Documents and Settings\Julia Walters\My Documents\hw4.py
Is it because I have to create the index.html page in my directory before I execute the program?
Julia Walters
| Do think about reading the FAQ page on how to format code when you post it on the CoWeb. No, you shouldn't have to create the index.html page. What are you passing in for the directory "dir"? Don't end it with a "/" or "\". Mark Guzdial |
for targetY in range(1,height):
px=getPixel(picture,sourceX,sourceY)
print targetX,targetY
ex=getPixel(emptypicture,targetX,targetY)
I wasn't able to do what you wanted.
The error java.lang.ArrayIndexOutOfBoundsException has occured
in file C:\JES\Homeworks\hw4, on line 19, in function linksPage
in file C:\JES\Sources\media.py, on line 790, in function getPixel
in file C:\JES\Sources\media.py, on line 465, in function getPixel
in file C:\JES\Sources\media.py, on line 545, in function __init__
java.lang.ArrayIndexOutOfBoundsException: java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
Please check line 19 of C:\JES\Homeworks\hw4
This should work,shouldn't it?
| It depends. Is height within the height of the picture? What is targetX set as? What are the values of targetX and targetY when you get the error? What's the size of the emptypicture? Mark Guzdial |
ok, so i'm trying to run my function, and i can't seem to type in the directory correctly.
Is this how you would do it?
dir=r"c:\pictures\hwr\"
linksPage(dir)
I keep getting a syntax error. Please Help!
| What error are you getting? That looks correct to me, assuming that youhave a directory named "pictures" in C: and a directory named "hw4" within that. Mark Guzdial |
I ran my program and everything worked fine. I went into the folder I created and it had the thumbnails (.5 original size), the original pictures, and the indexmhtml page. When I try to look at the index.html page, it is blank. I thought the pictures were suppposed to show up on it.
| The pictures only show up if you put the HTML tags in index.html so that you reference the pictures using <image> tags. Please revisit the HTML lecture notes. Mark Guzdial |
here is my program
i'm trying to run it, and i keep getting an error message on the get height line.
import os
def makeSamplePage(dir):
filepage=open(dir+"//index.html","wt")
filepage.write("<html><body>")
for file in os.listdir(dir):
if file.endswith(".jpg"):
pic=makePicture(dir+"//"+file)
height=int(getHeight(pic)/2)
width=int(getWidth(pic)/2)
emptypic=makeEmptyPicture(width,height)
sourceX=1
for targetX in range(1,width):
sourceY=1
for targetY in range(1,height):
px=getPixel(pic,sourceX,sourceY)
ex=getPixel(emptypic,targetX,targetY)
setColor(ex,getColor(px))
sourceY=sourceY+2
sourceX=sourceX+2
writePictureTo(emptypic,dir+"//half_"+file)
filepage.write("<a href ="'+file+'"/>\n")
filepage.write('<img src="half_'+file+'"/>\n')
filepage.write("</body></html>")
filepage.close()
here is the error message:
load image failed.
Was unable to load the image in C:\\Program Files\\JES\\mediasources\\hw4//desert.jpg
Make sure it's a valid image file.
getHeight(picture): Input is not a picture
An error occurred attempting to pass an argument to a function.
Please check line 8 of C:\Program Files\JES\newhw4.py
| The error is actually occurring earlier – in the makePicture. It actually stops executing at the getHeight because it's trying to get the height of a variable with no value, since the makePicture didn't work. Check your directory – I don't think you have it specified right. Mark Guzdial |
Sample index.html output file
Some people have been asking what the index.html file should look like after the program executes. I have a folder named C:\JES\hw4test that contains four pictures: band.jpg, boeing.jpg, pony.jpg, rafting.jpg. I executed my implementation of the homework assignment using the following command:
linksPage("C:\JES\hw4test")
and it generates the following HTML file.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>Images in c:/JES/hw4test</title></head>
<body><p><h1>Images in c:/JES/hw4test</h1></p>
<p><a href="c:/JES/hw4test/band.jpg"><image src="c:/JES/hw4test/half_band.jpg" /><br>band.jpg</a></p>
<p><a href="c:/JES/hw4test/boeing.jpg"><image src="c:/JES/hw4test/half_boeing.jpg" /><br>boeing.jpg</a></p>
<p><a href="c:/JES/hw4test/pony.jpg"><image src="c:/JES/hw4test/half_pony.jpg" /><br>pony.jpg</a></p>
<p><a href="c:/JES/hw4test/rafting.jpg"><image src="c:/JES/hw4test/half_rafting.jpg" /><br>rafting.jpg</a></p>
</body></html>
Stephen Voida
After running my program I go to the directory and see the index page, the pictures in the directory that are halved and then the "half_index.html". When I click on the index.html it has four boxes with x inside. And then when I click on the half_file picture in the directory, each of the thumbnails' colors are a little off. What can I do to fix this?
| Why do you have a page named half_index.html? I don't get that one. I don't know why the colors are a little off, but don't worry about that. If there are differences between what JES thinks the colors should be and what your browser thinks that they should be, we're not going to mark you down for that. Mark Guzdial |
argh something is wrong with my next to last line of code. I have tried linksfile.write("") and linksfile.write(body(links)) with the body function defined at the bottom and then i tried it with links in parantheses, but nothing works! It keeps saying: An error occurred attempting to pass an argument to a function.
Please check line 25 of C:\Documents and Settings\Laura\My Documents\hw4.py
| Laura, if linksfile is a valid file (you opened it already for writing), then linksfile.write("") should work. As for the body() function, it depends on how that function is implemented. You can send me your code, if you'd like, and I'll take a look at it. Mark Guzdial |
i don't understand why this is a problem....
but here is the error message(appears after i tried to makePicture()):
>>> directory = r"C:\Jes\Jes-Win.5\homework#4"
>>> linksPage(directory)
load image failed.
Was unable to load the image in C:\Jes\Jes-Win.5\homework#4//barbara.jpg
Make sure it's a valid image file.
getHeight(picture): Input is not a picture
An error occurred attempting to pass an argument to a function.
Please check line 9 of C:\Documents and Settings\Owner\My Documents\homework4
>>>
| Same issue that came up previously – is there a file at C:\Jes\Jes-Win.5\homework#4? Is one named barbara.jpg? Your program is saying that it can't find it. Mark Guzdial |
I don't know why but when i run the program i get not errors whatsoever.. But when i check my directory my thumbnails are black boxes are black images scaled down.. and when I check my samples page I my thumbnails are boxes with the x in them.. here is the code for my thumbnails..
def copyhalf(pict):
height=getHeight(pict)
width=getWidth(pict)
newheight=int(height/2)
newwidth=int(width/2)
EmptyPict=makeEmptyPicture(newwidth,newheight)
targetx=1
for sourcex in range(1,width,2):
targety=1
for sourcey in range(1,height,2):
p=getPixel(pict,sourcex,sourcey)
PColor=getColor(p)
p2=getPixel(EmptyPict,targetx,targety)
setColor(p2,PColor)
targety=targety=1
targetx=targetx=1
return EmptyPict
| Look at your targetx and targety lines. I think you meant those second "=" to be "+". You're actually not changing the empty picture at all, so it's staying black. Mark Guzdial |
Umm is this the right doctype tag?
return '<DOCTYPE HTML PUBLIC"_//W3C//DTD HTML 4.01 Transition//EN""http://wwww.w3.org/TR/html4/loose.dtd">'
| Sure – don't sweat the doctype tag. You won't be marked down for not having it right. Mark Guzdial |
| BTW: HW4 may not be as much fun, but it is realistic. Most websites with lots of images don't just use height= and width= in the <image> tags to provide a thumbnail – that ends up costing a lot of bandwidth since the fullsize image still has to be sent to the browser, and the browser scales it. Real websites tend to exactly this: Create thumbnails, then provide links to the actual pictures from the thumbnails. So this is a realistic task like one that real website designers need to do. Mark Guzdial |
Will the TA put the directory in the command area or is it necessary for us to define the directory within the function? Also, on the index page, should the links show up as thumbnails or just text in the form of links? Thanks.
| The TAs will put the directory in the command area so they will basically set the directory to their directory and do: linksPage(directory). On the index page, the links should show up as thumbnails. So you should be able to click on the thumbnail to show the full size picture. Angela Liang |
My code continually returns a syntax error and I cannot find it. Help, please...?
I fixed the problem with the = and + signs but I am still getting boxes with x's in them as my thumbnails and black images in my directory.. any idea what is going wrong??
I'm having the same trouble...and when that's not the problem I get an ArrayOutOfBounds error.
One time I ran the program and it finally gave me the page but with the black boxes...next time I changed the how the file name was printed without doing anything to the picture part and it gave me the array.... error........
Also for some reason my links (file print out link) takes me to the wrong picture....it says sunset and when I click on it, it takes me to stormfront
| ArrayOutOfBounds means that you are going beyond the size of the picture. Increase the size of the picture, or make sure that the numbers are smaller. (Maybe just subtract one from whatever the maximum value is that you have targetX and targetY reaching?). Mark Guzdial |
import os
def makeSamplePage(dir):
samplesfile=open(dir+"//index.html","wt")
samplesfile.write(doctype())
samplesfile.write(title("Samples from "+dir))
# Now, let's make up the string that will be the body.
samples="<h1>Samples from "+dir+"</h1> \n"
for file in os.listdir(dir):
if file.endswith(".jpg"):
print "NOW PROCESSING", file
pic=makePicture(dir+"//"+file)
height=getHeight(pic)
width=getWidth(pic)
newheight=int(height/2)
newwidth=int(width/2)
EmptyPict=makeEmptyPicture(newwidth,newheight)
targetx=1
for sourcex in range(1,width,2):
targety=1
for sourcey in range(1,height,2):
p=getPixel(pic,sourcex,sourcey)
PColor=getColor(p)
p2=getPixel(EmptyPict,targetx,targety)
setColor(p2,PColor)
targety=targety + 1
targetx=targetx + 1
writePictureTo(EmptyPict,dir+"//half_"+file)
samples=samples+"<p>Filename: "+file+'\n'
samples = samples+'<a href="'+file+'">\n'
samples=samples+'<image src="half_'+file+'" />\n'
samplesfile.write(body(samples))
samplesfile.close()
def doctype():
return ''
def title(titlestring):
return ""
def body(bodystring):
return ""+bodystring+"</body></html>"
sorry I don't know why it's making my code so big....if some could just go to edit and fix it cause I don't know the problem
And my problem are listed above the code for anyone wanting to help
I am getting an error on the line:
p2=getPixel(emptyPict,targetx,targety)
I have the JES update and everything. any suggestions on what I am doing wrong?
| WHAT IS THE ERROR?!?! Come on! I need to know the error to fix the problem. The errors DO have meaning! Mark Guzdial |
def copyHalf(pict):
height=getHeight(pict)
width=getWidth(pict)
newWidth=width/2
newHeight=height/2
emptyPict=makeEmptyPicture(newWidth,newHeight)
targetx=1
for sourcex in range(1,width,2):
targety=1
for sourcey in range(1,height,2):
p=getPixel(pict,sourcex,sourcey)
pcolor=getColor(p)
p2=getPixel(emptyPict,targetx,targety)
setColor(p2,pcolor)
targety=targety+2
targetx=targetx+2
return (emptyPict)
my code is going crazy...and so i'm I....can't we just make it easier or someone post a final product.
The links beside my pictures on my index.html page take me to the wrong picture when i click on them. but when i click on the pictures, it takes me to a bigger version of that picture. what is wrong with it? why does it do that?
i'm getting an picture box with an X in it on my page...and I then my image tag is right
samples=samples+'image src="half_'+file+'" />\n'
??????????????
and I lefr the before image out so it would show up on this page.
thanks
Guzdial where are you I'm dieing out here
ok and sometimes like this last time the page showed up with a black box not the box with an x but either way
| I'm here. Where were you when we did this in lecture? It's the line with the <a href= – that's where you're setting up the link. Fix that line. Mark Guzdial |
When my picture is scaled down, the resultant "half_image" has an extra pixel added to the width and height. For example, I have a 800 by 600 image. I added print statement that tell me it took the width or height and divided each by two. It printed 400 and 300. But after makeEmptyPicture and so on, the final halved image has 401 by 301 dimensions. Why is this and will this count against me?
I think my code is correct, but I i don't know how to run it. how do I set a directory and what else do i need to do in the command area. I've looked through all my notes and the new text pages, but I just can't find it...please help me
| Come to lecture or read the slides! makeSamplePage(r"C:\hw4") (or whatever your directory name is.) Mark Guzdial |
My thumbnails keep linking to the directory. I think it has to do with the placement of a quotation mark, but can someone give me some advice? Thanks.
samplesfile.write(" <a href = '''+file+''' />\n")
samplesfile.write(' <image src = "half_' +file+'" />\n')
| Use the colors – that's why colors change, to let you know where strings start and end. I can tell that the first one is wrong – you're switching between using " and ''' midstring. Can't do that. Mark Guzdial |
someone please tell me what is wrong with this code I've done all I know to do
| What's the error? What's going on? You have to provide SOME of the help in debugging your program... Mark Guzdial |
import os
def makeSamplePage(dir):
samplesfile=open(dir+"//index.html","wt")
samplesfile.write(doctype())
samplesfile.write(title("Samples from "+dir))
# Now, let's make up the string that will be the body.
samples="<h1>Samples from "+dir+" \n"
for file in os.listdir(dir):
if file.endswith(".jpg"):
print "NOW PROCESSING", file
pic=makePicture(dir+"//"+file)
height=getHeight(pic)
width=getWidth(pic)
newheight=int(height/2)
newwidth=int(width/2)
EmptyPict=makeEmptyPicture(newwidth,newheight)
targetx=1
for sourcex in range(1,width,2):
targety=1
for sourcey in range(1,height,2):
p=getPixel(pic,sourcex,sourcey)
PColor=getColor(p)
p2=getPixel(EmptyPict,targetx,targety)
setColor(p2,PColor)
targety=targety + 1
targetx=targetx + 1
writePictureTo(EmptyPict,dir+"//half_"+file)
samples=samples+"<p>Filename: "+file
samples = samples+'<a href="'+file+'">\n'
samples=samples+'<image src="half_'+file+'" />\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">'
I got the program to run through but the index page looks a little wierd. I'm not sure if my computer is retarted...but the thumbnails show up as boxes with X's in them. Also there isn't any spacing between the title and the picture files. Maybe there is some html code you could post to help me out with spacing? Thanks.
| For the spacing issue, you can put " " without the quotations for a space. If you want multiple spaces, just do it over and over like " " would give you 3 spaces (without quotations). Angela Liang |
I got through the program and I am online now (for the person asking for help). -Irish Peaches 2 (sn)
PLEASE FOLLOW THE RULES ON FAQ FOR POSTING CODE!
I just spent 20 minutes fixing the page because of all the HTML code people are uploading. Please, PLEASE use < instead of <. Mark Guzdial
For turn-in, do we need to include original images or a media folder or will the TA's use their own?
| The only thing you need to turn in is your code. The TAs will run your program on a directory they create and your program should generate an index.html file with all the things specified in the hw4 assignment. Angela Liang |
When i run the program i get an index in my folder but when i open it all that shows up are four boxes with X's in the middle like it cant display the picture but when i click on them it takes me to the page where it is displayed in full. What is going on?
When I run the program I get the four boxes but they all have X in them and when I click on them it takes me to my picture. Is there a problem with this line of code.
file.write ("<a href = \"" + aFile + "\">
")
| Looks okay, but PLEASE do use the FAQ and replace the <'s with <! Mark Guzdial |
| TA's will use their own directories for running. If yo'ure getting red X's, it means that you're not getting valid images in your <image> tags – either you're referring to an image that isn't there (i.e., you're not writing the picture out), or your reference to the image is wrong (e.g., maybe you've got the filename wrong.) DO View Source to see what's going in in the indx.html file. Mark Guzdial |
When this program is run, it creates a black box, scaled to teh right size, but it is jsut black. I went through the code and I am pretty sure it is changing the new canvas, but obvioulsy Im not. Also when I open my page in Opera it has a box with "image" written in it and when you click on it it says "The address name is unknown or is unsupported." But when I open it in IE it shows me my black thumbnails and then when I clck them it takes me to the big picture. Whats up witht eh difference? And is it due to a program or html problem or is it just Opera being stupid?
def halfsize(pic):
nheight=int(getHeight(pic)/2)
nwidth=int(getWidth(pic)/2)
canvas=makeEmptyPicture(nwidth,nheight)
targetx=1
for sourcex in range(1,getWidth(pic)-1,2):
targety=1
for sourcey in range(1,getHeight(pic)-1,2):
sourcep=getPixel(pic,sourcex,sourcey)
targetp=getPixel(canvas,targetx,targety)
setColor(targetp,getColor(sourcep))
targety=targety+1
targetx=targetx+1
return canvas
| Nevermind, Steve found my error, my targetx=targetx+1 is not spaced correctly, it should be lined up under for sourcey in range... |
I keep getting the following error:
makeSamplePage
NameError: title
why is title a name error? My code looks very similar to what every one else has posted but I keep getting that error. Is it because of my directory?
when trying to make smaller images of my pictures, I get the error message 'imput is not a picture' when it tries to get the pictures higth. I know that it is a picture because I put the file in directly, but the code is't messed up as far as I can tell. anyone have a guess what's going on?
| The code IS messed up – you're not actually opening the picture. Try printing out what you're trying to open and see what it looks like. Mark Guzdial |
def copyhalf(pict):
height=getHeight(pict)
width=getWidth(pict)
newheight=int(height/2)
newwidth=int(width/2)
EmptyPict=makeEmptyPicture(newwidth,newheight)
targetx=1
for sourcex in range(1,width,2):
targety=1
for sourcey in range(1,height,2):
p=getPixel(pict,sourcex,sourcey)
PColor=getColor(p)
p2=getPixel(EmptyPict,targetx,targety)
setColor(p2,PColor)
targety=targety+1
targetx=targetx+1
return EmptyPict
this code is returning a samples page with boxes with X's in them and the half images in the directory are all black.. any ideas whats wrong?
| You're not writing out the EmptyPict using writePictureTo, and your indentation for targetx=targetx+1 is wrong – it has to be indented 2. Mark Guzdial |
how do I define Title? I looked at the lecture notes and title is used the same way as its used in my program?
thanks
| You could send me your code, if you like, but you should have def title() in your code. Try testing it by itself. Mark Guzdial |
Here are my first 7 lines of code:
import os
def linksPage(dir):
samplesfile=open(dir+"//index.html","wt")
samplesfile.write(doctype())
samplesfile.write(title("Samples from "+dir))
samples="<h1<Samples from "+dir+" \n"
and I keep getting this error message when I try to run my program
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 6 of C:\hw4.py
Any ideas?
| You're missing the function title. BTW, if you turn on Expert mode (Go to the Edit menu, then choose Options, then switch Normal to Expert, then click OK), it'll tell you EXACTLY which name is causing you headaches. Mark Guzdial |
My program creates the thumbnails, but I can't seem to make it link to the original picture - it just tries to go to "file." Any ideas?
Here are my lines that I think are erroneous...
writePictureTo(emptypic,dir+"//half_"+file)
filepage.write("<a href ="'file'"/>\n",)
filepage.write('<img src="half_'+file+'"/>\n')
| You forgot the +'s so that you add in the file into the middle of the anchor tag. Mark Guzdial |
def copyhalf(pict):
height=getHeight(pict)
width=getWidth(pict)
newheight=int(height/2)
newwidth=int(width/2)
EmptyPict=makeEmptyPicture(newwidth,newheight)
targetx=1
for sourcex in range(1,width,2):
targety=1
for sourcey in range(1,height,2):
p=getPixel(pict,sourcex,sourcey)
PColor=getColor(p)
p2=getPixel(EmptyPict,targetx,targety)
setColor(p2,PColor)
targety=targety+1
targetx=targetx+1
return EmptyPict
Okay.. everything works fine.. no errors.. when i run the program, I see 4 half size imgages in my directory. When I open index.html I get 4 boxes with X's in them, that when I click on them take me to the correct fullsize image.. I just cant figure out why iam not getting the half images to get on index.html... I know iam not the only one with this problem...Can someone please explain whats wrong.
PS if its a writePictureTo problem can someone explain that to me.. I think it goes after the target X line. but i dont know how to write it out, and if i even need to write it out.. thanks alot
| It's not the writePictureTo – that's fine if you can see the half images in your directory. The problem is in your HTML. You're not generating the <image> tag correctly. Mark Guzdial |
My thumbnails are still coming up black even after updating to the most recent version of JES. Also, my first link (next to my first thumbnail) is actually not a link, it's just text even though clicking on the thumbnail links to the actual image and all my other links work. What's up with that?
| If it's still black, it means that your copying isn't working – you're generating an empty picture (that's why it's black) and you're writing it out to a file, but your copying isn't working right. The most common error I'm seeing there is not indenting the sourceX = sourceX + 2 line correctly. Mark Guzdial |
# Now generate the HTML
samples=samples+"Filename: "+file
samples = samples+'<a href="'+file+'">'
samples=samples+'<image src="half_'+file+'>"\n"
'
samplesfile.write(body(samples))
samplesfile.close()
This is my html code.. I posted the second last question about the boxes with X's in them.. and idea whats wrong with the image line?? I don't understand i fiddled around with it a couple of times already.. Thanks
| YOu have the double quotes in the wrong place—must be inside the > on the image tag. Mark Guzdial |
Okay, so when testing my code, it all works correctly until the second to last line. I get the following error in the command area: You are trying to access a part of the object that doesn't exist.
Please check line 28 of C:\JES\hw4.py
Line 28 is this: file.write(body(files))
("file" is "samplefiles" and "files" is "samples" in Guzdial's version)
| That means that you didn't upload to the new JES, or you didn't open the file correctly. Mark Guzdial |
Link to this Page