iPhone PNG Images Normalizer

Información general

iPhone PNG Images Normalizer es un software hecho en python que te permitirá normalizar las imágenes PNG que haz descargado desde tu iPhone o iPod touch. Una vez normalizadas las imágenes podrás visualizarlas y editarlas como cualquier otro archivo PNG.

Código
#---
# 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

Download

» Download iPhone PNG Images Normalizer v1.0





Comments (17 comments)

nofxx wrote: 2009-03-10 18:22:52
Works as a charm! Thank you!
 
rriphone wrote: 2009-03-15 18:50:26
This is great. I used another tool called iphone_png which left the image colors inverted. This one works wonderfully. Thank you
 
Harry Harrison wrote: 2009-04-10 17:07:13
Damn, i wish i knew how i could use this.

I've been using your online normalizer for a a while, but it doesn't seem to be working at the moment. Just letting you know.

http://zope.release78.org/beta2/iconvert/index_html
 
Axel E. Brzostowski wrote: 2009-04-13 03:49:12
Harry Harrison,

Thanks, but that page isn't mine.

To use this code you have to:

1) Install python at your computer

2) Save ipin.py file in the folder where you have all PNG images to modify (warning: images will be replaced!)

3) Run the program using 'python ipin.py' command (without quotes), or maybe you will be able to run it just double clicking on file.

I hope it was helpful to you!

Good luck!

Axel
 
Pedro wrote: 2009-04-25 14:43:04
How to do the invers way?
 
Grant wrote: 2009-05-08 14:22:06
This seems like the only script that will work on my Mac but theres one major problem when i run the script it searches my whole hard drive not just the folder i put ipin.py in, and i beleive i've heard that if its used on a already viewable png then those files would be permanently corrupted so i dot know what to do.
 
Michael wrote: 2009-05-08 19:00:16
it doesnt work for me, how do i run the script?
 
Pharma798 wrote: 2009-07-03 22:26:15
Very nice site!
 
Pharmg206 wrote: 2009-07-04 14:17:05
Very nice site!
 
Pharmd981 wrote: 2009-07-04 16:04:51
Very nice site!
 
Pharmc15 wrote: 2009-07-08 23:50:40
Very nice site!
 
Phil wrote: 2009-07-14 02:10:06
Your online converter produces an error :(
 
Axel E. Brzostowski wrote: 2009-07-17 15:17:08
Hi Phil, I don't have an online converter. Any web page outside this server isn't mine.
 
Pharmd846 wrote: 2009-07-28 11:41:52
Very nice site!
 
erik wrote: 2009-07-28 19:37:39
i can't get it to work on windows... followed the instructions and ran it in the folder with the png files, but when i try to edit them i still can't display them properly
 
vishy wrote: 2009-08-15 16:33:11
sad spam, but how to mod your amazing python script to not convert all png on hard drive, I\'ve tried putting it in different folders, I read the code but don\'t know what to alter, if possible, thanks still
 
Axel E. Brzostowski wrote: 2012-09-17 17:19:48
Thanks to Uriel Katz for this contribution:

https://gist.github.com/3609051

(It handles files with more than one IDAT chunk. Icon-72.png has that problem)
 
Post your comment
Name:
Email:
(optional)
(It will not be shown / No será mostrado)
Comment: