Skip to content

Instantly share code, notes, and snippets.

Created January 3, 2019 07:51
Show Gist options
  • Save Jetroid/b664cc94bc95e757689b7e761abc00ce to your computer and use it in GitHub Desktop.
Save Jetroid/b664cc94bc95e757689b7e761abc00ce to your computer and use it in GitHub Desktop.
import yaml
import os
import git
FOLDERS = ["_tattoos","_brows","_commission"]
#Link git with python
repo = git.Repo()
#Get the staged files
diffs = repo.index.diff('HEAD')
staged_files = [x.a_blob.path for x in diffs if hasattr(x, 'a_blob') and x.a_blob != None ]
#Directory of this file
dir = os.path.dirname(os.path.abspath(__file__))
#For each gallery directory, find all files and build the yaml.
for folder in FOLDERS:
#generate the path to the yaml file Jekyll will use
folderpath = os.path.join(dir, folder)
yamlfile = os.path.join(folderpath, YAML_FILE)
#Generate a list of images the yaml file knows about
#These images won't need compressing as they already are.
knownimages = []
if os.path.exists(yamlfile):
stream = open(yamlfile, 'r')
data = yaml.load_all(stream)
knownimages =['images'] or []
#Generate a list of images that are there right now
realimages = []
for file in os.listdir(folderpath):
if file.endswith(".jpg") or file.endswith(".jpeg"):
elif file.endswith(".png"):
#Some images may have been removed since the yaml was last updated
#Let's remove those entries
images = [img for img in knownimages if img in realimages]
#Now get the images that need compressing and adding to the yaml
newimages = [img for img in realimages if img not in knownimages]
#Compress the image and remember it
for image in newimages:
imagepath = os.path.join(folder, image)
#Compress, re-stage, and remember the images
if imagepath in staged_files:
if image.endswith(".jpg") or image.endswith(".jpeg"):
os.system("convert " + imagepath + " -sampling-factor 4:2:0 -strip -quality 85 -interlace JPEG -colorspace RGB " + imagepath)
elif image.endswith(".png"):
os.system("optipng -quiet -o1 -strip all " + imagepath);
#Remember the images
#Write the new yaml
with open(yamlfile, 'w+') as outfile:
yaml.dump({'images':images}, outfile, default_flow_style=False)
#Restage the yaml file:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment