Python Image converter¶
As detailed all the way back in week2, I use VScode to edit the markdowns documents that make up the website you are currently reading.
This has worked really nicely for me, I especially love the markdown paste extension that allows me to paste clipboard images straight into documents. There is however a problem. The images always get pasted as PNG
PNG is a nice format for many reasons, such as having an alpha channel for transparency. However, it¨s compression is nonexistent.
For photos, which this site contains a fair amount of, JPEG is a much better format, as it has excellent image compression. However, as far as i know, my extension won’t paste natively into that.
I could, of course do like most upstanding citizens, and manually insert images as jpeg… Or we could AUTOMATE this thing
You can probably guess what i went for.
Que my code. ChatGPT wrote the basic structure, with some modification needed to actually have the desired effect.
import os
import glob
from PIL import Image
import shutil
index = 0
def convert_to_jpeg(image_path):
img = Image.open(image_path)
new_image_path = os.path.splitext(image_path)[0] + '.jpg'
img = img.convert('RGB')
img.save(new_image_path, quality=90, optimize=True)
return new_image_path
def update_markdown_files(directory, old_filename, new_filename):
markdown_files = glob.glob(os.path.join(directory, '*.md'))
for file in markdown_files:
with open(file, 'r') as f:
content = f.read()
updated_content = content.replace(old_filename, new_filename)
with open(file, 'w') as f:
f.write(updated_content)
def convert_png_to_jpeg(directory):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.png'):
print("found" + file + "\n")
png_path = os.path.join(root, file)
jpeg_path = convert_to_jpeg(png_path)
old_filename = file
new_filename = os.path.basename(jpeg_path)
update_markdown_files(root, old_filename, new_filename)
print("Saving as" + new_filename + "\n")
index =+ 1
os.remove(png_path)
# Specify the directory to walk through the r converts text into raw string which seems to help when copying from explorer
# Currently configured to go through my assignments and projects folders.
directory1 = r'C:\Users\User\Documents\GitHub\arthur-tollet\docs\assignments'
directory2 = r'C:\Users\User\Documents\GitHub\arthur-tollet\docs\projects'
# Convert PNG to JPEG, compress, update filenames in associated Markdown files, and delete PNG files in all subdirectories
convert_png_to_jpeg(directory1)
convert_png_to_jpeg(directory2)
print("converted " + index + " Images\n")
Demonstration¶
My projects folder before conversion has about 80 PNG images and is 44 megs in size.
After conversion, we find that the folder size is under half it’s original size!
The directory has some PNGs that shouldn’t be touched, such as website favicons etc. So it’s currently limited to the projects and assignments directories. On the other hand, it can now be run automatically at regular intervals to keep file sizes down. Anyway, current total website size has gone down from 190 to 105 megs, resulting in faster loading times as well. If needed, one could crank up the compression factor on the images as well to save even more space.
The script can be found here