Changeset View
Changeset View
Standalone View
Standalone View
contrib/devtools/optimize-pngs.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2014-2017 The Bitcoin Core developers | # Copyright (c) 2014-2017 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
''' | """ | ||||
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). | 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). | ||||
#pngcrush -brute -ow -rem gAMA -rem cHRM -rem iCCP -rem sRGB -rem alla -rem text | #pngcrush -brute -ow -rem gAMA -rem cHRM -rem iCCP -rem sRGB -rem alla -rem text | ||||
''' | """ | ||||
import hashlib | import hashlib | ||||
import os | import os | ||||
import subprocess | import subprocess | ||||
import sys | import sys | ||||
# pip3 install Pillow | # pip3 install Pillow | ||||
from PIL import Image | from PIL import Image | ||||
def file_hash(filename): | def file_hash(filename): | ||||
'''Return hash of raw file contents''' | """Return hash of raw file contents""" | ||||
with open(filename, 'rb') as f: | with open(filename, "rb") as f: | ||||
return hashlib.sha256(f.read()).hexdigest() | return hashlib.sha256(f.read()).hexdigest() | ||||
def content_hash(filename): | def content_hash(filename): | ||||
'''Return hash of RGBA contents of image''' | """Return hash of RGBA contents of image""" | ||||
i = Image.open(filename) | i = Image.open(filename) | ||||
i = i.convert('RGBA') | i = i.convert("RGBA") | ||||
data = i.tobytes() | data = i.tobytes() | ||||
return hashlib.sha256(data).hexdigest() | return hashlib.sha256(data).hexdigest() | ||||
pngcrush = 'pngcrush' | pngcrush = "pngcrush" | ||||
git = 'git' | git = "git" | ||||
folders = ["src/qt/res/animation", "src/qt/res/icons", "share/pixmaps"] | folders = ["src/qt/res/animation", "src/qt/res/icons", "share/pixmaps"] | ||||
basePath = subprocess.check_output( | basePath = subprocess.check_output( | ||||
[git, 'rev-parse', '--show-toplevel'], universal_newlines=True, encoding='utf8').rstrip('\n') | [git, "rev-parse", "--show-toplevel"], universal_newlines=True, encoding="utf8" | ||||
).rstrip("\n") | |||||
totalSaveBytes = 0 | totalSaveBytes = 0 | ||||
noHashChange = True | noHashChange = True | ||||
outputArray = [] | outputArray = [] | ||||
for folder in folders: | for folder in folders: | ||||
absFolder = os.path.join(basePath, folder) | absFolder = os.path.join(basePath, folder) | ||||
for file in os.listdir(absFolder): | for file in os.listdir(absFolder): | ||||
extension = os.path.splitext(file)[1] | extension = os.path.splitext(file)[1] | ||||
if extension.lower() == '.png': | if extension.lower() == ".png": | ||||
print(f"optimizing {file}...", end=' ') | print(f"optimizing {file}...", end=" ") | ||||
file_path = os.path.join(absFolder, file) | file_path = os.path.join(absFolder, file) | ||||
fileMetaMap = {'file': file, 'osize': os.path.getsize( | fileMetaMap = { | ||||
file_path), 'sha256Old': file_hash(file_path)} | "file": file, | ||||
fileMetaMap['contentHashPre'] = content_hash(file_path) | "osize": os.path.getsize(file_path), | ||||
"sha256Old": file_hash(file_path), | |||||
} | |||||
fileMetaMap["contentHashPre"] = content_hash(file_path) | |||||
try: | try: | ||||
subprocess.call([pngcrush, "-brute", "-ow", "-rem", "gAMA", "-rem", "cHRM", "-rem", "iCCP", "-rem", "sRGB", | subprocess.call( | ||||
"-rem", "alla", "-rem", "text", file_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | [ | ||||
pngcrush, | |||||
"-brute", | |||||
"-ow", | |||||
"-rem", | |||||
"gAMA", | |||||
"-rem", | |||||
"cHRM", | |||||
"-rem", | |||||
"iCCP", | |||||
"-rem", | |||||
"sRGB", | |||||
"-rem", | |||||
"alla", | |||||
"-rem", | |||||
"text", | |||||
file_path, | |||||
], | |||||
stdout=subprocess.DEVNULL, | |||||
stderr=subprocess.DEVNULL, | |||||
) | |||||
except OSError: | except OSError: | ||||
print("pngcrush is not installed, aborting...") | print("pngcrush is not installed, aborting...") | ||||
sys.exit(0) | sys.exit(0) | ||||
# verify | # verify | ||||
if "Not a PNG file" in subprocess.check_output( | if "Not a PNG file" in subprocess.check_output( | ||||
[pngcrush, "-n", "-v", file_path], stderr=subprocess.STDOUT, universal_newlines=True, encoding='utf8'): | [pngcrush, "-n", "-v", file_path], | ||||
print("PNG file " + file + | stderr=subprocess.STDOUT, | ||||
" is corrupted after crushing, check out pngcursh version") | universal_newlines=True, | ||||
encoding="utf8", | |||||
): | |||||
print( | |||||
"PNG file " | |||||
+ file | |||||
+ " is corrupted after crushing, check out pngcursh version" | |||||
) | |||||
sys.exit(1) | sys.exit(1) | ||||
fileMetaMap['sha256New'] = file_hash(file_path) | fileMetaMap["sha256New"] = file_hash(file_path) | ||||
fileMetaMap['contentHashPost'] = content_hash(file_path) | fileMetaMap["contentHashPost"] = content_hash(file_path) | ||||
if fileMetaMap['contentHashPre'] != fileMetaMap['contentHashPost']: | if fileMetaMap["contentHashPre"] != fileMetaMap["contentHashPost"]: | ||||
print("Image contents of PNG file " + file + | print( | ||||
" before and after crushing don't match") | "Image contents of PNG file " | ||||
+ file | |||||
+ " before and after crushing don't match" | |||||
) | |||||
sys.exit(1) | sys.exit(1) | ||||
fileMetaMap['psize'] = os.path.getsize(file_path) | fileMetaMap["psize"] = os.path.getsize(file_path) | ||||
outputArray.append(fileMetaMap) | outputArray.append(fileMetaMap) | ||||
print("done") | print("done") | ||||
print("summary:\n+++++++++++++++++") | print("summary:\n+++++++++++++++++") | ||||
for fileDict in outputArray: | for fileDict in outputArray: | ||||
oldHash = fileDict['sha256Old'] | oldHash = fileDict["sha256Old"] | ||||
newHash = fileDict['sha256New'] | newHash = fileDict["sha256New"] | ||||
totalSaveBytes += fileDict['osize'] - fileDict['psize'] | totalSaveBytes += fileDict["osize"] - fileDict["psize"] | ||||
noHashChange = noHashChange and (oldHash == newHash) | noHashChange = noHashChange and (oldHash == newHash) | ||||
print(fileDict['file'] + "\n size diff from: " + str(fileDict['osize']) + " to: " + | print( | ||||
str(fileDict['psize']) + "\n old sha256: " + oldHash + "\n new sha256: " + newHash + "\n") | fileDict["file"] | ||||
+ "\n size diff from: " | |||||
print("completed. Checksum stable: " + str(noHashChange) + | + str(fileDict["osize"]) | ||||
". Total reduction: " + str(totalSaveBytes) + " bytes") | + " to: " | ||||
+ str(fileDict["psize"]) | |||||
+ "\n old sha256: " | |||||
+ oldHash | |||||
+ "\n new sha256: " | |||||
+ newHash | |||||
+ "\n" | |||||
) | |||||
print( | |||||
"completed. Checksum stable: " | |||||
+ str(noHashChange) | |||||
+ ". Total reduction: " | |||||
+ str(totalSaveBytes) | |||||
+ " bytes" | |||||
) |