Last active
June 29, 2020 21:33
-
-
Save elbaulp/188a7f9d24e586cb16d9ed9188aa5823 to your computer and use it in GitHub Desktop.
Cómo Añadir Automáticamente El Tamaño De Una Imagen en HTML Con Python
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
#!/bin/python | |
from BeautifulSoup import BeautifulSoup | |
from os.path import basename, splitext | |
from PIL import Image | |
import glob | |
import io | |
path = "/ruta/ficheros/*.markdown" | |
for fname in glob.glob(path): | |
with io.open(fname,'r',encoding='utf8') as f: | |
post = f.read() | |
soup = BeautifulSoup(post) | |
for img in soup.findAll('img'): | |
if img != None: | |
try: | |
if img['src'].startswith("/assets") == True: | |
pil = Image.open("/ruta/imagenes" + img['src']) | |
width, height = pil.size | |
img['width'] = str(width) + "px" | |
img['height'] = str(height) + "px" | |
except KeyError: | |
pass | |
with open(fname, "wb") as file: | |
file.write(soup.prettify()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment