【问题标题】:How to preserve RGB color while saving png using PIL?如何在使用 PIL 保存 png 时保留 RGB 颜色?
【发布时间】:2020-02-17 10:38:34
【问题描述】:

我编写了一个 python 程序,它将三个 png 图像组合成一个图像。我正在使用 PIL 打开、调整大小、合并和保存生成的图像。所有功能都在那里,但生成的图像具有与原始图像完全不同的颜色配置文件。

我尝试了几种选择:
1. 我尝试将新图像创建为“RGBA”
结果: TKinter GUI 不再显示图像
2. 尝试从原始图像复制颜色配置文件,然后在保存最终图像时使用该配置文件:
代码: profile = image.info.get("icc_profile", "") 然后我在使用参数保存文件时使用结果变量icc_profile = profile 结果:没有变化

最少的可重现代码

from PIL import Image as pImage
from tkinter.filedialog import asksaveasfilename

newImage = pImage.new('RGB', (976, 976))
background = pImage.open("Gameboy_Background.png")
screen_shot = pImage.open("screenshot.png")
cover_art = pImage.open("[coverart.png][1]")
newImage.paste(background)

w, h = screen_shot.size
newW = 875
newH = int(newW * h / w)
screen_shot = screen_shot.resize((newW, newH), pImage.ANTIALIAS)
newImage.paste(screen_shot, (50, 155))

w, h = cover_art.size
newW = 175
newH = int(newW * h / w)
cover_art = cover_art.resize((newW, newH), pImage.ANTIALIAS)
newImage.paste(cover_art, (100, 205))

file2Save = asksaveasfilename(initialdir="/", title="Select file", filetypes={("PNG files", "*.png")})
newImage.save(file2Save + ".png", "PNG")

使用的 PNG 图片
1: https://i.stack.imgur.com/Lj1wo.png [2]:https://i.stack.imgur.com/4iauQ.png [3]:https://i.stack.imgur.com/2voFC.png

Resulting Image

【问题讨论】:

  • 您能详细说明您的profile = image.info.get("icc_profile", "") 代码吗? image 来自哪里,它与pImagebg 有什么关系? profile 创建后你会做什么?
  • 我不明白。您说您编写了组合 3 张 PNG 图像的代码,但它只打开一个 PNG 图像?此外,您还没有提供 3 张图片,而是提供了其中一张的屏幕截图,这可能完全不同 - 例如它可能是 BMP 格式图像的 PNG 格式抓取,所以如果没有太大帮助。谢谢。
  • @Kevin 我已经编辑了帖子。保存图像时,我尝试使用生成的配置文件作为修饰符。
  • 我仍然不清楚image 是什么。你能提供一个minimal reproducible example吗?
  • @Kevin 我提供了一个可重现的例子。我还删除了令人困惑的图像。然而,之前生成的图像是在屏幕上并排打开的两个 PNG 文件的屏幕截图。图片 B 是用于生成图片 A 的原始 PNG。

标签: python python-imaging-library color-profile


【解决方案1】:

profile = image.info.get("icc_profile", "") 然后我在使用参数icc_profile = profile 保存文件时使用结果变量

实际上,这听起来对我来说是正确的方法。 image是截图吧?那就是您要复制其个人资料的人。

from PIL import Image as pImage
from tkinter.filedialog import asksaveasfilename

newImage = pImage.new('RGB', (976, 976))
background = pImage.open("Gameboy_Background.png")
screen_shot = pImage.open("screenshot.png")
cover_art = pImage.open("coverart.png")
newImage.paste(background)

profile = screen_shot.info.get("icc_profile", "")

w, h = screen_shot.size
newW = 875
newH = int(newW * h / w)
screen_shot = screen_shot.resize((newW, newH), pImage.ANTIALIAS)
newImage.paste(screen_shot, (50, 155))

w, h = cover_art.size
newW = 175
newH = int(newW * h / w)
cover_art = cover_art.resize((newW, newH), pImage.ANTIALIAS)
newImage.paste(cover_art, (100, 205))

file2Save = "output"
newImage.save(file2Save + ".png", "PNG", icc_profile=profile)

结果:

【讨论】:

  • 赞成,因为在问题中存在模糊和模棱两可,并且问题中没有结果图像的情况下,您制作了一个。干得好!
  • 成功了!至少在最小的可重现代码中。我对完整程序执行了相同的程序,但也许我复制了错误的配置文件。我还复制了获取配置文件的代码。它使用单引号 ( ' ' ),如 ('icc_profile', ' ')。当我将代码复制到 stackOverflow 时,我“更正”了它。
  • 这就是解决方案。显然我选择了错误的图像来获取个人资料。我选择了背景的颜色配置文件,因为它在开始时静态加载。如果您有让问题标题更具体的建议,请告知。
【解决方案2】:

我认为打开并读取图像时会出现问题。尝试使用 tkinkter gui 打开和读取文件,然后使用 numpy 转换为数组。像这样的代码:

import tkinter as tk
from tkinter import filedialog
import numpy as np
//get ur extension as png file
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
images = Image.open(file_path)
image_data=np.asarray(image)
image_data = cv2.cvtColor(image_data, cv2.COLOR_BGR2RGB)

你在 image_data 变量中有你的 rgb 模式的图像保存它或将它用于进一步处理

【讨论】:

  • Bhavin Thakar 你能检查一下我添加到问题中的新的最小可重现代码并澄清你的修改将去哪里。谢谢。
猜你喜欢
  • 1970-01-01
  • 2020-01-05
  • 1970-01-01
  • 2013-08-17
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 2017-06-16
  • 1970-01-01
相关资源
最近更新 更多