Code valid for all image dtypes