optimize-pngs.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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
  14. def file_hash(filename):
  15. '''Return hash of raw file contents'''
  16. with open(filename, 'rb') as f:
  17. return hashlib.sha256(f.read()).hexdigest()
  18. def content_hash(filename):
  19. '''Return hash of RGBA contents of image'''
  20. i = Image.open(filename)
  21. i = i.convert('RGBA')
  22. data = i.tobytes()
  23. return hashlib.sha256(data).hexdigest()
  24. pngcrush = 'pngcrush'
  25. git = 'git'
  26. folders = ["src/qt/res/movies", "src/qt/res/icons", "share/pixmaps"]
  27. basePath = subprocess.check_output([git, 'rev-parse', '--show-toplevel']).rstrip('\n')
  28. totalSaveBytes = 0
  29. noHashChange = True
  30. outputArray = []
  31. for folder in folders:
  32. absFolder=os.path.join(basePath, folder)
  33. for file in os.listdir(absFolder):
  34. extension = os.path.splitext(file)[1]
  35. if extension.lower() == '.png':
  36. print("optimizing "+file+"..."),
  37. file_path = os.path.join(absFolder, file)
  38. fileMetaMap = {'file' : file, 'osize': os.path.getsize(file_path), 'sha256Old' : file_hash(file_path)}
  39. fileMetaMap['contentHashPre'] = content_hash(file_path)
  40. pngCrushOutput = ""
  41. try:
  42. pngCrushOutput = subprocess.check_output(
  43. [pngcrush, "-brute", "-ow", "-rem", "gAMA", "-rem", "cHRM", "-rem", "iCCP", "-rem", "sRGB", "-rem", "alla", "-rem", "text", file_path],
  44. stderr=subprocess.STDOUT).rstrip('\n')
  45. except:
  46. sys.exit(0)
  47. #verify
  48. if "Not a PNG file" in subprocess.check_output([pngcrush, "-n", "-v", file_path], stderr=subprocess.STDOUT):
  49. sys.exit(1)
  50. fileMetaMap['sha256New'] = file_hash(file_path)
  51. fileMetaMap['contentHashPost'] = content_hash(file_path)
  52. if fileMetaMap['contentHashPre'] != fileMetaMap['contentHashPost']:
  53. sys.exit(1)
  54. fileMetaMap['psize'] = os.path.getsize(file_path)
  55. outputArray.append(fileMetaMap)
  56. print("done\n"),
  57. print ("summary:\n+++++++++++++++++")
  58. for fileDict in outputArray:
  59. oldHash = fileDict['sha256Old']
  60. newHash = fileDict['sha256New']
  61. totalSaveBytes += fileDict['osize'] - fileDict['psize']
  62. noHashChange = noHashChange and (oldHash == newHash)
  63. print (fileDict['file']+"\n size diff from: "+str(fileDict['osize'])+" to: "+str(fileDict['psize'])+"\n old sha256: "+oldHash+"\n new sha256: "+newHash+"\n")