Write image files in both GUI and non-GUI cases