项目作者: Neoteroi

项目描述 :
Classes to handle pictures stored in Azure Storage Blob Service with Gallerist
高级语言: Python
项目地址: git://github.com/Neoteroi/Gallerist-AzureStorage.git
创建时间: 2019-09-21T10:11:18Z
项目社区:https://github.com/Neoteroi/Gallerist-AzureStorage

开源协议:MIT License

下载


Build
pypi
versions
license

Gallerist-AzureStorage

Gallerist classes for Azure Storage: implements reading image files from Azure
Blob Service, and writing of resized pictures to the same.

  1. $ pip install gallerist-azurestorage

This library is used in Torino, a file
explorer for Azure Storage Account, and in
Gallerist-AzureFunctions,
an Azure Functions front-end that uses
Gallerist library, to resize
pictures in Azure Storage Blob Service.

Example: synchronous code resizing pictures on Azure Storage

  1. from gallerist import Gallerist, ImageSize
  2. from galleristazurestorage import AzureBlobFileStore
  3. store = AzureBlobFileStore.from_connection_string(
  4. "<YOUR_CONNECTION_STRING>",
  5. "CONTAINER_NAME",
  6. )
  7. gallerist = Gallerist(store)
  8. # configuring sizes by mime (use '*' to match any other mime):
  9. gallerist = Gallerist(
  10. store,
  11. sizes={
  12. "image/jpeg": [ImageSize("a", 1200), ImageSize("b", 600), ImageSize("c", 300)],
  13. "image/png": [ImageSize("a", 350), ImageSize("b", 250), ImageSize("c", 150)],
  14. },
  15. )
  16. # the following function call causes the creation of several versions of the
  17. # image in different sizes; note that this operation is CPU bound
  18. metadata = gallerist.process_image("ORIGINAL_FILE_NAME_ALREADY_ON_STORAGE.png")
  19. print(metadata)

Asynchronous example using executors (recommended for async scenarios)

  1. import asyncio
  2. import concurrent.futures
  3. from gallerist import Gallerist, ImageSize
  4. from galleristazurestorage import AzureBlobFileStore
  5. store = AzureBlobFileStore.from_connection_string(
  6. "<YOUR_CONNECTION_STRING>",
  7. "CONTAINER_NAME",
  8. )
  9. gallerist = Gallerist(store)
  10. # configuring sizes by mime (use '*' to match any other mime):
  11. gallerist = Gallerist(
  12. store,
  13. sizes={
  14. "image/jpeg": [ImageSize("a", 1200), ImageSize("b", 600), ImageSize("c", 300)],
  15. "image/png": [ImageSize("a", 350), ImageSize("b", 250), ImageSize("c", 150)],
  16. },
  17. )
  18. async def main():
  19. loop = asyncio.get_event_loop()
  20. with concurrent.futures.ProcessPoolExecutor() as pool:
  21. metadata = await loop.run_in_executor(
  22. pool, gallerist.process_image, "EXISTING_FILE_ON_STORAGE.jpg"
  23. )
  24. print(metadata)
  25. asyncio.run(main())

Alternatively to using an executor explicitly, it is possible to use
loop.call_soon_threadsafe:

  1. from gallerist import Gallerist, ImageSize
  2. from galleristazurestorage import AzureBlobFileStore
  3. store = AzureBlobFileStore.from_connection_string(
  4. "<YOUR_CONNECTION_STRING>",
  5. "CONTAINER_NAME",
  6. )
  7. gallerist = Gallerist(store)
  8. # configuring sizes by mime (use '*' to match any other mime):
  9. gallerist = Gallerist(
  10. store,
  11. sizes={
  12. "image/jpeg": [ImageSize("a", 1200), ImageSize("b", 600), ImageSize("c", 300)],
  13. "image/png": [ImageSize("a", 350), ImageSize("b", 250), ImageSize("c", 150)],
  14. },
  15. )
  16. def process_image(image_path: str):
  17. # configuring sizes by mime (use '*' to match any other mime):
  18. gallerist = Gallerist(
  19. store,
  20. sizes={
  21. "image/jpeg": [
  22. ImageSize("a", 1200),
  23. ImageSize("b", 600),
  24. ImageSize("c", 300),
  25. ],
  26. "image/png": [
  27. ImageSize("a", 350),
  28. ImageSize("b", 250),
  29. ImageSize("c", 150),
  30. ],
  31. },
  32. )
  33. metadata = gallerist.process_image(image_path)
  34. print(metadata)
  35. async def main():
  36. loop = asyncio.get_event_loop()
  37. loop.call_soon_threadsafe(process_image, "EXISTING_FILE_ON_STORAGE.jpg")
  38. asyncio.run(main())

Asynchronous example using asynchronous methods from azure-storage-blob.aio

Note: azure-storage-blob requires aiohttp, and is not compatible with
concurrent.futures.ProcessPoolExecutor.

  1. import asyncio
  2. from gallerist import Gallerist, ImageSize
  3. from galleristazurestorage.aio import AzureBlobAsyncFileStore
  4. store = AzureBlobFileStore.from_connection_string(
  5. "<YOUR_CONNECTION_STRING>",
  6. "CONTAINER_NAME",
  7. )
  8. gallerist = Gallerist(store)
  9. # configuring sizes by mime (use '*' to match any other mime):
  10. gallerist = Gallerist(
  11. store,
  12. sizes={
  13. "image/jpeg": [ImageSize("a", 1200), ImageSize("b", 600), ImageSize("c", 300)],
  14. "image/png": [ImageSize("a", 350), ImageSize("b", 250), ImageSize("c", 150)],
  15. },
  16. )
  17. async def main():
  18. metadata = await gallerist.process_image_async(
  19. "EXISTING_FILE_ON_STORAGE.jpg"
  20. )
  21. print(metadata)
  22. asyncio.run(main())