How I Upload Pictures To My Blog

Uploaded on June 30, 2024

As I’ve stated in other blog posts – I hate social media, digital services, etc.

This website provides me with a way to display my work digitally over the internet without entering into the attention as commodity ecosystem that META, GOOG, etc have created.

Further, I don’t really like the idea of creating APPs. I love websites. Having a route where you can upload files feels very different from the way that I upload pictures from an aesthetic standpoint.

In my eyes, the aesthetics of website development are very much rooted in technologies like SFTP, SSH, HTML, JS, MYSQL. So these are the tools that I use.

Following these principles, these are the steps that I take to upload images to my blog.

Step 1:

Take pictures

Step 2:

Go through pictures individually, uploading to my local directories that are sorted by

year
└──month
└── day

Add the images that I want to be publicly viewable to these directories

Step 3:

Compress and upload the images. This is done through a python script that I created:

from PIL import Image
import os
import paramiko
import getpass  # For securely getting input from the user

def compress_image(input_path, output_path, max_size=3, quality=80):
    max_size_bytes = max_size * 1024 * 1024
    img = Image.open(input_path)

    while True:
        img.save(output_path, quality=quality, optimize=True)
        if os.path.getsize(output_path) <= max_size_bytes:
            break
        quality -= 5
        if quality < 10:
            break

    print(f"Compression complete for {os.path.basename(output_path)}. Final size: {os.path.getsize(output_path)/1024/1024:.2f} MB, Quality: {quality}")

def compress_images_in_directory(directory_path):
    compressed_files = []
    for filename in os.listdir(directory_path):
        if filename.lower().endswith('.jpg'):
            input_path = os.path.join(directory_path, filename)
            base, ext = os.path.splitext(filename)
            output_path = os.path.join(directory_path, f"c_{base}{ext.lower()}")
            compress_image(input_path, output_path)
            compressed_files.append(output_path)
    return compressed_files

def upload_files(sftp, files, remote_directory):
    try:
        sftp.chdir(remote_directory)  # Test if remote path exists
    except IOError:
        sftp.mkdir(remote_directory)  # Create if it does not exist
        sftp.chdir(remote_directory)

    for file in files:
        sftp.put(file, os.path.basename(file))  # Upload file
        print(f"Uploaded {os.path.basename(file)}")

def main():
    directory_path = input("Enter the path to the directory containing JPG images: ")
    if not os.path.isdir(directory_path):
        print("The specified path does not exist or is not a directory.")
        return

    compressed_files = compress_images_in_directory(directory_path)

    # SFTP Configuration
    host = "■■■.■■■.■■■.■■"
    username = "■■"
    key_file = "■■■■■■■■"
    passphrase = getpass.getpass("Enter your SSH key passphrase: ")  
    port = ■■■
    remote_directory = "■■■■■■■■■■/■■■■■"

    # Connect to SFTP using an SSH key with a passphrase
    private_key = paramiko.RSAKey.from_private_key_file(key_file, password=passphrase)
    transport = paramiko.Transport((host, port))
    transport.connect(username=username, pkey=private_key)
    sftp = paramiko.SFTPClient.from_transport(transport)

    try:
        upload_files(sftp, compressed_files, remote_directory)
    finally:
        sftp.close()
        transport.close()

if __name__ == "__main__":
    main()

After uploading the files I pwd copy and paste, enter my ssh key password and then it uploads the files using sftp to my remote server.

Step 4:

Once the files are on the server, then I SSH onto the server and run another python script here that adds a row to the database. I reference the name of the file and add the associated metadata (title,description,tags) via CLI.

Step 5:

Finally, because the site is a gatsby site, the pages aren’t created until build time, so I trigger a new gatsby build. Now, the images are live on my blog!