[Sync] Image building now is async
This commit is contained in:
1 parent
87da3492ef
commit
ca691b492a
1 file changed
+21
-22
@@ -1,6 +1,8 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import asyncio
|
||||||
|
|
||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
|
|
||||||
import settings
|
import settings
|
||||||
@@ -80,12 +82,15 @@ class Image(Model):
|
|||||||
|
|
||||||
async def upload(self):
|
async def upload(self):
|
||||||
if self.data:
|
if self.data:
|
||||||
for i in self.data.values():
|
async def fn(i):
|
||||||
url = get_upload_url(i['url'])
|
url = get_upload_url(i['url'])
|
||||||
file = i['file']
|
file = i['file']
|
||||||
|
|
||||||
|
logger.info('Uploading to {}'.format(url))
|
||||||
await request.upload(url, file)
|
await request.upload(url, file)
|
||||||
|
|
||||||
|
await asyncio.wait([fn(i) for i in self.data.values()])
|
||||||
|
|
||||||
async def save(self):
|
async def save(self):
|
||||||
c = sync_image(self.bake())
|
c = sync_image(self.bake())
|
||||||
self.__set_id(c['_id'])
|
self.__set_id(c['_id'])
|
||||||
@@ -142,6 +147,7 @@ async def create_image(file: str, sizes: [()], url_fn, output_dir: str) -> Optio
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
_, ext = os.path.splitext(file)
|
_, ext = os.path.splitext(file)
|
||||||
|
images = []
|
||||||
|
|
||||||
async def fn(size):
|
async def fn(size):
|
||||||
size_name, width, height = size
|
size_name, width, height = size
|
||||||
@@ -157,18 +163,16 @@ async def create_image(file: str, sizes: [()], url_fn, output_dir: str) -> Optio
|
|||||||
return None
|
return None
|
||||||
width, height = image.size
|
width, height = image.size
|
||||||
|
|
||||||
return {
|
images.append({
|
||||||
'file': local_image_path,
|
'file': local_image_path,
|
||||||
'url': image_url,
|
'url': image_url,
|
||||||
'size': size_name,
|
'size': size_name,
|
||||||
'width': width,
|
'width': width,
|
||||||
'height': height
|
'height': height
|
||||||
}
|
})
|
||||||
|
|
||||||
|
await asyncio.wait([fn(s) for s in sizes])
|
||||||
|
|
||||||
images = []
|
|
||||||
for s in sizes:
|
|
||||||
img = await fn(s)
|
|
||||||
images.append(img)
|
|
||||||
return images
|
return images
|
||||||
|
|
||||||
|
|
||||||
@@ -178,7 +182,7 @@ async def process_image(input_file, output_file, size):
|
|||||||
if size_name == 'original':
|
if size_name == 'original':
|
||||||
return await optimize(input_file, output_file, quality=90)
|
return await optimize(input_file, output_file, quality=90)
|
||||||
else:
|
else:
|
||||||
image = read_image(input_file)
|
image = await read_image(input_file)
|
||||||
if not image:
|
if not image:
|
||||||
return None
|
return None
|
||||||
image_width, image_height = image.size
|
image_width, image_height = image.size
|
||||||
@@ -187,28 +191,23 @@ async def process_image(input_file, output_file, size):
|
|||||||
return await thumbnail(input_file, output_file, (width, height), quality=90)
|
return await thumbnail(input_file, output_file, (width, height), quality=90)
|
||||||
|
|
||||||
|
|
||||||
def read_image(src):
|
async def read_image(src):
|
||||||
|
import aiofiles
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
import io
|
||||||
|
|
||||||
try:
|
async with aiofiles.open(src, mode='rb') as f:
|
||||||
i = Image.open(src)
|
image_data = await f.read()
|
||||||
return i
|
image = Image.open(io.BytesIO(image_data))
|
||||||
except Exception as e:
|
return image
|
||||||
print(src, e)
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
async def thumbnail(src, dest, size, quality):
|
async def thumbnail(src, dest, size, quality):
|
||||||
try:
|
|
||||||
await image_magick_resize(src, dest, size, quality)
|
await image_magick_resize(src, dest, size, quality)
|
||||||
return read_image(dest)
|
return await read_image(dest)
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
async def optimize(src, dest, quality):
|
async def optimize(src, dest, quality):
|
||||||
image = read_image(src)
|
image = await read_image(src)
|
||||||
if image:
|
|
||||||
image.save(dest, quality=quality)
|
image.save(dest, quality=quality)
|
||||||
return image
|
return image
|
||||||
Reference in new issue
Block a user