fixed image writing bug