Merge #12062: Increment MIT Licence copyright header year on files modified in 2017
[bitcoinplatinum.git] / contrib / devtools / optimize-pngs.py
blob5cb3bb6f7566565b5eb1a0cfdda6c03087ffcc70
1 #!/usr/bin/env python
2 # Copyright (c) 2014-2017 The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 '''
6 Run this script every time you change one of the png files. Using pngcrush, it will optimize the png files, remove various color profiles, remove ancillary chunks (alla) and text chunks (text).
7 #pngcrush -brute -ow -rem gAMA -rem cHRM -rem iCCP -rem sRGB -rem alla -rem text
8 '''
9 import os
10 import sys
11 import subprocess
12 import hashlib
13 from PIL import Image
15 def file_hash(filename):
16 '''Return hash of raw file contents'''
17 with open(filename, 'rb') as f:
18 return hashlib.sha256(f.read()).hexdigest()
20 def content_hash(filename):
21 '''Return hash of RGBA contents of image'''
22 i = Image.open(filename)
23 i = i.convert('RGBA')
24 data = i.tobytes()
25 return hashlib.sha256(data).hexdigest()
27 pngcrush = 'pngcrush'
28 git = 'git'
29 folders = ["src/qt/res/movies", "src/qt/res/icons", "share/pixmaps"]
30 basePath = subprocess.check_output([git, 'rev-parse', '--show-toplevel']).rstrip('\n')
31 totalSaveBytes = 0
32 noHashChange = True
34 outputArray = []
35 for folder in folders:
36 absFolder=os.path.join(basePath, folder)
37 for file in os.listdir(absFolder):
38 extension = os.path.splitext(file)[1]
39 if extension.lower() == '.png':
40 print("optimizing "+file+"..."),
41 file_path = os.path.join(absFolder, file)
42 fileMetaMap = {'file' : file, 'osize': os.path.getsize(file_path), 'sha256Old' : file_hash(file_path)}
43 fileMetaMap['contentHashPre'] = content_hash(file_path)
45 pngCrushOutput = ""
46 try:
47 pngCrushOutput = subprocess.check_output(
48 [pngcrush, "-brute", "-ow", "-rem", "gAMA", "-rem", "cHRM", "-rem", "iCCP", "-rem", "sRGB", "-rem", "alla", "-rem", "text", file_path],
49 stderr=subprocess.STDOUT).rstrip('\n')
50 except:
51 print "pngcrush is not installed, aborting..."
52 sys.exit(0)
54 #verify
55 if "Not a PNG file" in subprocess.check_output([pngcrush, "-n", "-v", file_path], stderr=subprocess.STDOUT):
56 print "PNG file "+file+" is corrupted after crushing, check out pngcursh version"
57 sys.exit(1)
59 fileMetaMap['sha256New'] = file_hash(file_path)
60 fileMetaMap['contentHashPost'] = content_hash(file_path)
62 if fileMetaMap['contentHashPre'] != fileMetaMap['contentHashPost']:
63 print "Image contents of PNG file "+file+" before and after crushing don't match"
64 sys.exit(1)
66 fileMetaMap['psize'] = os.path.getsize(file_path)
67 outputArray.append(fileMetaMap)
68 print("done\n"),
70 print "summary:\n+++++++++++++++++"
71 for fileDict in outputArray:
72 oldHash = fileDict['sha256Old']
73 newHash = fileDict['sha256New']
74 totalSaveBytes += fileDict['osize'] - fileDict['psize']
75 noHashChange = noHashChange and (oldHash == newHash)
76 print fileDict['file']+"\n size diff from: "+str(fileDict['osize'])+" to: "+str(fileDict['psize'])+"\n old sha256: "+oldHash+"\n new sha256: "+newHash+"\n"
78 print "completed. Checksum stable: "+str(noHashChange)+". Total reduction: "+str(totalSaveBytes)+" bytes"