`

把ipa中压缩过的png转换成正常PNG的Python脚本

 
阅读更多

苹果应用安装程序,其实质是一个压缩文件,里面包含应用所需的图片资源,但被压缩过,无法直接拿来使用,需要在MacOSX 中调用pngcrush工具把图片转换成正常的图像才行。在Windows系统上,在sf上有个开源工具号称可以转换,但没搞明白如何使用,放弃了。今天从国外网站找了个Python脚本,还没试,等需要的时候再试。

#---
# 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
# 
# 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)
    
    # 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
                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):
    global _dirs
    global _pngs
    if base == ".":
        _dirs = []
        _pngs = []
        
    if base in _dirs:
        return

    files = os.listdir(base)
    for  file in files:
        filepath = os.path.join(base, file)
        try:
            st = os.lstat(filepath)
        except os.error:
            continue
        
        if stat.S_ISDIR(st.st_mode):
            if not filepath in _dirs:
                getFiles(filepath)
                _dirs.append( filepath )
                
        elif file[-4:].lower() == ".png":
            if not filepath in _pngs:
                _pngs.append( filepath )
            
    if base == ".":
        return _dirs, _pngs

print "-----------------------------------"
print " iPhone PNG Images Normalizer v1.0"
print "-----------------------------------"
print " "
print "[+] Searching PNG files...",
dirs, pngs = getFiles(".")
print "ok"

if len(pngs) == 0:
    print " "
    print "[!] Alert: There are no PNG files found. Move this python file to the folder that contains the PNG files to normalize."
    exit()
    
print " "
print " -  %d PNG files were found at this folder (and subfolders)." % len(pngs)
print " "
while True:
    normalize = raw_input("[?] Do you want to normalize all images (Y/N)? ").lower()
    if len(normalize) > 0 and (normalize[0] == "y" or normalize[0] == "n"):
        break

normalized = 0
if normalize[0] == "y":
    for  ipng in xrange(len(pngs)):
        perc = (float(ipng) / len(pngs)) * 100.0
        print "%.2f%% %s" % (perc, pngs[ipng])
        if updatePNG(pngs[ipng]):
            normalized += 1
print " "
print "[+] %d PNG files were normalized." % normalized

 

分享到:
评论

相关推荐

    Java提取IPA中的png文件, 并进行解码还原png图片

    这次我们要探讨的是如何在Java环境中处理iOS应用(IPA)中的PNG图像文件,并将其解码以便在Windows或其他非iOS平台上正常显示。首先,让我们理解一下问题的背景。 PNG(Portable Network Graphics)是一种无损压缩...

    IPA图片还原工具源代码

    4. ipin.py:这是源代码文件的名字,通常是一个Python脚本,用于实现从IPA文件中提取PNG图片的功能。Python是一种广泛使用的编程语言,因其简洁的语法和丰富的库支持而适合进行这样的任务。它可能包含了读取ZIP文件...

    Python-查找iOS或Android项目未使用的png并支持删除

    PNG图像文件因其高质量和无损压缩特性被广泛使用,但随着时间的推移,项目中可能会积累许多未使用的PNG图片,这不仅占用存储空间,还可能导致编译过程中的冗余处理。本教程将详细介绍如何利用Python编写脚本来查找并...

    swift-iOS自动打包脚本并实现图片素材文字资源部分代码的替换和重签名

    1. 查找:Python脚本可以遍历项目的资源文件,如图片(.png, .jpg等)和本地化文件(.strings)。 2. 替换:通过正则表达式或特定规则定位需要替换的资源,然后进行替换操作。例如,替换图片名称或字符串变量。 四...

    Python实现递归遍历文件夹并删除文件

    在本方法中,我们利用Python的标准库`os`和`shutil`来递归地遍历文件夹并删除指定的文件夹(例如`.svn`文件夹)。这种做法适用于那些包含版本控制系统产生的文件夹,这些文件夹通常占用空间较大但对用户来说并不重要...

    小游戏源码-长大后我就是你吗?.rar

    2. 脚本文件:使用JavaScript、Python、C#等语言编写的控制游戏逻辑的代码。 3. 图形资源:包括PNG、JPEG或SVG格式的图像,用于游戏的背景、角色、UI元素等。 4. 声音文件:MP3、WAV或OGG格式的音频,可能包含音乐、...

    hellochat.zip

    今天,我们要探讨的是一个名为"hellochat"的应用程序,它封装在一个名为"hellochat.zip"的压缩文件中。这个压缩包很可能是开发者为了方便分发或备份而创建的,包含了一系列与"hellochat"相关的文件。在这个讨论中,...

    附件1.rar

    1. **编程语言**:如果压缩包包含代码文件(如`.java`、`.py`、`.cpp`等),它们可能涉及Java、Python或C++等编程语言的基础知识,包括语法、数据结构、算法等。 2. **软件开发**:可能包含项目文件(`.git`、`.sln...

    Appium

    在 Python 中编写测试脚本,我们可以调用 WebDriver 提供的各种方法来模拟用户操作,如点击按钮、输入文本、滑动屏幕等。例如: ```python driver.find_element_by_name('ButtonName').click() # 点击按钮 driver....

Global site tag (gtag.js) - Google Analytics