Only decrement the image counter if it is set and larger than 0

Backported from devel