Fix empty images getting written

Fixes #163, #184 and #187.
Merge pull request #188 from kevva/fix

Fix empty images getting written