Last active
December 14, 2015 23:59
-
-
Save calimarkus/5169828 to your computer and use it in GitHub Desktop.
validate Retina image sizes (they have to be even in width & height and @1x has to be half sized of @2x version)
This file contains hidden or 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
import os, sys, glob | |
# logging methods | |
def printRed(msg): | |
print "\x1B[" + "31;40m" + msg + "\x1B[0m" | |
def printGreen(msg): | |
print "\x1B[" + "32;40m" + msg + "\x1B[0m" | |
# check for argument | |
if len(sys.argv) <= 1 : | |
print "No directory name given." | |
sys.exit() | |
# check if PIL is installed | |
try: | |
from PIL import Image | |
except ImportError: | |
printRed("ERROR: You have to install PIL (python image library) first!") | |
sys.exit() | |
# read and print path | |
path = sys.argv[1] | |
wrongFiles = [] | |
# define scan method | |
def scanDir(path, dirname='') : | |
if not os.path.exists(path): | |
return | |
if len(dirname) == 0 : | |
print "Scanning directory: \"" + os.path.basename(path) + "\"" | |
# read all @2x png files in path (not including subdirectories yet) | |
listing = glob.glob( os.path.join(path, '*@2x.*')) | |
# no png files found | |
if len(listing) == 0 and len(dirname) == 0 : | |
print ">> No @2x.png files found." | |
elif len(listing) > 0 and len(dirname) > 0 : | |
print "Scanning directory: \"" + os.path.basename(path.replace("/"+dirname,"")) + "/" + os.path.basename(path) + "\"" | |
# iterate over png files | |
filesOkayCount = 0 | |
for file in listing: | |
basename = os.path.basename(file) | |
baseWithDir = basename | |
if len(dirname) > 0 : | |
baseWithDir = dirname + '/' + basename | |
# read 2x image | |
image = Image.open(file) | |
imageSizeString = str(image.size[0]) + "x" + str(image.size[1]) + "px" | |
msg = basename + ", " + imageSizeString | |
# read 1x image | |
smallFile = file.replace("@2x", ""); | |
smallFileExisting = os.path.exists(smallFile); | |
if smallFileExisting: | |
smallImage = Image.open(smallFile) | |
smallImageSizeString = str(smallImage.size[0]) + "x" + str(smallImage.size[1]) + "px" | |
smallMsg = basename + " and " + basename.replace("@2x", "") + " are not half/double size of each other " + imageSizeString + " vs " + smallImageSizeString | |
else : | |
smallMsg = basename.replace("@2x", "") + " (non retina) image missing" | |
# check size | |
if image.size[0]%2 != 0 or image.size[1]%2 != 0 : | |
wrongFiles.append(baseWithDir) | |
printRed("ERROR " + msg) | |
# compare size | |
elif not smallFileExisting or smallImage.size[0] != image.size[0]/2 or smallImage.size[1] != image.size[1]/2 : | |
wrongFiles.append(baseWithDir) | |
printRed("ERROR " + smallMsg) | |
else : | |
# printGreen("OK " + msg) | |
filesOkayCount += 1 | |
if filesOkayCount > 0 : | |
printGreen("OK " + str(filesOkayCount) + " correct files") | |
if len(listing) > 0 : | |
print " " | |
# scan subdirectories | |
for file, dirs, other in os.walk(path) : | |
for dir in dirs : | |
scanDir(path+"/"+dir, os.path.basename(dir)) | |
# start scanning | |
scanDir(path) | |
# print total results | |
print "Results:" | |
if len(wrongFiles) > 0 : | |
printRed(str(len(wrongFiles)) + " wrong file(s):") | |
print '\n'.join(['x ' + str(file) for file in wrongFiles]) | |
else : | |
printGreen("\nAll files OK.") |
Jetzt wird auch geprüft ob die Höhe/Breite im 1x genau die Hälfte von 2x ist!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Max: und cool wäre es natürlich, wenn es gleich noch schauen würde, ob es auch zur nicht-Retina-Version passt.