簡體   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