[GH-ISSUE #301] How to convert opencv Mat data to image_buffer? #242

Closed
opened 2026-05-05 12:01:10 -06:00 by gitea-mirror · 5 comments
Owner

Originally created by @simon5u on GitHub (Aug 10, 2020).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/301

Originally assigned to: @jmcnamara on GitHub.

Hi all, I am using opencv to read the frame from video. I want to output the frame into excel but I received the following error "[WARNING]: worksheet_insert_image()/_opt: couldn't read image type for: image_buffer.". Following is my code:-

        Mat full = cv::imread("test.png");
        int fsize = full.total() * full.elemSize();
        unsigned char * fdata = new uchar[fsize];
        memcpy(fdata, full.data, fsize * sizeof(unsigned char));
        worksheet_insert_image_buffer(worksheet, 1, 3, fdata, fsize * sizeof(unsigned char));

Please help. Thanks.

Originally created by @simon5u on GitHub (Aug 10, 2020). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/301 Originally assigned to: @jmcnamara on GitHub. Hi all, I am using opencv to read the frame from video. I want to output the frame into excel but I received the following error "[WARNING]: worksheet_insert_image()/_opt: couldn't read image type for: image_buffer.". Following is my code:- ``` Mat full = cv::imread("test.png"); int fsize = full.total() * full.elemSize(); unsigned char * fdata = new uchar[fsize]; memcpy(fdata, full.data, fsize * sizeof(unsigned char)); worksheet_insert_image_buffer(worksheet, 1, 3, fdata, fsize * sizeof(unsigned char)); ``` Please help. Thanks.
gitea-mirror 2026-05-05 12:01:10 -06:00
Author
Owner

@jmcnamara commented on GitHub (Aug 10, 2020):

That warning usually means that the data doesn't contain any headers that indicate that the file is a known image format.

Can you add the image to this issue on GitHub. Also can you print out the size of fsize to see if it equates to the byte size of the file.

Also, it would be good to attach a full working example to avoid any confusion.

<!-- gh-comment-id:671281896 --> @jmcnamara commented on GitHub (Aug 10, 2020): That warning usually means that the data doesn't contain any headers that indicate that the file is a known image format. Can you add the image to this issue on GitHub. Also can you print out the size of `fsize` to see if it equates to the byte size of the file. Also, it would be good to attach a full working example to avoid any confusion.
Author
Owner

@simon5u commented on GitHub (Aug 10, 2020):

Thanks for the reply. I am using ubuntu16, gcc5.4.0, opencv2.4.9, libxlsxwriter and codeblocks. Attached is the code I am trying. Following is the main.cpp. Please help.

===

#include <sys/stat.h>
#include "xlsxwriter.h"
#include "opencv2/opencv.hpp"

int main() {

/* Create a new workbook and add a worksheet. */
lxw_workbook  *workbook  = workbook_new("output.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, "test");

/* Add a format. */
/* Set the normal text property */
lxw_format *normal_format = workbook_add_format(workbook);
format_set_text_wrap(normal_format);
format_set_align(normal_format, LXW_ALIGN_VERTICAL_TOP);

/* Set the bold property for the format */
lxw_format *bold_format = workbook_add_format(workbook);
format_set_bold(bold_format);
format_set_text_wrap(bold_format);
format_set_align(bold_format, LXW_ALIGN_LEFT);
format_set_align(bold_format, LXW_ALIGN_VERTICAL_TOP);


/* Configure a format for the merged range. */
lxw_format *text_wrap = workbook_add_format(workbook);
format_set_bold(text_wrap);
format_set_text_wrap(text_wrap);
format_set_align(text_wrap, LXW_ALIGN_LEFT);
format_set_align(text_wrap, LXW_ALIGN_VERTICAL_TOP);

/* Image scale option */
//lxw_image_options image_scale = {.x_scale  = 0.5, .y_scale  = 0.5};

/* Change the column width for clarity. */
worksheet_set_column(worksheet, 1, 2, 25, NULL);

/* Change the row width for clarity. */
worksheet_set_row(worksheet, 0, 45, NULL);

/* Write some simple text. */
worksheet_write_string(worksheet, 0, 0, "S/No", bold_format);
worksheet_write_string(worksheet, 0, 1, "Image Name", bold_format);
worksheet_write_string(worksheet, 0, 2, "Captured Full Image", text_wrap);

/* Text without formatting. */
worksheet_set_row(worksheet, 1, 150, NULL);
worksheet_write_string(worksheet, 1, 0, "1", normal_format);
worksheet_write_string(worksheet, 1, 1, "Sample.avi", normal_format);

/* Insert an image. */
//worksheet_insert_image(worksheet, 1, 2, "logo.png"); // this is working fine
cv::Mat full = cv::imread("logo.png");
int fsize = full.total() * full.elemSize();
unsigned char * fdata = new uchar[fsize];
memcpy(fdata, full.data, fsize * sizeof(unsigned char));
worksheet_insert_image_buffer(worksheet, 1, 3, fdata, fsize);
printf("%d %d\n", fsize, fsize * sizeof(unsigned char));

workbook_close(workbook);

std::cout << "completed!";

return 0;

}

xlsx_example.tar.gz

<!-- gh-comment-id:671382713 --> @simon5u commented on GitHub (Aug 10, 2020): Thanks for the reply. I am using ubuntu16, gcc5.4.0, opencv2.4.9, libxlsxwriter and codeblocks. Attached is the code I am trying. Following is the main.cpp. Please help. === #include <sys/stat.h> #include "xlsxwriter.h" #include "opencv2/opencv.hpp" int main() { /* Create a new workbook and add a worksheet. */ lxw_workbook *workbook = workbook_new("output.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, "test"); /* Add a format. */ /* Set the normal text property */ lxw_format *normal_format = workbook_add_format(workbook); format_set_text_wrap(normal_format); format_set_align(normal_format, LXW_ALIGN_VERTICAL_TOP); /* Set the bold property for the format */ lxw_format *bold_format = workbook_add_format(workbook); format_set_bold(bold_format); format_set_text_wrap(bold_format); format_set_align(bold_format, LXW_ALIGN_LEFT); format_set_align(bold_format, LXW_ALIGN_VERTICAL_TOP); /* Configure a format for the merged range. */ lxw_format *text_wrap = workbook_add_format(workbook); format_set_bold(text_wrap); format_set_text_wrap(text_wrap); format_set_align(text_wrap, LXW_ALIGN_LEFT); format_set_align(text_wrap, LXW_ALIGN_VERTICAL_TOP); /* Image scale option */ //lxw_image_options image_scale = {.x_scale = 0.5, .y_scale = 0.5}; /* Change the column width for clarity. */ worksheet_set_column(worksheet, 1, 2, 25, NULL); /* Change the row width for clarity. */ worksheet_set_row(worksheet, 0, 45, NULL); /* Write some simple text. */ worksheet_write_string(worksheet, 0, 0, "S/No", bold_format); worksheet_write_string(worksheet, 0, 1, "Image Name", bold_format); worksheet_write_string(worksheet, 0, 2, "Captured Full Image", text_wrap); /* Text without formatting. */ worksheet_set_row(worksheet, 1, 150, NULL); worksheet_write_string(worksheet, 1, 0, "1", normal_format); worksheet_write_string(worksheet, 1, 1, "Sample.avi", normal_format); /* Insert an image. */ //worksheet_insert_image(worksheet, 1, 2, "logo.png"); // this is working fine cv::Mat full = cv::imread("logo.png"); int fsize = full.total() * full.elemSize(); unsigned char * fdata = new uchar[fsize]; memcpy(fdata, full.data, fsize * sizeof(unsigned char)); worksheet_insert_image_buffer(worksheet, 1, 3, fdata, fsize); printf("%d %d\n", fsize, fsize * sizeof(unsigned char)); workbook_close(workbook); std::cout << "completed!"; return 0; } [xlsx_example.tar.gz](https://github.com/jmcnamara/libxlsxwriter/files/5051323/xlsx_example.tar.gz)
Author
Owner

@jmcnamara commented on GitHub (Aug 10, 2020):

I compiled and ran your example.

The worksheet_insert_image_buffer() function works on a buffer of image data. However, the cv::MAT appears to be a matrix representation of the pixels in the file.

You need to be able to convert this into the raw buffer format for the PNG file or use some simpler read mechanism. The imread() function probably isn't the right way to do what you need to do.

I don't use OpenCV so I can't really help you with this. It is more of a general question than a Libxlsxwriter question. As such it may be better to ask it on StackOverflow.

Also, if you are reading from video you will need to ensure that the frame data is actually in PNG (or a supported format). You can't insert a raw H.264 or other video frame into Excel.

<!-- gh-comment-id:671443785 --> @jmcnamara commented on GitHub (Aug 10, 2020): I compiled and ran your example. The `worksheet_insert_image_buffer()` function works on a buffer of image data. However, the cv::MAT appears to be a matrix representation of the pixels in the file. You need to be able to convert this into the raw buffer format for the PNG file or use some simpler read mechanism. The imread() function probably isn't the right way to do what you need to do. I don't use OpenCV so I can't really help you with this. It is more of a general question than a Libxlsxwriter question. As such it may be better to ask it on StackOverflow. Also, if you are reading from video you will need to ensure that the frame data is actually in PNG (or a supported format). You can't insert a raw H.264 or other video frame into Excel.
Author
Owner

@simon5u commented on GitHub (Aug 11, 2020):

Thanks for the feedback. Here is the solution I found:-

    /* Insert an image. */
    cv::Mat img = cv::imread("logo.png");
    // encode image into jpg
    cv::vector<uchar> buf;
    cv::imencode(".png", img, buf, std::vector<int>() );
    // encoded image is now in buf (a vector)
    unsigned char *imageBuf = new unsigned char [buf.size()];
    memcpy(imageBuf, &buf[0], buf.size());
    //  size of imageBuf is buf.size();
    worksheet_insert_image_buffer(worksheet, 1, 2, imageBuf, buf.size());
    workbook_close(workbook);
    delete[] imageBuf;
<!-- gh-comment-id:671684181 --> @simon5u commented on GitHub (Aug 11, 2020): Thanks for the feedback. Here is the solution I found:- ``` /* Insert an image. */ cv::Mat img = cv::imread("logo.png"); // encode image into jpg cv::vector<uchar> buf; cv::imencode(".png", img, buf, std::vector<int>() ); // encoded image is now in buf (a vector) unsigned char *imageBuf = new unsigned char [buf.size()]; memcpy(imageBuf, &buf[0], buf.size()); // size of imageBuf is buf.size(); worksheet_insert_image_buffer(worksheet, 1, 2, imageBuf, buf.size()); workbook_close(workbook); delete[] imageBuf; ```
Author
Owner

@jmcnamara commented on GitHub (Aug 11, 2020):

Thanks for letting me know. Someone else may find that useful.

<!-- gh-comment-id:671882209 --> @jmcnamara commented on GitHub (Aug 11, 2020): Thanks for letting me know. Someone else may find that useful.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/libxlsxwriter#242
No description provided.