繁体   English   中英

Python + Wand.Image-使用连续的pagenumber.jpg名称将输出图像保存到AWS

[英]Python + Wand.Image - saving output images to AWS with sequential pagenumber.jpg names

我正在研究一个脚本,该脚本会将Internet上的PDF(不保存到磁盘)转换为一系列jpeg,然后将JPG保存到AWS s3。

不幸的是,下面的代码仅将PDF的第一页另存为JPG到AWS。 关于如何修改它以使用顺序文件名将图像保存到AWS的任何想法?

from urllib2 import urlopen
from wand.image import Image
from io import BytesIO
import boto3
    s3 = boto3.client(
        's3',
        aws_access_key_id='mykey',
        aws_secret_access_key='mykey'
    )

    bucket_name = 'testbucketAWS323'
    #location on disk

    #file prefix
test_id = 'example'
f = urlopen("https://s3.us-east-2.amazonaws.com/converted1jpgs/example.pdf")
bytes_io_file = BytesIO()
with Image(file=f) as img:
    print('pages = ', len(img.sequence))
    with img.convert('png') as converted:
        bytes_io_file = BytesIO(converted.make_blob('jpeg'))
      #code below should take 'converted' object, and save it to AWS as jpg. 
        s3.upload_fileobj(bytes_io_file, bucket_name, "assssd.jpg")
        print 'done'

只需枚举文档页面( wand.image.Image.sequence )即可获得页码和资源。 将页面资源复制到Image的新实例后,直接导出blob,不必担心中间转换。

from urllib2 import urlopen
from wand.image import Image
from io import BytesIO
import boto3

# ....

url = 'https://s3.us-east-2.amazonaws.com/converted1jpgs/example.pdf'
resource = urlopen(url)
with Image(file=resource) as document:
    for page_number, page in enumerate(document.sequence):
        with Image(page) as img:
            bytes_io_file = BytesIO(img.make_blob('JPEG'))
            filename = 'output_{0}.jpg'.format(page_number)
            s3.upload_fileobj(bytes_io_file, bucket_name, filename)

在转换时使用upload_fileobj方法怎么样?

暂无
暂无

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

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