使用Python和PIL裁剪并保存上传的图像
我遇到了一个问题,想要裁剪上传的图片并保存它。我正在使用Cherrypy。
@cherrypy.expose
def uploadphoto(self, **data):
photo = data['photo']
original_image = Image.open(StringIO(photo.file)) //error happen here
box = (0, 0, 100, 100)
cropped_image = photo.crop(original_image)
path = 'uploads/myphoto.jpg'
cropped_image.save(path, 'JPEG');
这是表单:
<form id="form" action="uploadphoto" method="post" enctype="multipart/form-data">
<input type='file' name="photo"/>
<input type="submit" value="save" />
</form>
有没有什么问题?我刚学Python,请帮帮我。
2 个回答
0
尝试通过以下方式将文件指针重置到文件的开头:
photo.file.seek(0)
0
看看这个页面上“发布编辑过的照片(这个例子调整了照片大小)”的标题。
你是不是应该在裁剪的那一行里加上.size呢?
像这样:
cropped_image = photo.crop(original_image.size)