【问题标题】:PyQt5 - fromIccProfile: failed minimal tag size sanity errorPyQt5 - fromIccProfile:最小标签大小健全性错误失败
【发布时间】:2021-04-04 09:53:53
【问题描述】:

我正在使用最新的 PyQt5 5.12.2,对于我使用 QPixmap 或 QIcon 在脚本中显示的每张 JPG 图片,我都会收到一条奇怪的消息。

qt.gui.icc: fromIccProfile: failed minimal tag size sanity

它没有引起任何问题,并且脚本可以正常工作。问题是我试图同时显示大量 jpg 图片(作为照片库),因此在为每张照片打印所​​有消息之前,窗口会无响应。

我花了几个小时在网上寻找有用的东西,但不幸的是,似乎几乎没有人遇到同样的问题。我也在使用一些 PNG 文件,它们不会引发这个错误,所以我假设问题出在 jpg 格式上。我尝试使用较旧的 pyqt5 版本,但唯一的区别是它们没有打印消息,但问题仍然存在。

同时,我尝试使用此命令将这些消息静音,因为没有使用它们,但是即使没有在控制台中打印,窗口几秒钟无响应的问题仍然存在。

def handler(*args):
    pass
qInstallMessageHandler(handler)

编辑:我尝试将这些图像转换为 PNG,但错误仍然存​​在。所以JPG格式不是问题

【问题讨论】:

    标签: python pyqt5 jpeg icc


    【解决方案1】:

    我更深入地研究了 ICC 配置文件和色彩空间,看起来您的图片使用的色彩空间在某种程度上对于 PyQt 来说是非标准的。

    我的解决方案是将这些图片转换为经典的 ICC 配置文件,例如 sRGB。 这是一个示例函数:

    import io
    from PIL import Image, ImageCms
    def convert_to_srgb(file_path):
            '''Convert PIL image to sRGB color space (if possible)'''
            img = Image.open(file_path)
            icc = img.info.get('icc_profile', '')
            if icc:
                io_handle = io.BytesIO(icc)     # virtual file
                src_profile = ImageCms.ImageCmsProfile(io_handle)
                dst_profile = ImageCms.createProfile('sRGB')
                img_conv = ImageCms.profileToProfile(img, src_profile, dst_profile)
                icc_conv = img_conv.info.get('icc_profile','')
            if icc != icc_conv:
                # ICC profile was changed -> save converted file
                img_conv.save(file_path,
                            format = 'JPEG',
                            quality = 50,
                            icc_profile = icc_conv)
    

    使用 PIL 库是正确解决该错误的一种快速有效的方法。

    【讨论】:

      【解决方案2】:

      我正在使用 Pyside2 制作一个 GUI 图像查看器,并且遇到了类似的问题。 图像加载正常,就我而言,没有性能问题,但我不断收到这些 ICC 警告。 而且我不想修复原始文件,因为我的应用应该只是一个查看器。

      我不知道这对您的情况有帮助,但我的解决方案是首先使用 pillow ImageQT module 加载图像

      from pathlib import Path
      from PIL.ImageQt import ImageQt
      
      def load_image(path):
          if Path(path).is_file():
              return ImageQt(path)
      

      然后在显示图像的 QT Widget 类中,我将此图像加载到一个空的 QPixmap 上:

      def on_change(self, path):
          pixmap = QtGui.QPixmap()
          image = load_image(path)
          if image:
              pixmap.convertFromImage(image)
          if pixmap.isNull():
              self.display_area_label.setText('No Image')
          else:
              self.display_area_label.setPixmap(pixmap)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-26
        • 2012-10-02
        • 2017-06-12
        • 1970-01-01
        • 2022-07-07
        • 1970-01-01
        • 2019-06-27
        • 1970-01-01
        相关资源
        最近更新 更多