Given a folder with images in it, create an HTML page named index.html that cont
ID: 3919368 • Letter: G
Question
Given a folder with images in it, create an HTML page named index.html that contains links to each JPEG file in the directory as follows: Using Jython.
-Write a function that creates a string of the path to a directory
-Generate a thumbnail (half the size) copy of each image
-Use makeEmptyPicture to create a blank picture in the correct size, then scale down the original picture into the blank picture
-Name the new image "half-" + the original filename (e.g., if the original filename was fred.jpg, save the half-size image as half-fred.jpg). The anchor in the link to each full-size picture should be the half-size image.
Explanation / Answer
Answer:
let the path be user\Mypicture\Myfolder
use the following code
>>>import os
>>>os.path.join('user','Mypicture','Myfolder')
#This will create a string of path to the directory. If the files in the folder are img1.jpeg,img2.jpeg and img3.jpeg, then the paths can be printed as
>>> NewFile=['Img1.jpeg','Img2.jpeg','ing3.jpeg']
>>>for filename in NewFile
print(os.path.join( 'c:\users\Mypictures\Myfolder', filename))
In order to create a blank picture in correct size and scale down each of the original image( here code for scaling down img1.jpeg is shown). The same principle can be extended to other images in the folder as well or a lop can be run for converting all images.
def copyMyImage():
MyImage=getMediaPath("Img1.jpg")
MyScaledImage=makePicture(MyScaledImage)
width=getWidth(MyScaledImage)
height=getHeight(MyScaledImage)
canvas=makeEmptyPicture(width,height)
#Perform actual copying
for a in range(0, width):
for b in range(0, height):
color=getColor(getPixel(MyScaledImage, a, b))
setColor(getPixel(canvas, a, b), color)
show(MyScaledImage)
show(canvas)
return canvasshow(canvas)
return canvas
Inorder to create the html file and create thumbnails the following code can be used( assuming that the images are wil jpg extension.
import os
def MyFirstPage(directory):
MyImageFiles=open(directory+"//MyImages.html","wt")
MyImageFiles.write(title("Images from "+directory))
# String which will form the body of the page
MyImages="<h1>Images from "+directory+" </h1>"
for file in os.listdir(directory):
if file.endswith(".jpg"):
MyImages=MyImages+"<p>Filename: "+ ”half-“ +file
MyImages=MyImages+’<image src="’+”half-“+file+’" height="100" /></p>’
MyImageFiles.write(body(MyImages))
MyImageFiles.close()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.