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[1] * scl / 100)
h = int(logo_img.shape[0] * 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.

  • \$\begingroup\$ did you check whether most time was spent in the downloading/uploading, or the watermarking? \$\endgroup\$ Commented Aug 28, 2019 at 13:14
  • \$\begingroup\$ @maartenFabre most of time is for watermarking. i test it again and just for calling watermark, it is spent about 0.4 second, as seen before. \$\endgroup\$
    – cutename
    Commented Aug 28, 2019 at 13:20
  • 1
    \$\begingroup\$ How large are the images you are using to test this? \$\endgroup\$
    – AlexV
    Commented Aug 28, 2019 at 16:37

1 Answer 1


I'll focus more on the

How can I improve my code

part of your question.

Imports at the top

Move your import time to the top of the file, which is standard in Python.

Code in functions

Your logo dimension calculation should be in a function instead of global scope. The code at the bottom should be in a top-level main function.

Integer division

w = int(logo_img.shape[1] * scl / 100)

can be

w = logo_img.shape[1] * scl // 100

Use pathlib

...so that this:

'./test/' + current_object.object_name

can be

Path('test') / current_object.object_name
  • \$\begingroup\$ thank you but my means was improve duration time not style code. \$\endgroup\$
    – cutename
    Commented Aug 28, 2019 at 14:02
  • 2
    \$\begingroup\$ @cutename, by asking on Code Review, you should expect reviewers to assess any and all aspects of your code - askers don't get to constrain reviewers like that. \$\endgroup\$ Commented Aug 28, 2019 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.