打开jpg图像时更改颜色

2024-03-28 11:34:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我首先将此图像保存到本地计算机:http://imgur.com/w3uQ9Ra

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

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

show显示的图像(或保存到新文件中的图像)与原始文件略有不同,因为可以在这里检查:http://imgur.com/9TAPiqx。有什么办法可以避免这种情况吗?你知道吗

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

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

Tags: 文件代码from图像imageimportcomhttp
1条回答
网友
1楼 · 发布于 2024-03-28 11:34:35

请注意:这张图片使用的是ICC显示屏P3空间,只有苹果新产品和其他一些高端显示器才支持这种空间。如果我们强迫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')

相关问题 更多 >