我的意图是用来自 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 个小时,但我希望你能帮助我找出我的错误。我需要学习!