使用ImageMagick高效生成缩略图并进行转换 [英] Efficiently generating thumbnails with ImageMagick and convert

查看:149
本文介绍了使用ImageMagick高效生成缩略图并进行转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用Python中的ImageMagick转换实用程序有效地生成各种大小的缩略图。我的一些图像文件相当大(约15MB JPG)。

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 way I could do it would be to take the full-sized image, and to generate the various thumbnails from the full-sized image, as follows:

convert sample_image.jpg -resize 1024x768  sample_image-1024x768.jpg
convert sample_image.jpg -resize 800x600   sample_image-800x600.jpg
convert sample_image.jpg -resize 400x300   sample_image-400x300.jpg
convert sample_image.jpg -resize 200x150   sample_image-200x150.jpg

但另一种方法是彼此调整图像大小:

But another way would be to resize the images from each other:

convert sample_image.jpg           -resize 1024x768  sample_image-1024x768.jpg
convert sample_image-1024x768.jpg  -resize 800x600   sample_image-800x600.jpg
convert sample_image-800x600.jpg   -resize 400x300   sample_image-400x300.jpg
convert sample_image-400x300.jpg   -resize 200x150   sample_image-200x150.jpg

这样做是否有任何不利因素,或者更好的方法?看起来这会更有效率。

Is there any downside to doing this, or perhaps a better way? It seems like this would be a lot more efficient.

作为必然结果,是否有任何标志或技巧转换用途来加速这个过程?

As a corollary, are there any flags or "tricks" convert uses to speed up the process?

推荐答案

ImageMagick有一些技巧可以帮助您在处理大型图像时优化速度,并且当您想要从同一原始图像创建不同的输出时:

ImageMagick has a few tricks up its sleeves which help you to optimize for speed when you want to process large images and when you want to create different output from the same original:


  1. 利用ImageMagick的 mpr:{name} 功能,这会暂时保存将图像输入到命名的内存程序寄存器中,您可以稍后(处理时)读取数据的速度比硬盘上的速度快得多。

  1. Make use of ImageMagick's mpr:{name} feature, which makes it temporarily save the input image into a named memory program register, from which you can later (while processing) read the data much faster than you could do from harddisk.

一个进程中进行所有调整大小操作,写出所需的不同输出大小。

Do all resize operations in one single process writing out the different output sizes you require.

更好的消息是,您可以将这两者合并为一个命令。

And the even better news is you can combine both these into one single command.

因此,您不需要使用所有上下文切换开销来运行多个进程 - 一次完成所有操作。

So you do not need to run multiple processes with all their context-switching overhead -- do it all in one go.

以下示例还会从原始图像中裁剪两个单独的区域,并从中创建重新调整大小的缩略图,以显示IM在一个命令行中可以执行的操作数量。它当然也会输出您要求的尺寸。 (当然,你需要一个非常大尺寸的输入图像来使裁剪参数起作用)。

The following example also crops two separate areas from the original image and creates re-sized thumbnails from them, just to show how many different operations IM can do in one commandline. It also, of course outputs the sizes you requested. (You'll need, of course, a really large-dimensioned input image for the cropping parameters to work).

convert                           \
  huge-original.jpg               \
 -quality 80                      \
 -colorspace rgb                  \
 +profile '*'                     \
 -filter Lanczos                  \
 -write mpr:copy-of-huge-original \
 +delete                          \
  mpr:copy-of-huge-original -crop '3000x2000+0+480'   -resize '200x125!>' -write thumb1-extract.jpg +delete \
  mpr:copy-of-huge-original -crop '2000x1500+280+220' -resize '75x75!>'   -write thumb2-extract.jpg +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






更新



我现在才看到@JonathanOng提出的问题:如何将输出流式传输到< ; stdout>

假设您希望stdout的格式也是JPEG,您可以试试这个:

Assuming, you want the format going to stdout also be JPEG, you can try this:

convert                           \
  huge-original.jpg               \
 -quality 80                      \
 -colorspace rgb                  \
 +profile '*'                     \
 -filter Lanczos                  \
 +write mpr:copy-of-huge-original \
  mpr:copy-of-huge-original -crop '3000x2000+0+480'   -resize '200x125!>' +write thumb1-extract.jpg \
  mpr:copy-of-huge-original -crop '2000x1500+280+220' -resize '75x75!>'   +write thumb2-extract.jpg \
  mpr:copy-of-huge-original -resize '1024x768'  +write jpeg:- \
  mpr:copy-of-huge-original -resize '800x600'   +write jpeg:- \
  mpr:copy-of-huge-original -resize '400x300'   +write jpeg:- \
  mpr:copy-of-huge-original -resize '200x150'   +write jpeg:- \
  mpr:copy-of-huge-original -resize '163x163!>' +write jpeg:-

这样每个变体都会转到stdout。你如何处理这一连续图像流,取决于你...

This way each variant will go to stdout. How you deal with this stream of consecutive images then, is up to you...

注意 ,而不是写 -write filename + delete 你可以使用 + write filename 。它的效果相同。

Note, instead of writing -write filename +delete you can use +write filename. It amounts to the same effect.

这篇关于使用ImageMagick高效生成缩略图并进行转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆