Extract common image conversion code