Multi page imwrite for Python [closed]

asked 2019-02-24 00:47:54 -0600

scossu gravatar image

updated 2019-02-24 09:48:42 -0600

Hi there, I gladly saw that a feature allowing to save multi-page images was introduced about a year ago: https://github.com/opencv/opencv/pull...

However I don't see this working in Python:

>>> import cv2
>>> im = cv2.imread('/tmp/test.tif')
>>> im2 = cv2.imread('/tmp/thumb.tif')
>>> cv2.imwrite([im, im2], '/tmp/opencv_multi.tif')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: bad argument type for built-in operation
>>> help(cv2.imwrite)
imwrite(...)
    imwrite(filename, img[, params]) -> retval
[...]

Is a Python port foreseen? Or am I just using the wrong syntax?

Thanks.

Edit: As pointed out in the comment, the above snippet gave me a different error because of the wrong signature. With the "correct" signature I still get a TypeError though:

>>> import cv2
>>> im = cv2.imread('/tmp/test.tif')
>>> im2 = cv2.imread('/tmp/thumb.tif')
>>> cv2.imwrite([im, im2], '/tmp/opencv_multi.tif')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Expected cv::UMat for argument 'img'
edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-11-28 08:04:53.471762

Comments

please try to read the original images as grayscale, single channel: im = cv2.imread('/tmp/test.tif', cv2.IMREAD_GRAYSCALE) , else they're forced into 3channel, bgr mode,

then, you got filename and images in reverse ..., please look at the docs, again (you're even printing it out, still !!!)

berak gravatar imageberak ( 2019-02-24 01:47:22 -0600 )edit

My bad. I pasted the wrong line. See update, which gives the error I am talking about. The point is that I don't see anything in the help that indicates a list of images to be passed.

I don't understand your point about grayscale... my image is RGB, why do I need it to open it as grayscale?

scossu gravatar imagescossu ( 2019-02-24 09:51:23 -0600 )edit

sorry, ignore the grayscale, i probably misread it.

but a vector<Mat> is a 2d numpy array (with a single column) so it likely (i havent's tried !) goes like:

cv2.imwrite(np.array[[im, im2]],np.uint8), '/tmp/opencv_multi.tif')
berak gravatar imageberak ( 2019-02-25 01:38:19 -0600 )edit