我有带有图像字段的表单和我的上传功能:
def handle_uploaded_file(f, gallery):
directory = settings.GALLERIES_ROOT+'gal_'+str(gallery.id)+'/'
# check if directory exist
try:
if not os.path.exists(directory):
os.makedirs(directory)
except Exception, e:
print 'DEBUG (directory): ', e
# find next pic number, to improve
i = 1
for r,d,f in os.walk(directory):
for file in f:
if fl.startswith("pic"):
i += 1
extension = os.path.splitext(f.name.replace(" ", "_"))[1]
filename = "pic%03d%s" % (i, extension)
# saving
try:
with open(directory + pf + filename, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
return True
except Exception, e:
print 'DEBUG (file writing problem): ', e
return False
现在我想调整上传图像的大小并将它们保存到文件 ico[number].[extension] 图像可能是 png 或 jpg、纵向或横向。如何使用纵横比来做到这一点(最终在缩小后裁剪更长的部分)?