How to merge images in command line?

2019-03-08 05:02发布

I would like to try the CSS Sprite technique to load a few thumbnails as a single image. So I need to "merge" a few thumbnails in a single file offline in the server.

Suppose I have 10 thumbnails of the same size. How would you suggest I "merge" them from Linux command line?

4条回答
贪生不怕死
2楼-- · 2019-03-08 05:11

You can also use GraphicsMagick, a lighter and faster fork of ImageMagick:

gm convert image1.png image2.png -append combined.png

A simple time comparison of merging 12 images:

time convert image{1..12}.jpg -append test.jpg

real    0m3.178s
user    0m3.850s
sys     0m0.376s

time gm convert image{1..12}.jpg -append test.jpg

real    0m1.912s
user    0m2.198s
sys     0m0.766s

GraphicsMagick is almost twice as fast as ImageMagick.

查看更多
叛逆
3楼-- · 2019-03-08 05:14

Use the pnmcat of the netpbm-package.

You probably have to convert your input files to and fro for using it:

pnmcat -lr <(pngtopnm 1.png) <(pngtopnm 2.png) | pnmtopng > all.png
查看更多
做个烂人
4楼-- · 2019-03-08 05:19

You can also try ImageMagic which is great for creating CSS sprites. Some tutorial about it here.

Example (vertical sprite):

convert image1.png image2.png image3.png -append result/result-sprite.png

Example (horizontal sprite):

convert image1.png image2.png image3.png +append result/result-sprite.png
查看更多
我命由我不由天
5楼-- · 2019-03-08 05:30

If you prefer to merge the pictures from left to right, use the following command:

convert image{1..0}.png +append result/result-sprite.png

Note the +append instead of -append.

查看更多
登录 后发表回答