`
MEDIA THUMBNAIL GENERATOR

This are very (very) simple script that generates thumbnails for every image / video in the specified folder. The thumbnails preserve the original filename and are placed inside the thumbnails folder.


For .jpg, .gif & .png images

#!/usr/bin/python3

import os
import subprocess
import PIL.Image  # or import PIL.image depedning on your installation

dir = '/home/username/Downloads/img/'
thumbnailDir = dir+'thumbnails/'
thumbnailWidth = 256
thumbnailHeight = 256
thumbnailQuality = 40

imgName = ''
imgFormat = ''
thumbnailName = ''
tbFormat = ''


if (not os.path.isdir(thumbnailDir)):
    print(f"Creating thumbnail folder in {thumbnailDir}")
    os.mkdir(thumbnailDir)


for fn in os.listdir(dir):
    f = os.path.join(dir, fn)
    if os.path.isfile(f):

        ar = f.split('/')
        imgName = ar[len(ar) - 1]
        imgFormat = imgName[-3:]

        if (imgFormat == "jpg"):
            tbFormat = "JPEG"
        if (imgFormat == "png"):
            tbFormat = "PNG"
        if (imgFormat == "gif"):
            tbFormat = "GIF"

        imageFile = PIL.Image.open(f)
        imageFile.thumbnail([thumbnailWidth, thumbnailHeight])
        imageFile.save(
            thumbnailDir+imgName[:-3]+imgFormat, tbFormat, optimize=True, quality=thumbnailQuality)
        print(thumbnailDir+imgName[:-3] + imageFile.format)

                
For .webms

#!/usr/bin/python3

import os
import subprocess

# Folder containing the videos to process
dir = '/home/username/Downloads/videos/'
thumbnailDir = dir+'thumbnails/'
thumbnailWidth = 400
thumbnailHeight = 222
thumbnailFileName = ''


if (not os.path.isdir(thumbnailDir)):
    print(f"Creating thumbnail folder in {thumbnailDir}")
    os.mkdir(thumbnailDir)


for fn in os.listdir(dir):
    f = os.path.join(dir, fn)
    if os.path.isfile(f):
        ar = f.split('/')
        thumbnailFileName = (ar[len(ar) - 1])[:-4]+'jpg'
        print(f + ' -> ' + thumbnailFileName)
        os.system(
            f'ffmpeg -i {f} -vframes 1 -an -s {thumbnailWidth}x{thumbnailHeight} -ss 1 {thumbnailDir+thumbnailFileName}')

                

The media in the gallery uses thumbnails that were generated with these scripts.

`