import os import sys import time import datetime import json from PIL import Image import cv2 as cv import numpy as np from app.utils.im_utils import ensure_np, ensure_pil from flask import current_app as app from app.server.tasks import celery from celery.utils.log import get_task_logger celery_logger = get_task_logger(__name__) import imutils @celery.task(bind=True) def blur_task(self, uuid_name, extra): """Process image and update during""" celery_logger.debug('process_image_task, uuid: {}'.format(uuid_name)) files = [] im = Image.open(os.path.join(upload_dir, uuid_name + '.jpg')).convert('RGB') im = im.resize((256,256)) files.append({ 'title': 'Original image', 'fn': upload_uri + uuid_name + '.jpg' }) self.update_state( state = 'PROCESSING', meta = { 'percent': 0.25, 'message': 'Applying blur', 'uuid': uuid_name }) im_np = ensure_np(im) im_blur = cv.blur(im_np, (5,5), 1.0) im_blur_pil = ensure_pil(im_blur) fn = uuid_name + '_blur.jpg' fpath = os.path.join(render_dir, fn) im_blur_pil.save(fpath, 'JPEG', quality=95) files.append({ 'title': 'Blurred image', 'fn': render_uri + uuid_name + '_blur.jpg' }) time.sleep(3) self.update_state( state = 'PROCESSING', meta = { 'percent': 0.75, 'message': 'Sleeping some more', 'uuid': uuid_name }) time.sleep(2) data = { 'uuid': uuid_name, 'date': str(datetime.datetime.now()), 'files': files } json_path = os.path.join(json_dir, uuid_name + '.json') with open(json_path, 'w') as json_file: json.dump(data, json_file) celery_logger.debug('ok') return { 'percent': 100, 'state': 'complete', 'uuid': uuid_name, }