如何使用python模仿在图像编辑器中打开和保存图像文件? [英] How to use python to imitate the opening and saving of an image file in an image editor?

查看:176
本文介绍了如何使用python模仿在图像编辑器中打开和保存图像文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了损坏的.png图像文件*的某些问题,只需在图像编辑器中打开它们即可解决(Apple的

I encountered a certain problem with corrupt .png image files* that I was able to overcome by simply opening them in an image editor (Apple's Preview) and saving them back, without making any explicit changes.

我有成千上万个类似损坏的图像文件,需要执行相同的步骤.有没有办法在python中自动化它?

I have thousands of similarly corrupt image files that need to be subjected to the same procedure. Is there a way to automate it in python?

以下朴素的代码不起作用:

The following naive code doesn't work:

>>> with open('an_image.png', 'rb') as f:
...     data = f.read()  
... with open('an_image.png', 'wb') as f:
...     f.write(data)  

为什么此代码不起作用?图像编辑器可以做什么来解决问题,我该如何以编程方式模仿它?

Why doesn't this code work? What does the image editor do that corrects the problem, and how can I imitate it programmatically?

我的操作系统是macOS Sierra版本10.12.4.
我的python版本是3.6.1.

My operating system is macOS Sierra Version 10.12.4.
My python version is 3.6.1.

此处是其中一个损坏文件的示例.

Here is an example of one of the corrupt files.

*对于感兴趣的人,原始问题在此处描述.

* For those interested, the original problem is described here.

推荐答案

您的示例文件 https ://i.stack.imgur.com/a5uXX.png 实际上不是PNG文件.它是"CgBI"文件,它是由Apple X-代码软件,可能使用他们的流氓"pngcrush"应用程序.有多种方法可以从CgBI文件中恢复有效的PNG文件(除了不可避免的非透明像素的基础颜色值精度损失外).

Your example file, https://i.stack.imgur.com/a5uXX.png, isn't actually a PNG file. It's a "CgBI" file instead, which is a PNG-like file created by Apple X-code software, probably using their rogue "pngcrush" application. There are various ways of recovering a valid PNG file from a CgBI file (except for unavoidable loss of precision in the underlying color values of non-opaque pixels).

  • Apple修改的pngcrush本身具有功能 恢复"iPhone优化"

  • The Apple-modified pngcrush is itself capable of reverting the "iPhone optimizations"

有第三方应用程序,例如公共领域,开源 pngdefry 应用程序 可以在网上找到.

There are third-party applications such as the public domain, open source pngdefry application that can be found on the net.

道歉,没有向您展示如何通过python调用来调用pngdefry或pngcrush,但希望这能使您走上正确的轨道,并至少告诉您文件发生了什么事.

Apologies for not showing you how to invoke pngdefry or pngcrush via python calls, but hopefully this will set you on the right track, and at least tell you what has happened to your files.

法律声明:我需要注意Apple和iPhone 是Apple,Inc.的注册商标.

Legal Notice: I am required to note that Apple and iPhone are registered trademarks of Apple, Inc.

这篇关于如何使用python模仿在图像编辑器中打开和保存图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆