View Single Post
Old Oct 31st, 2006, 4:06 PM   #5
public2
Newbie
 
Join Date: Aug 2006
Posts: 13
Rep Power: 0 public2 is on a distinguished road
Hey again.

I finally got finished with my assignment, and thought I would write the code down here. It turned out that we had to make most of the code in Jython, so some of the modules couldn't be used, but I managed anyway. Here is the complete code:

import urllib
from urlparse import urljoin
import random

def makeCollageFromUrl(urlString):    
    listOfImages = getImagesUrl(urlString)
    imageNames = []
    for imageUrl in listOfImages:
        filename = saveImage(imageUrl)
        imageNames.append(filename)
    width = 640
    height = 480
    picture = makeEmptyPicture(width,height)
    for imageName in imageNames:
        p = makePicture(imageName)
        if p.getWidth()<width and p.getHeight()<height:
            copyPictureToPicture(p,picture,random.randint(0,width-p.getWidth()),random.randint(0,height-p.getHeight()),0.5)
    
    picture.show()
    writePictureTo(picture,r"C:\HTMLCollage.jpg")

def getImagesUrl(urlString):
  connection=urllib.urlopen(urlString)
  getPictures = connection.read()
  connection.close()
  executeIndex = 0
  PicHTMLlist = []
  while getPictures.find("<img",executeIndex) <> -1:
    currentPicIndex = getPictures.find("<img",executeIndex)
    currentSrcIndex = getPictures.find("src=",currentPicIndex)
    nxtIndex = getPictures.find(">",currentSrcIndex)
    executeIndex = nxtIndex
    if getPictures.find("http",currentSrcIndex,nxtIndex)!=-1:
        end = getPictures.find(" ",currentSrcIndex,nxtIndex)
        currentPic = getPictures[currentSrcIndex+4:end]
        currentPic = currentPic.replace('"'," ")
        currentPic = currentPic.replace("'"," ")
        repCurrPic = currentPic.lstrip()
        repCurrPic = repCurrPic.rstrip()
        if repCurrPic.rfind(".jpg") != -1 or repCurrPic.rfind(".gif") != -1:
            PicHTMLlist.append(repCurrPic)
  return PicHTMLlist
  
def saveImage(urlString):
    connection = urllib.urlopen(urlString)
    getPictures = connection.read()
    connection.close()
    sepIndex = urlString.rfind("/")
    filnavn = urlString[(sepIndex+1):]
    file = open(filnavn,"wb")
    file.write(getPictures)
    file.close()
    return filnavn

def copyPictureToPicture(sourcePic,targetPic,offsetX,offsetY, blend):
  for x in range(1,sourcePic.getWidth()+1):
    for y in range(1,sourcePic.getHeight()+1):
      color = sourcePic.getPixel(x,y).getColor()
      targetPixel = targetPic.getPixel(x+offsetX,y+offsetY)
      targetColor = targetPixel.getColor()
      targetPixel.setRed(int(color.getRed()*blend+targetColor.getRed()*blend))
      targetPixel.setGreen(int(color.getGreen()*blend+targetColor.getGreen()*blend))
      targetPixel.setBlue(int(color.getBlue()*blend+targetColor.getBlue()*blend))
There might be some word in Danish, but most of it is in English. Thanks for your help Arevos.

Have a great evening.

Greetings Public2

Last edited by public2; Oct 31st, 2006 at 5:03 PM.
public2 is offline   Reply With Quote