fix bugs in writing images

Signed-off-by: yuhuazou <yuhuazou@gmail.com>