Alejandro Alcalde

Data Scientist and Computer Scientist. Creator of this blog.

Alejandro Alcalde's posts | Porfolio

Some time ago I needed to add height and width attributes to all images on this blog. So I wrote a python script, here is the main idea:

To parse the data, I’ve used BeautifulSoup:


from BeautifulSoup import BeautifulSoup
from os.path import basename, splitext
from PIL import Image
import glob

# Path where the posts are, in markdown format
path = "/ruta/ficheros/*.md"

# Iterate over all posts
for fname in glob.glob(path):
    # Open the post
    f = open(fname)
    # Create a BeautifulSoup object to parse the file
    soup = BeautifulSoup(f)
    # For each img tag:
    for img in soup.findAll('img'):
        if img != None:
                if img['src'].startswith("/assets") == True:
                    # Open the image
                    pil ="/ruta/carpeta/imagenes" + img['src'])
                    # Get its size
                    width, height = pil.size
                    # Modify img tag with image size
                    img['width'] = str(width) + "px"
                    img['height'] = str(height) + "px"
            except KeyError:
    # Save the updated post
    with open(fname, "wb") as file:

Hope you find it useful, you can visit the script at github.


Spot a typo?: Help me fix it by contacting me or commenting below!


Maybe this posts are also worth reading