WEB开发网
开发学院手机开发iPhone 开发 将iPhone的png图片转换为可读png图片的Python源码... 阅读

将iPhone的png图片转换为可读png图片的Python源码

 2010-06-19 18:34:00 来源:WEB开发网   
核心提示:chunkCRC = unpack(">L", chunkCRC)[0]chunkPos += chunkLength + 12# Parsing the header chunkif chunkType == "IHDR":width = unpack(">

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

chunkData = decompress( chunkData, -8, bufSize)

except Exception, e:

# The PNG image is normalized

return None

# 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)

newdata += chunkData[i+2]

newdata += chunkData[i+1]

newdata += chunkData[i+0]

newdata += chunkData[i+3]

# Compressing the image chunk

chunkData = newdata

chunkData = compress( chunkData )

chunkLength = len( chunkData )

chunkCRC = crc32(chunkType)

chunkCRC = crc32(chunkData, chunkCRC)

chunkCRC = (chunkCRC + 0x100000000) % 0x100000000

# Removing CgBI chunk

if chunkType != "CgBI":

newPNG += pack(">L", chunkLength)

newPNG += chunkType

if chunkLength > 0:

newPNG += chunkData

newPNG += pack(">L", chunkCRC)

# Stopping the PNG file parsing

if chunkType == "IEND":

break

return newPNG

def updatePNG(filename):

data = getNormalizedPNG(filename)

if data != None:

file = open(filename, "wb")

file.write(data)

file.close()

return True

return data

def getFiles(base):

Tags:iPhone png 图片

编辑录入:coldstar [复制链接] [打 印]
赞助商链接