Skip to content

Instantly share code, notes, and snippets.

@jcupitt
Created August 16, 2018 14:41
Show Gist options
  • Save jcupitt/459959dfbbdf00371963d9f9d67af834 to your computer and use it in GitHub Desktop.
Save jcupitt/459959dfbbdf00371963d9f9d67af834 to your computer and use it in GitHub Desktop.
how to handle BytesIO in pyvips
import asyncio
from functools import partial
from io import BytesIO
import aiohttp
import pyvips
async def get_image():
async with aiohttp.ClientSession() as session:
async with session.get("https://user-images.githubusercontent.com/29148882/43419292-d2ae9d68-9440-11e8-80f5-dcdaaae59e2e.png") as req:
image_bytes = BytesIO(await req.read())
return image_bytes
def invert_pyvips(image_bytes):
image = pyvips.Image.new_from_buffer(image_bytes.read(), "")
image = image * [-1, -1, -1, 1] + [255, 255, 255, 0]
return image.write_to_buffer(".png")
async def main():
image_bytes = await get_image()
func = partial(invert_pyvips, image_bytes)
print(type(await loop.run_in_executor(None, func)))
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment