gpt4 book ai didi

python - 如何在使用 PIL 修改文件上传表单后将文件上传到 s3?

转载 作者:太空宇宙 更新时间:2023-11-03 11:02:48 25 4
gpt4 key购买 nike

代码如下:

app.config['DEBUG']= True


if request.method == 'POST' and request.form['file_submit']:
print request.form
print request.files['image']
if request.files['image']:
print 'foshhh'
image_file = request.files['image']
img = PIL.Image.open(image_file.stream)
print img
if request.form['make_transparent']:
threshold=100
print 'changin sizesd'
dist=5
# np.asarray(img) is read only. Wrap it in np.array to make it modifiable.
arr=np.array(np.asarray(img))
r,g,b,a=np.rollaxis(arr,axis=-1)
mask=((r>threshold)
& (g>threshold)
& (b>threshold)
& (np.abs(r-g)<dist)
& (np.abs(r-b)<dist)
& (np.abs(g-b)<dist)
)
arr[mask,3]=0
img=Image.fromarray(arr,mode='RGBA')
if request.form['change_size']:
img = Image.open('out.png')
img.thumbnail(size,Image.ANTIALIAS)
img.save('out.png',"PNG")

img.save('out.png',"PNG")
print os.path.getsize("out.png") #from answer
assert os.path.isfile("out.png") #from answer
conn = S3Connection(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)
b = conn.get_bucket('snappie.watermarks')
k = Key(b)
k.key = "test.png"
k.set_metadata('Content-Type', 'image/png')
k.set_contents_from_filename("out.png")
print "got file"
return redirect("https://s3.amazonaws.com/snappie.watermarks/"+filename)
else:
print 'please upload a file to submit the form!'

这是 html 表单:

   <form method="POST" enctype="multipart/form-data" name="file_submit">
<label>Choose png here.<input type="file" name="image"></label>
<input type="hidden" name="file_submit" value="yes">
Change size?<input type="checkbox" name="change_size" value="yes"/>
Make Background Transparent?<input type="checkbox" name="make_transparent" value="yes"\><br><br>
<input type="submit" value="submit">
</form>

部分问题是它并没有真正给我错误日志。你可以看到这个想法是有 2 个复选框可以修改图像文件。如果第一个被选中,它的“透明化”,如果第二个被选中,它会改变大小。

我想我在转换和修改图像对象时遇到了问题,尤其是当它被传递到这个对象时:k.set_contents_from_filename("out.png")

这里有什么帮助吗?这是服务器日志给我的唯一输出:

GET
127.0.0.1 - - [18/Jan/2015 15:42:26] "GET / HTTP/1.1" 200 -
POST
ImmutableMultiDict([('file_submit', u'yes')])
<FileStorage: u'birnam_wood.jpg' ('image/jpeg')>
foshhh
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=100x100 at 0x7FDDF4CA6C68>
127.0.0.1 - - [18/Jan/2015 15:42:32] "POST / HTTP/1.1" 400 -

最佳答案

为了正确的调试目的,您确实需要查看回溯。它会告诉你哪里出了问题:-)!!

否则:我猜你的一般方法(首先在文件系统中创建一个图像文件,然后使用 boto 上传它)是好的。但是,出于调试目的,您可以检查 img.save('out.png',"PNG") 留下的内容。出于调查目的,您可以测试文件是否存在,否则会引发异常:assert os.path.isfile("out.png")。此外,您可能希望使用 os.path.getsize("out.png") 打印文件大小。据我记得使用 boto 时,k.set_contents_from_filename("out.png") 是正确的选择。

也就是你做事的顺序是对的。正如 Dmitry 已经指出的那样,S3 很可能存在身份验证/连接问题。您将通过查看 Traceback 找到此问题的详细信息。 boto 回溯将包含错误 AWS 错误响应。

关于python - 如何在使用 PIL 修改文件上传表单后将文件上传到 s3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28016203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com