我的目的是用flickr中的图片通过方格网格创建一些东西。我已经编写了所有代码,并且它已经完成了我已经尝试调整参数的点,以便我可以创建任何大小的网格(意味着并不总是严格的4x4,而是我可以放入5x5网格或你有什么)
这是一项家庭作业,我尽我所能。我不知道为什么代码运行后图像没有弹出。我一遍又一遍地调试它,看了很多网站,我不知道!我对编程比较陌生,所以请理解。提前感谢您的任何建议或线索!
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])