CLImage icon indicating copy to clipboard operation
CLImage copied to clipboard

[REQUEST] Provide method work with opencv image array

Open bprabhus opened this issue 3 years ago • 4 comments

Provide a method to work with opencv image arrays. This enhancement will extend climage use cases where opencv facilitates in handling images from camera/webcam.

Snippet Conversion:

import cv2
from PIL import Image
  
img = cv2.imread("test.png")
cv_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
pil_img = Image.fromarray(cv_img)

Expected Method:

def convert_ocv(
    cv_img,
    is_unicode=False,
    is_truecolor=False,
    is_256color=True,
    is_16color=False,
    is_8color=False,
    width=80,
    palette="default",
)

bprabhus avatar Sep 06 '23 18:09 bprabhus

Thanks for the request, very clear what you're asking for.

I think it might be best to instead rely on the numpy api endpoint (which as you've discovered is pending in a PR, I'll fix and merge that tomorrow....life goes past fast!), which is probably the most general format when working with images.

Opencv does use numpy internally, so that would be fine. I haven't got opencv installed on the machine I'm typing this out on, so I'm relying on here: https://stackoverflow.com/questions/7762948/how-to-convert-an-rgb-image-to-numpy-array

I'll mark this as closed when I merge #6. Apologies for the delay!

pnappa avatar Sep 07 '23 00:09 pnappa

numpy works for me. Thank you for looking into the issue.

bprabhus avatar Sep 07 '23 04:09 bprabhus

Addressed by #6 in climage 0.2.0.

You'll need to update to get this: pip3 install --upgrade climage.

Example usage:

import cv2
img = cv2.imread('/home/pnappa/github.com/pnappa/climage/teapot.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
resized = cv2.resize(img, (50, 50))
print(climage.convert_array(resized, **climage.color_to_flags(climage.color_types.truecolor)))

# Or,
# Note: need to trim off the final row of pixels, if using unicode when there's a odd number of pixels, the following code does that, but isn't needed (it's 50px tall).
converted = climage.convert_array(resized[:len(resized)//2 * 2], is_unicode=True, **climage.color_to_flags(climage.color_types.truecolor))
print(converted)

pnappa avatar Sep 08 '23 09:09 pnappa

Works like a charm.. Thank you for the release!

bprabhus avatar Sep 08 '23 18:09 bprabhus