general code to process images