Thumbnails sheet #3954
Replies: 3 comments 8 replies
-
Hello @sergeevabc, You can do this by thumbnailing all the files in a directory, then assembling them with For example: #!/bin/bash
# expand a relative path
dir=$(realpath -s $1)
thumbnails=$dir/.thumbnails
mkdir -p $thumbnails
rm -rf $thumbnails/*
rm $dir/contact-sheet.jpg
echo "generating thumbnails ..."
parallel vipsthumbnail {} -o $thumbnails/%s.jpg --size 256x256 ::: $dir/*
echo "writing $dir/contact-sheet.jpg ..."
vips arrayjoin "$(echo $thumbnails/*)" $dir/contact-sheet.jpg \
--across 10 --halign centre --valign centre --shim 3 And run with:
To scan a directory and make a contact sheet of any recognised images, for example: You'd probably want to add support for filenames with spaces in, and perhaps subdirectories. You could use Of course your file manager can probably do this too. |
Beta Was this translation helpful? Give feedback.
-
You'd make a separate script to make a thumbnail with a label, perhaps #!/bin/bash
path=$(realpath -s $1)
file=$(basename $path)
name="${file%.*}"
dir=$(dirname $path)
thumbnails=$dir/.thumbnails
mkdir -p $thumbnails
width=$(vipsheader -f width $path)
height=$(vipsheader -f width $path)
bytes=$(stat -c %s $path)
txt="$file
${width}x${height}, $bytes"
vips text $thumbnails/$$-2.v "$txt" --font "courier 12" --rgba --align centre
vipsthumbnail $path -o $thumbnails/$$-1.v --size 256x256
vips gravity $thumbnails/$$-1.v $thumbnails/$$-3.v centre 256 256 \
--background 223
vips gravity $thumbnails/$$-3.v $thumbnails/$$-4.v north 256 300 \
--background 240
text_width=$(vipsheader -f width $thumbnails/$$-2.v)
text_height=$(vipsheader -f height $thumbnails/$$-2.v)
vips composite2 $thumbnails/$$-4.v $thumbnails/$$-2.v \
$thumbnails/$name.jpg over \
--x $(((256 - text_width) / 2)) --y $((300 - text_height - 5))
rm $thumbnails/$$-*.v Then modify the contact sheet thing above to be: #!/bin/bash
# expand a relative path
dir=$(realpath -s $1)
thumbnails=$dir/.thumbnails
mkdir -p $thumbnails
rm -rf $thumbnails/*
rm -f $dir/contact-sheet.jpg
echo "generating thumbnails ..."
parallel ./make-thumb {} ::: $dir/*
echo "writing $dir/contact-sheet.jpg ..."
vips arrayjoin "$(echo $thumbnails/*)" $dir/contact-sheet.jpg[Q=90] \
--across 3 --halign centre --valign centre --shim 20 --background 240 To make eg.: You'd need to rework it a bit for windows, of course. |
Beta Was this translation helpful? Give feedback.
-
I had a quick go at a python version, just for fun: #!/usr/bin/env python
import sys
import os
import glob
import multiprocessing
import pyvips
def thumbnail_and_label(filename):
name = os.path.basename(filename)
image = pyvips.Image.new_from_file(filename)
size = os.path.getsize(filename)
label = pyvips.Image.text(f"{name}\n{image.width}x{image.height}, {size}",
font="courier 12",
rgba=True,
align="centre")
return pyvips.Image.thumbnail(filename, 256) \
.gravity("centre", 256, 256, background=223) \
.gravity("north", 256, 310, background=240) \
.composite2(label, "over",
x=(256 - label.width) / 2,
y=300 - label.height - 5) \
.write_to_buffer(".ppm")
for dirname in sys.argv[1:]:
filenames = sorted(glob.glob(f"{dirname}/*"))
with multiprocessing.Pool(multiprocessing.cpu_count()) as p:
thumbs_ppm = p.map(thumbnail_and_label, filenames)
thumbs = [pyvips.Image.new_from_buffer(thumb, "") for thumb in thumbs_ppm]
sheet = pyvips.Image.arrayjoin(thumbs,
across=3,
halign="centre",
valign="centre",
shim=20,
background=240)
sheet \
.gravity("centre",
sheet.width + 40,
sheet.height + 40,
background=240) \
.write_to_file(f"{dirname}/contact-sheet.jpg", Q=85) It's about twice the speed of the Each thumbnail has to be encoded as ppm to be posted back to the main process, which is unfortunate. A C/C++ implementation could avoid this for a possible speedup. |
Beta Was this translation helpful? Give feedback.
-
I like the idea of having a sheet with thumbnails of images with the corresponding filenames. Thus, we save time: instead of looking through all the images in search of the right one, just look at that sheet. But how to generate one with VIPS?
Beta Was this translation helpful? Give feedback.
All reactions