Port PIL-based code to Pillow