Created
February 3, 2020 11:23
-
-
Save dajor/bdd2110e27e5a84de46dde954bb12bc9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, io | |
import logging | |
from PIL import Image | |
import azure.functions as func | |
import tempfile | |
from azure.storage.blob import BlockBlobService | |
from pdf2image import convert_from_path, convert_from_bytes | |
BlobAccount = os.environ['BlobAccount'] | |
BlobKey = os.environ['BlobKey'] | |
BlobItem = os.environ['BlobItem'] | |
def main(myblob: func.InputStream): | |
logging.info(f"Python blob trigger function processed blob \n" | |
f"Name: {myblob.name}\n" | |
f"Blob Size: {myblob.length} bytes") | |
file = myblob.read() | |
image = io.BytesIO(file) | |
blob_service = BlockBlobService(account_name=BlobAccount, account_key=BlobKey) | |
blob_name = myblob.name.replace(BlobItem + "/", "") | |
with open(blob_name, "wb") as outfile: | |
# Copy the BytesIO stream to the output file | |
outfile.write(image.getbuffer()) | |
with tempfile.TemporaryDirectory() as path: | |
pages = convert_from_path(blob_name,dpi=600, output_folder=path) | |
x=1 | |
for page in pages: | |
filename = str(x)+".jpg" | |
page.save(filename, "JPEG") | |
x = x +1 | |
blob_service.create_blob_from_path(BlobItem,filename,filename) | |
os.remove(filename) | |
os.remove(blob_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment