资源简介
Apple 对 iPhone 应用程序中的 png 图片进行了特殊的处理,在 png 文件头之后加了一个非标准的 CgBI 数据段,IDAT 段图像数据也没有传统的压缩数据头和尾,并且红色和蓝色是反的,这样就无法在 Mac 或 Windows 正常使用了。将本python文件放于导出的png文件目录下,并执行python ipin.py,可用将png图片还原正常。
代码片段和文件信息
#---
# iPIN - iPhone PNG Images Normalizer v1.0
# Copyright (C) 2007
#
# Author:
# Axel E. Brzostowski
# http://www.axelbrz.com.ar/
# axelbrz@gmail.com
#
# References:
# http://iphone.fiveforty.net/wiki/index.php/PNG_Images
# http://www.libpng.org/pub/png/spec/1.2/PNG-Contents.html
#
# Changes:
# 2011.6.14. handle cases of multiple IDAT chunks
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation either version 3 of the License.
#
# This program is distributed in the hope that it will be useful
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#---
from struct import *
from zlib import *
import stat
import sys
import os
def getNormalizedPNG(filename):
pngheader = “\x89PNG\r\n\x1a\n“
file = open(filename “rb“)
oldPNG = file.read()
file.close()
if oldPNG[:8] != pngheader:
return None
newPNG = oldPNG[:8]
chunkPos = len(newPNG)
pendingIDATChunks = []
# For each chunk in the PNG file
while chunkPos < len(oldPNG):
# Reading chunk
chunkLength = oldPNG[chunkPos:chunkPos+4]
chunkLength = unpack(“>L“ chunkLength)[0]
chunkType = oldPNG[chunkPos+4 : chunkPos+8]
chunkData = oldPNG[chunkPos+8:chunkPos+8+chunkLength]
chunkCRC = oldPNG[chunkPos+chunkLength+8:chunkPos+chunkLength+12]
chunkCRC = unpack(“>L“ chunkCRC)[0]
chunkPos += chunkLength + 12
# Parsing the header chunk
if chunkType == “IHDR“:
width = unpack(“>L“ chunkData[0:4])[0]
height = unpack(“>L“ chunkData[4:8])[0]
# Parsing the image chunk
if chunkType == “IDAT“:
try:
# Uncompressing the image chunk
bufSize = width * height * 4 + height
if pendingIDATChunks:
chunkData = decompress( ‘‘.join(pendingIDATChunks) + chunkData -8 bufSize)
else:
chunkData = decompress( chunkData -8 bufSize)
except Exception e:
# The PNG image is normalized
pendingIDATChunks.append(chunkData)
continue
else:
pendingIDATChunks = []
# Swapping red & blue bytes for each pixel
newdata = ““
for y in xrange(height):
i = len(newdata)
newdata += chunkData[i]
for x in xrange(width):
i = len(newdata)
- 上一篇:线性回归做房价预测 python源码
- 下一篇:进程管理实验
相关资源
- 四行Python代码实现将word文件转换为
- QT文件转换成Python的自动化工具*.ui转
- month2day.py
- 栅格投影转换.py
- 根据epsg代号进行坐标的批量投影转换
- cpp-TBXtoPYT用于在ArcGIS101平台中将TBX文
- Arcgis中利用python语言把dbf文件批量转
- Python OpenCV 类型转换-
- python大漠转换
- python将pdf文件第一页转换成图片
- 可以把labelme标注的json文件批量转换为
- Python批量读取modis的hdf文件转asc文件
- python opencv 透视转换
- python利用百度地图API进行地理编码将
-
txt2xm
l,txt文件转换成xm l文件 - voc to yolo标注文件格式转换器
评论
共有 条评论