Efficiently generating thumbnails with ImageMagick and convert

前端 未结 5 1421
栀梦
栀梦 2020-12-25 09:06

I\'m looking to efficiently generate various sized thumbnails with ImageMagick\'s convert utility in Python. Some of my image files are quite large (~15MB JPGs).

One

5条回答
  •  难免孤独
    2020-12-25 09:47

    I tried timing vipsthumbnail against @KurtPfeifle's excellent answer. I ran this with a 10k x 10k pixel RGB JPG image (about 15MB):

    convert                           \
      /data/john/pics/wtc.jpg         \
     -quality 80                      \
     -colorspace rgb                  \
     +profile '*'                     \
     -filter Lanczos                  \
     -write mpr:copy-of-huge-original \
     +delete                          \
      mpr:copy-of-huge-original -resize '1024x768'  -write sample-1024x768.jpg +delete \
      mpr:copy-of-huge-original -resize '800x600'   -write sample-800x600.jpg  +delete \
      mpr:copy-of-huge-original -resize '400x300'   -write sample-400x300.jpg  +delete \
      mpr:copy-of-huge-original -resize '200x150'   -write sample-200x150.jpg  +delete \
      mpr:copy-of-huge-original -resize '163x163!>' -write sample-163x163.jpg  x.jpg
    

    I found I needed the extra x.jpg at the end, I'm not sure why. On this machine (E5-1650 3.2 GHz, IM 6.8.9-9) I see:

    $ time ./m.sh 
    real    0m6.560s
    user    0m31.908s
    sys     0m0.264s
    peak RES 711MB
    

    This is (I think) the equivalent with vipsthumbnail:

    img=/data/john/pics/wtc.jpg
    icc=/usr/share/color/argyll/ref/sRGB.icm
    
    for size in 1024x768 800x600 400x300 200x150 163x163; do
      vipsthumbnail $img --size $size --eprofile $icc -o vips-sample-$size.jpg[Q=80]
    done
    

    vipsthumbnail defaults to Lanczos3. Timing it with vips-8.4.4 I see:

    $ time ./n.sh
    real    0m2.376s
    user    0m2.412s
    sys     0m0.108s
    peak RES 70MB
    

    So a useful speedup, and a large drop in memory use.

    Because memory use is low, you can run many vipsthumbnail in parallel without killing your server. If I change the script to be:

    img=/data/john/pics/wtc.jpg
    icc=/usr/share/color/argyll/ref/sRGB.icm
    
    parallel vipsthumbnail $img \
      --size {} --eprofile $icc -o vips-sample-{}.jpg[Q=80] ::: \
      1024x768 800x600 400x300 200x150 163x163
    

    I now see:

    $ time ./n.sh 
    real    0m0.593s
    user    0m1.960s
    sys     0m0.100s
    peak RES 280MB
    

    More than 10x faster than ImageMagick.

提交回复
热议问题