2012-10-16 32 views
1

我的意圖是通過東西方形網格與flickr中的圖片創建一些東西。我編寫了我所有的代碼,並且它已經達到了我嘗試調整參數的程度,以便我可以創建任意大小的網格(意思並不總是嚴格爲4x4,而是可以放入5x5網格或你有什麼)在程序結束時(Python),我的圖像沒有出現?

這是一個家庭作業,我已儘可能地得到。我不知道爲什麼圖像在代碼運行後沒有彈出。我一遍又一遍地調試過,看了很多網站,我不知道!我對編程比較陌生,所以請理解。提前感謝您的任何建議或線索!

import flickr import Image 

def adjust_sizing(image): 
    (w, h) = image.size 

    #proportionality 
    height = ((256*h)/w) 
    width = ((w/h) * 256) 

    if h > w: 
     image = image.resize((256 , (height))) 
    elif w > h: 
     image = image.resize(((width) , 256)) 

    image = image.crop((0, 0, 256, 256)) 
    image.save("image.png") 
    return image 


def photocollage(tag, number, rawnum, canvas): 
    url_list = flickr.getphotos(apicode, tag, number) 

    #number is number of images needed total 
    #rawnumb is number of rows/columns 

    #Create list of all coordinates that need to be occupied 
    coordinates = set() 
    i = 0 
    while i < rawnum: 
     w = (256 * i) 
     j = 0 
     while j < rawnum: 
      h = (256 * j) 
      j += 1 
      coordinates.add((w,h)) 
    i += 1 
    coordinates.add((w,h)) 
    cords = list(coordinates) 

    #Create list for all images that need to be matched with a coordinate 
    imagelist = [] 
    for url in url_list: 
     image = flickr.openphoto(url) 
     image.save("image.png") 
     image = adjust_sizing(image) 
     imagelist.append(image) 

    #paste to canvas image in image list[k] at coordinates cords[k] 
    k = 0 
    while k <= number: 
     canvas.paste(imagelist[k], cords[k]) 


    #return final image 
    return canvas 


def create_wallpaper(tag, length, output_name): 
      collage = Image.new('RGB', (length, length), 'white') 
      image = photocollage(tag, (length**2), length, collage) 
      image.save(output_name) 
      image.show()   


apicode = '219084039852' #I made this up for now, it's irrelevant 

create_wallpaper("cats", 4, "catpic.png") 

我知道這是一個代碼相當長的塊,但我真的很沮喪。我知道確定功能調整大小的作品。

這可能是因爲我一直盯着這個代碼8個小時,但我希望你能幫我弄清楚我的錯誤。我需要學習!

+0

我已經更新了我的答案。 –

回答

0

你永遠不會增加k所以你的while循環永遠不會結束。

您可能也僅僅使用for循環,而不是一個while循環:

for k in range(0, number): 
    canvas.paste(imagelist[k], cords[k]) 
+0

但image.show()以前工作..爲什麼現在它不起作用? – pearbear