Skip to content

Instantly share code, notes, and snippets.

@ajmas
Last active September 11, 2024 03:40
Show Gist options
  • Save ajmas/45554fbb34eb87dc830817219efc7637 to your computer and use it in GitHub Desktop.
Save ajmas/45554fbb34eb87dc830817219efc7637 to your computer and use it in GitHub Desktop.
Creating thumbnail for different media
#!/bin/sh
## Draft tool to create thumbnails from office documents, since unoconv wasn't working for me.
soffice_cmd="/Applications/LibreOffice.app/Contents/MacOS/soffice"
input_file="$1"
extension="${input_file##*.}"
noext_filepath="${input_file%.*}"
tmp_filepath="${TMPDIR}docthumb-$(date +%s)"
output_format=jpg
cd "${TMPDIR}"
cp "${input_file}" "${tmp_filepath}.${extension}"
"${soffice_cmd}" --headless --convert-to pdf "${tmp_filepath}.${extension}"
gs -dNOPAUSE -dBATCH -r400 -sDEVICE=png16m -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sOutputFile="${tmp_filepath}.png" -dLastPage=1 "${tmp_filepath}.pdf"
magick mogrify -format ${output_format} -write "${noext_filepath}_thumb.${output_format}" -path "${TMPDIR}" -thumbnail 512x512 "${tmp_filepath}.png"
rm "${tmp_filepath}.png"
rm "${tmp_filepath}.${extension}"
rm "${tmp_filepath}.pdf"
rm "${tmp_filepath}.${output_format}"
echo "thumbnail written to '${noext_filepath}_thumb.${output_format}'"

Creating thumbnail for different media

Everything here expects a Unix type environment, as found on Linux or macOS X.

By File Type

Video

Using FFMPEG:

ffmpeg -i input.mp4 -ss 00:00:01.000 -vframes 1 output.png

Image

Using Image Magick (more info):

mkdir thumbs
magick mogrify  -format gif -path thumbs -thumbnail 512x512 *.jpg

PDF

Using Ghostscript,

To JPG:

gs -dNOPAUSE -dBATCH -sDEVICE=jpeg -sOutputFile=output.jpg -dLastPage=1 "input.pdf"

To PNG:

gs -dNOPAUSE -dBATCH -sDEVICE=png16m -sOutputFile=output.png -dLastPage=1 "input.pdf"

Then scale with Image Magick, as described in the "image" section

PowerPoint, Word, Excel

Using unoconv and Libreoffice (more info), though ruan into a few issues:

unoconv --export Quality=100 filename.pptx filename.pdf 

Using Libreoffice, Ghostscript and Image Magick:

input_file=inputfile.pptx
noext_filepath="${input_file%.*}"
soffice --headless --convert-to pdf "${input_file}"
gs -dNOPAUSE -dBATCH -sDEVICE=png16m -sOutputFile="${noext_filepath}.png" -dLastPage=1 "${noext_filepath}.pdf"
magick mogrify -format jpg -write "${noext_filepath}_thumb.png" -thumbnail 512x512 "${noext_filepath}.png"

Notes:

  • on macOS X using soffice as /Applications/LibreOffice.app/Contents/MacOS/soffice
  • support for MS Office docs: pptx, docx, xlsx, per testing
  • support for Apple Office docs, though with some limitations: key, numbers, pages
    • it was hit and miss, with many outputted PDFs being blank

Libraries for Languages

Nodejs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment