将调整大小后的图像上传到S3

将调整大小后的图像上传到S3,第1张

将调整大小后的图像上传到S3

您需要先将输出图像转换为一组字节,然后才能上载到s3。您可以将图像写入文件然后上传文件,也可以使用cStringIO对象来避免写入磁盘,就像我在这里所做的那样:

import botoimport cStringIOimport urllibimport Image#Retrieve our source image from a URLfp = urllib.urlopen('http://example.com/test.png')#Load the URL data into an imageimg = cStringIO.StringIO(fp.read())im = Image.open(img)#Resize the imageim2 = im.resize((500, 100), Image.NEAREST)#NOTE, we're saving the image into a cStringIO object to avoid writing to diskout_im2 = cStringIO.StringIO()#You MUST specify the file type because there is no file name to discern it fromim2.save(out_im2, 'PNG')#Now we connect to our s3 bucket and upload from memory#credentials stored in environment AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEYconn = boto.connect_s3()#Connect to bucket and create keyb = conn.get_bucket('example')k = b.new_key('example.png')#Note we're setting contents from the in-memory string provided by cStringIOk.set_contents_from_string(out_im2.getvalue())


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5508375.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-13
下一篇2022-12-13

发表评论

登录后才能评论

评论列表(0条)

    保存