简体   繁体   English

使用 python 和 PIL 裁剪并保存上传的图像

[英]crop and save uploaded image with python and PIL

i am having a problem crop the uploaded image and save it.我在裁剪上传的图像并保存时遇到问题。 I am using Cherrypy.我正在使用 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');

here is the form:这是表格:

    <form id="form" action="uploadphoto" method="post" enctype="multipart/form-data">
        <input type='file' name="photo"/>
        <input type="submit" value="save" />
    </form>

is there anything wrong?有什么不对的吗? i am new to python, please help me out.我是python的新手,请帮帮我。

Look at the "Posting an Edited Photo (This example resizes a photo)" heading on this page.查看页面上的“发布编辑过的照片(此示例调整照片大小)”标题。

shouldn't you include .size in the crop line?您不应该在裁剪线中包含 .size 吗?

like this:像这样:

    cropped_image = photo.crop(original_image.size)

尝试通过以下方式将文件指针重置为文件开头:

photo.file.seek(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM