taichi icon indicating copy to clipboard operation
taichi copied to clipboard

Cannot write image on Windows when using tempfile.NamedTemporaryFile

Open Morcki opened this issue 2 years ago • 0 comments

To Reproduce

# sample code here
import taichi as ti
from tempfile import NamedTemporaryFile

ti.init(arch=ti.cuda)
image = ti.Vector.field(4, dtype=ti.f32, shape=(512, 512))

with NamedTemporaryFile(suffix=".png") as fp:
    ti.tools.imwrite(image, fp.name)

Log/Screenshots Running the code on Windows platform would report bug as below:

[taichi/util/image_io.cpp:imwrite@30] Cannot write image file [...]

It seems that it failed in this code[As below].


// imwrite -> stbi_write_png

#ifndef STBI_WRITE_NO_STDIO
STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, int stride_bytes)
{
   FILE *f;
   int len;
   unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len);
   if (png == NULL) return 0;
   f = fopen(filename, "wb");
   if (!f) { STBIW_FREE(png); return 0; }
   fwrite(png, 1, len, f);
   fclose(f);
   STBIW_FREE(png);
   return 1;
}

Morcki avatar Sep 01 '22 07:09 Morcki