-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
905711f contrib: improve optimize-pngs.py (Wladimir J. van der Laan) 42f6a0c [Qt] optimize PNG files (Jonas Schnelli)
- Loading branch information
Showing
82 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#!/usr/bin/env python | ||
|
||
import os | ||
import sys | ||
import subprocess | ||
import hashlib | ||
from PIL import Image | ||
|
||
def file_hash(filename): | ||
'''Return hash of raw file contents''' | ||
with open(filename, 'rb') as f: | ||
return hashlib.sha256(f.read()).hexdigest() | ||
|
||
def content_hash(filename): | ||
'''Return hash of RGBA contents of image''' | ||
i = Image.open(filename) | ||
i = i.convert('RGBA') | ||
data = i.tostring() | ||
return hashlib.sha256(data).hexdigest() | ||
|
||
#optimize png, 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 = 'pngcrush' | ||
git = 'git' | ||
folders = ["src/qt/res/movies", "src/qt/res/icons", "src/qt/res/images"] | ||
basePath = subprocess.check_output([git, 'rev-parse', '--show-toplevel']).rstrip('\n') | ||
totalSaveBytes = 0 | ||
|
||
outputArray = [] | ||
for folder in folders: | ||
absFolder=os.path.join(basePath, folder) | ||
for file in os.listdir(absFolder): | ||
extension = os.path.splitext(file)[1] | ||
if extension.lower() == '.png': | ||
print("optimizing "+file+"..."), | ||
file_path = os.path.join(absFolder, file) | ||
fileMetaMap = {'file' : file, 'osize': os.path.getsize(file_path), 'sha256Old' : file_hash(file_path)}; | ||
fileMetaMap['contentHashPre'] = content_hash(file_path) | ||
|
||
pngCrushOutput = "" | ||
try: | ||
pngCrushOutput = subprocess.check_output( | ||
[pngcrush, "-brute", "-ow", "-rem", "gAMA", "-rem", "cHRM", "-rem", "iCCP", "-rem", "sRGB", "-rem", "alla", "-rem", "text", file_path], | ||
stderr=subprocess.STDOUT).rstrip('\n') | ||
except: | ||
print "pngcrush is not installed, aborting..." | ||
sys.exit(0) | ||
|
||
#verify | ||
if "Not a PNG file" in subprocess.check_output([pngcrush, "-n", "-v", file_path], stderr=subprocess.STDOUT): | ||
print "PNG file "+file+" is corrupted after crushing, check out pngcursh version" | ||
sys.exit(1) | ||
|
||
fileMetaMap['sha256New'] = file_hash(file_path) | ||
fileMetaMap['contentHashPost'] = content_hash(file_path) | ||
|
||
if fileMetaMap['contentHashPre'] != fileMetaMap['contentHashPost']: | ||
print "Image contents of PNG file "+file+" before and after crushing don't match" | ||
sys.exit(1) | ||
|
||
fileMetaMap['psize'] = os.path.getsize(file_path) | ||
outputArray.append(fileMetaMap) | ||
print("done\n"), | ||
|
||
print "summary:\n+++++++++++++++++" | ||
for fileDict in outputArray: | ||
oldHash = fileDict['sha256Old'] | ||
newHash = fileDict['sha256New'] | ||
totalSaveBytes += fileDict['osize'] - fileDict['psize'] | ||
print fileDict['file']+"\n size diff from: "+str(fileDict['osize'])+" to: "+str(fileDict['psize'])+"\n old sha256: "+oldHash+"\n new sha256: "+newHash+"\n" | ||
|
||
print "completed. Total reduction: "+str(totalSaveBytes)+" bytes" |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.