1- #!/usr/bin/env python
1+ #!/usr/bin/env python3
22# Copyright (c) 2014-2017 The Bitcoin Core developers
33# Distributed under the MIT software license, see the accompanying
44# file COPYING or http://www.opensource.org/licenses/mit-license.php.
1010import sys
1111import subprocess
1212import hashlib
13- from PIL import Image
13+ from PIL import Image # pip3 install Pillow
1414
1515def file_hash (filename ):
1616 '''Return hash of raw file contents'''
@@ -27,7 +27,7 @@ def content_hash(filename):
2727pngcrush = 'pngcrush'
2828git = 'git'
2929folders = ["src/qt/res/movies" , "src/qt/res/icons" , "share/pixmaps" ]
30- basePath = subprocess .check_output ([git , 'rev-parse' , '--show-toplevel' ]).rstrip ('\n ' )
30+ basePath = subprocess .check_output ([git , 'rev-parse' , '--show-toplevel' ], universal_newlines = True ).rstrip ('\n ' )
3131totalSaveBytes = 0
3232noHashChange = True
3333
@@ -37,42 +37,40 @@ def content_hash(filename):
3737 for file in os .listdir (absFolder ):
3838 extension = os .path .splitext (file )[1 ]
3939 if extension .lower () == '.png' :
40- print ("optimizing " + file + " ..."),
40+ print ("optimizing {} ..." . format ( file ), end = ' ' )
4141 file_path = os .path .join (absFolder , file )
4242 fileMetaMap = {'file' : file , 'osize' : os .path .getsize (file_path ), 'sha256Old' : file_hash (file_path )}
4343 fileMetaMap ['contentHashPre' ] = content_hash (file_path )
4444
45- pngCrushOutput = ""
4645 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 ' )
46+ subprocess .call ([pngcrush , "-brute" , "-ow" , "-rem" , "gAMA" , "-rem" , "cHRM" , "-rem" , "iCCP" , "-rem" , "sRGB" , "-rem" , "alla" , "-rem" , "text" , file_path ],
47+ stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL )
5048 except :
51- print "pngcrush is not installed, aborting..."
49+ print ( "pngcrush is not installed, aborting..." )
5250 sys .exit (0 )
5351
5452 #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"
53+ if "Not a PNG file" in subprocess .check_output ([pngcrush , "-n" , "-v" , file_path ], stderr = subprocess .STDOUT , universal_newlines = True ):
54+ print ( "PNG file " + file + " is corrupted after crushing, check out pngcursh version" )
5755 sys .exit (1 )
5856
5957 fileMetaMap ['sha256New' ] = file_hash (file_path )
6058 fileMetaMap ['contentHashPost' ] = content_hash (file_path )
6159
6260 if fileMetaMap ['contentHashPre' ] != fileMetaMap ['contentHashPost' ]:
63- print "Image contents of PNG file " + file + " before and after crushing don't match"
61+ print ( "Image contents of PNG file {} before and after crushing don't match" . format ( file ))
6462 sys .exit (1 )
6563
6664 fileMetaMap ['psize' ] = os .path .getsize (file_path )
6765 outputArray .append (fileMetaMap )
68- print ("done\n " ),
66+ print ("done" )
6967
70- print "summary:\n +++++++++++++++++"
68+ print ( "summary:\n +++++++++++++++++" )
7169for fileDict in outputArray :
7270 oldHash = fileDict ['sha256Old' ]
7371 newHash = fileDict ['sha256New' ]
7472 totalSaveBytes += fileDict ['osize' ] - fileDict ['psize' ]
7573 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 "
74+ print ( fileDict ['file' ]+ "\n size diff from: " + str (fileDict ['osize' ])+ " to: " + str (fileDict ['psize' ])+ "\n old sha256: " + oldHash + "\n new sha256: " + newHash + "\n " )
7775
78- print "completed. Checksum stable: " + str (noHashChange )+ ". Total reduction: " + str (totalSaveBytes )+ " bytes"
76+ print ( "completed. Checksum stable: " + str (noHashChange )+ ". Total reduction: " + str (totalSaveBytes )+ " bytes" )
0 commit comments