Pixmap.tobytes, from Bytes to numpy.ndarray #1208
-
Hi, I work with Bytes object from Pixmap.tobytes and I want to convert it to a jpeg format. Actually, I use io.BytesIO, then PIL.Image.open to save the output in the desired format ( I use Using PIL might be inefficient, so I want to use OpenCV with cv2. But when I tried to create an image (numpy.ndarray I think) that cv2 can handle. I have errors like
Maybe I can use imread from cv2 but it read from a file, is there a good solution to pass the bytes pymupdf create to this function ? Do you have any suggestion? Did I miss something ? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
I found a solution. Here is my workaround: I must convert the bytes object to a numpy.bytearray. cv2_image = imdecode(numpy.frombuffer(bytearray(raw_bytes), dtype=numpy.uint8), IMREAD_COLOR) |
Beta Was this translation helpful? Give feedback.
-
The simplest thing to do is |
Beta Was this translation helpful? Give feedback.
-
only using the numpy.frombuffer is more efficient: from https://blog.csdn.net/qq_36998298/article/details/124177620 |
Beta Was this translation helpful? Give feedback.
-
I think So whenever a consumer of pixmaps can digest memoryviews or even Python pointers, they should definitely choose these alternatives. |
Beta Was this translation helpful? Give feedback.
The simplest thing to do is
Pixmap.pil_tobytes(...)
. This offers you using all Pillow arguments, e.g. creating a bytes object in JPEG format is as simple aspix.pil_tobytes("JPEG")
. There you could add all the goodies like "optimize" and what not.