Last active
February 12, 2020 08:08
-
-
Save hirokiky/caaebcea1f7606e86ec5b83e20934390 to your computer and use it in GitHub Desktop.
Just a scrap.
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
import re | |
import time | |
from pathlib import Path | |
from urllib.request import urlretrieve | |
from PIL import Image | |
ROOT_DIR = Path("path/to/repo") | |
PATH_GLOB = "**/*.md" | |
ASSET_DIR = "assets" | |
IMAGE_REGEXP = re.compile( | |
r"(\!\[.*\]\((http[s]?://" | |
r"(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|" | |
r"(?:%[0-9a-fA-F][0-9a-fA-F]))+)\))" | |
) | |
URL_FILTER = "REQUIRED DOMAIN NAME" | |
def downloader(): | |
for p in sorted(ROOT_DIR.glob(PATH_GLOB)): | |
with p.open() as f: | |
content = f.read() | |
for match in IMAGE_REGEXP.findall(content): | |
url = match[1] | |
if URL_FILTER not in url: | |
continue | |
_, basename = url.rsplit("/", 1) | |
if not basename: | |
raise ValueError("Empty basename is not supported") | |
print(url) | |
assets_path = p.parent / "assets" | |
assets_path.mkdir(exist_ok=True) | |
urlretrieve(url, assets_path / basename) | |
content = content.replace(match[0], f"[[!asset:{basename}]]") | |
time.sleep(0.2) | |
with p.open(mode="w") as f: | |
f.write(content) | |
def jpeg_converter(path): | |
img = Image.open(str(path)) | |
print(path) | |
filename, ext = path.name.rsplit(".", 1) | |
jpeg_path = path.parent / (filename + ".jpg") | |
if img.mode == "RGBA": | |
background = Image.new("RGB", img.size, (255, 255, 255)) | |
background.paste(img, (0, 0), img) | |
img = background | |
img = img.convert("RGB") | |
img.save(jpeg_path, format="JPEG", quality=90, optimize=True, progressive=True) | |
def jpeg_convert(): | |
for p in sorted(ROOT_DIR.glob("**/assets/*.png")): | |
jpeg_converter(p) | |
def rename_png(): | |
for p in sorted(ROOT_DIR.glob(PATH_GLOB)): | |
with p.open() as f: | |
content = f.read() | |
content = re.sub(r"\[\[\!asset\:(.+)\.png\]\]", r"[[!asset:\1.jpg]]", content) | |
with p.open(mode="w") as f: | |
f.write(content) | |
MAX_SIZE = 1000 | |
def resizer(): | |
for p in sorted(ROOT_DIR.glob("**/assets/*.jpg")): | |
img = Image.open(str(p)) | |
x, y = img.size | |
if x <= MAX_SIZE and y <= MAX_SIZE: | |
continue | |
base = x if x > y else y | |
ratio = MAX_SIZE / base | |
img.resize((int(x * ratio), int(y * ratio))).save(str(p)) | |
print(p) | |
def main(): | |
resizer() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment