Skip to content

Instantly share code, notes, and snippets.

@elbaulp
Last active June 29, 2020 21:33
Show Gist options
  • Save elbaulp/188a7f9d24e586cb16d9ed9188aa5823 to your computer and use it in GitHub Desktop.
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
#!/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