2020-12-29

Cannot convert to PIL from OpenCV after performing image overlay

I am taking a PIL image, converting it to OpenCV, performing an image overlay and then trying to return it as a PIL image. Usually, this works for me but now I am getting an unfamiliar error. My goal is to overlay a red box on top of the image. This is my code:

def overlay(path, lg_x, lg_y):
    img = cv2.imread(path, cv2.IMREAD_UNCHANGED)

    overlay = img.copy()
    output = img.copy()

    cv2.rectangle(overlay, (0, 0), (lg_x, lg_y), (0, 0, 255), -1)

    alpha = 0.3

    result = cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, output)

    pil_image = Image.fromarray(result)

    return pil_image

And this is the error:

Traceback (most recent call last):
  File "/home/image-augmentation.py", line 152, in <module>
    new_logo = overlay(logo_path, brand_logo_width, brand_logo_height)
  File "/home/image-augmentation.py", line 85, in overlay
    pil_image = Image.fromarray(result)
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2682, in fromarray
    raise TypeError("Cannot handle this data type: %s, %s" % typekey)
TypeError: Cannot handle this data type: (1, 1, 4), <u2


from Recent Questions - Stack Overflow https://ift.tt/2L2Z138
https://ift.tt/eA8V8J

No comments:

Post a Comment