#! /usr/bin/env python # Daybreaker's PNG Color Profile Removal Tool """ Copyright (c) 2008, Joongi Kim All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the SPARCS nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.""" import os, sys, getopt, struct, zlib opt_recurse = False opt_startdir = '.' opt_verbose = False opt_fields = set() opt_backup = False def usage(): print """Usage: python pngtool.py [OPTIONS] Options: -h, --help Show this help message. -r, --recurse Perform the removal operation recursively with subdirectories. -f, --full Remove all color profile information. (Same to -gsci) -v, --verbose Print more details while working. -b, --backup Preserve the original copy of each file. -g Remove gAMA field. -s Remove sRGB field. -c Remove cHRM field. -i Remove iCCP field. You can refer W3C PNG specification (http://www.w3.org/TR/PNG/) for details. This program is distributed under BSD License. """ try: opts, args = getopt.getopt(sys.argv[1:], 'bhrgscifv', ['backup', 'help', 'recurse', 'full', 'verbose']) except getopt.GetoptError: usage() sys.exit(2) for o, a in opts: if o in ('-h', '--help'): usage() sys.exit(0) if o in ('-r', '--recurse'): opt_recurse = True if o in ('-v', '--verbose'): opt_verbose = True if o in ('-b', '--backup'): opt_backup = True if o == '-g': opt_fields.add('gAMA') if o == '-s': opt_fields.add('sRGB') if o == '-c': opt_fields.add('cHRM') if o == '-i': opt_fields.add('iCCP') if o in ('-f', '--full'): opt_fields.add('gAMA') opt_fields.add('sRGB') opt_fields.add('cHRM') opt_fields.add('iCCP') class PNGFormatError(Exception): pass def unsigned32(n): return n & 0xFFFFFFFFL def read_chunk(file): _len = file.read(4) if _len == '' or _len == None: return None name = file.read(4) length = struct.unpack('>I', _len)[0] _data = file.read(length) if len(_data) != length: raise PNGFormatError('Chunk length mismatch.') _crc = file.read(4) crc = struct.unpack('>I', _crc)[0] crc1 = zlib.crc32(name) crc2 = unsigned32(zlib.crc32(_data, crc1)) if crc != crc2: raise PNGFormatError('CRC check failed.') return (name, length, _data, crc2) def clear_colorinfo(filename): global opt_fields, opt_verbose, opt_backup sfile = open(filename, 'rb') tfile = open(filename + '.tmp', 'w+b') sig = struct.unpack('>8B', sfile.read(8)) if sig != (137, 80, 78, 71, 13, 10, 26, 10): raise PNGFormatError('Invalid signature.') tfile.write(struct.pack('>8B', *sig)) while True: chunk = read_chunk(sfile) if chunk == None: break if opt_verbose: print 'Chunk %s, %d bytes' % (chunk[0], chunk[1]) if chunk[0] in opt_fields: if opt_verbose: print ' -> Removed...' else: tfile.write(struct.pack('>I', chunk[1])) tfile.write(chunk[0]) tfile.write(chunk[2]) tfile.write(struct.pack('>I', chunk[3])) tfile.close() sfile.close() if opt_backup: os.rename(filename, filename + '.backup') else: os.unlink(filename) os.rename(filename + '.tmp', filename) def do_in_dir(dirname): global opt_recurse files = map(lambda f: os.path.join(dirname, f), os.listdir(dirname)) for f in files: if os.path.isdir(f): if opt_recurse: do_in_dir(f) else: if f.lower().endswith('.png'): print 'Processing %s...' % f try: clear_colorinfo(f) except PNGFormatError, pe: print 'ERROR: %s' % pe except Exception, e: print 'Unexpected Error!' do_in_dir(opt_startdir)