Ask Your Question

imwrite with unicode path Windows

asked 2016-09-26 09:57:42 -0500

xczhang1024 gravatar image


Is there any way in Windows MFC to give a non-ascii file name to imwrite?

cv::String appears to be internally represented by char*and Unicode in Windows is UTF-16. Is there a way to convert an UTF-16 string to UTF-8 and put it in a char array? I thought chars in Windows only represents the ascii set?

I'm not the best at encoding so I'm wondering if anyone else has solved this


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-09-26 10:35:07 -0500

berak gravatar image

updated 2016-09-26 11:39:34 -0500

No, you cannot use non-ascii filenames with imwrite (or imread).

Is there a way to convert an UTF-16 string to UTF-8 and put it in a char array?

this sounds quite promising. since it is a windows-only problem, you could use WideCharToMultiByte

edit flag offensive delete link more


When I use WideCharToMultiByte, it does not retain the correct string and on passing it to imwrite, it writes the file with the incorrectly converted string. The resulting string has strange symbols

Here is the code I am trying to use. ws is a std::wstring, and strTo does not get the correct value.

int size = WideCharToMultiByte( CP_UTF8, 0, &ws[0], (int)ws.size(), NULL, 0, NULL, NULL );
std::string strTo( size, 0 );
WideCharToMultiByte( CP_UTF8, 0, &ws[0], (int)ws.size(), &strTo[0], size, NULL, NULL );
xczhang1024 gravatar imagexczhang1024 ( 2016-09-27 03:51:14 -0500 )edit

wrong code page ?

berak gravatar imageberak ( 2016-09-27 03:58:26 -0500 )edit

How do I know which code page I need? And does this have anything to do with the current language of my Windows install?

xczhang1024 gravatar imagexczhang1024 ( 2016-09-27 04:03:51 -0500 )edit

I worked out a way around it using imencode to send the image to a vector of uchar and wrote that to file using ostream in my unicode compiled project.

I should have known really because I used imdecode to do the opposite for imread :)

xczhang1024 gravatar imagexczhang1024 ( 2016-09-27 06:43:46 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-09-26 09:57:42 -0500

Seen: 1,713 times

Last updated: Sep 26 '16