Skip to content

Instantly share code, notes, and snippets.

@dajor
Created February 3, 2020 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dajor/bdd2110e27e5a84de46dde954bb12bc9 to your computer and use it in GitHub Desktop.
Save dajor/bdd2110e27e5a84de46dde954bb12bc9 to your computer and use it in GitHub Desktop.
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