Created
August 16, 2018 14:41
-
-
Save jcupitt/459959dfbbdf00371963d9f9d67af834 to your computer and use it in GitHub Desktop.
how to handle BytesIO in pyvips
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 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