打开 jpg 图像时更改颜色



我首先将此图像保存到本地计算机: https://i.stack.imgur.com/uS6CB.jpg

然后我尝试打开并使用以下代码显示它:

from PIL import Image
img = Image.open('gipsy1.jpg')
img.show()

通过显示(或保存到新文件中(的图像与原始文件略有不同,因为可以在此处检查:https://i.stack.imgur.com/LmtWG.jpg。有什么办法可以避免这种情况吗?

有关我的环境的一些信息:

    Python 2.7.10
  • (默认,2017 年 2 月 7 日,00:08:15([GCC 4.2.1兼容的苹果LLVM 8.0.0 (clang-800.0.34(]
  • 枕头 4.1.1
  • Mac OS Sierra 10.12.5 (16F73(

记录:此图像使用 ICC Display P3 空间,仅在新的 Apple 产品和其他一些高端显示器上受支持。如果我们强制ICC使用sRGB,我们会得到更好的结果。

以下代码工作正常:

from PIL import Image
img = Image.open('gipsy1.jpg')
import tempfile
from PIL import ImageCms
icc = tempfile.mkstemp(suffix='.icc')[1]
with open(icc, 'w') as f:
f.write(img.info.get('icc_profile'))
srgb = ImageCms.createProfile('sRGB')
img = ImageCms.profileToProfile(img, icc, srgb)
img.save('new_srgb_gipsy1.jpg')

最新更新