Recently, I started reading image processing via opencv library in python.
I create a minio object storage and upload some photos into it. After that, I wrote a program which reads every image in each bucket in minio and watermark it with a simple logo. I read each image with
imread and finally (after inserting a watermark), save it with
imwrite (on the same image).
But when testing my code, I saw that for each image it took about 0.4 seconds. Since I want to do this simple operation for a lot of images, this time is not acceptable.
Here is a part of my simple code:
import cv2 import numpy as np logo_img = cv2.imread('logo.png', cv2.IMREAD_UNCHANGED) scl = 50 w = int(logo_img.shape * scl / 100) h = int(logo_img.shape * scl / 100) dim = (w, h) logo = cv2.resize(logo_img, dim, interpolation=cv2.INTER_AREA) logo_height, logo_width = logo.shape[:2] def watermark_image(image_name): image = cv2.imread(image_name) image_height, image_width = image.shape[:2] image = np.dstack([image, np.ones((image_height, image_width), dtype="uint8") * 255]) # Blend ovr = np.zeros((image_height, image_width, 4), dtype="uint8") x_pos = int(random() * (image_height - 10 - logo_width)) y_pos = int(random() * (image_width - 10 - logo_height)) ovr[x_pos:x_pos + logo_height, y_pos:y_pos + logo_width] = logo image = cv2.addWeighted(ovr, 0.6, image, 1.0, 0, image) cv2.imwrite(image_name, image) import time start = time.time() for bucket in buckets: objects = minioClient.list_objects(bucket.name, prefix=None, recursive=True) for current_object in objects: watermark_image('./test/' + current_object.object_name) end = time.time() print(end - start)
How can I improve my code and optimize it in order to need less runtime?
Suppose that I have a object storage which contains a lot of image. Actually I want to prepare an API, for people to request a watermark for every image and then I want to show them requested image after watermarking it online.