如何使用imagemagick进行“转换”以创建Google Earth金字塔文件

时间:2018-09-26 00:28:52

标签: imagemagick-convert

我有一个大图像,正在使用imagemagick转换为图块,以用于Google Earth KML,如此处所述 instructions on image pyramid construction 想法是将图像切成4件,然后是16件,再是64件,等等。

为简单起见,我将图像画布制作为4096x4096,以便将其分割将产生相同大小的文件。基本命令非常简单。例如:

  

转换large.png-作物512x512 tile.png

问题是convert命令会顺序创建文件名,而google需要行列的格式。例如,如果输出四个文件,则文件名应为:

tiles00.png
tiles01.png
tiles10.png
tiles11.png

我强行强制重命名脚本,最多可容纳64个文件,但是在进行256文件大小写操作之前,我想知道是否有一种更简单的方法来生成文件名。我正在使用linux。

2 个答案:

答案 0 :(得分:0)

这是Imagemagick 6中使用for循环的一种方法。

lena.png

enter image description here

lena.png图片为256x256。我选择128x128大小的图块。因此,对于四个输出图像,总共将有2行2列。

infile="lena.png"
tx=128
ty=128
ncols=`convert -ping "$infile" -format "%[fx:floor(w/$tx)]" info:`
nrows=`convert -ping "$infile" -format "%[fx:floor(h/$ty)]" info:`
for ((j=0; j<nrows; j++)); do
offy=$((j*ty))
for ((i=0; i<ncols; i++)); do
offx=$((i*tx))
convert lena.png -crop ${tx}x${ty}+${offx}+${offy} +repage lena_tile${j}${i}.png
done
done


lena_tile00

enter image description here

lena_tile01

enter image description here

lena_tile10

enter image description here

lena_tile11

enter image description here

另一种更紧凑的方法是将-set filename命令与fx计算一起使用,以命名图像链中的文件。

infile="lena.png"
tx=128
ty=128
ncols=`convert -ping "$infile" -format "%[fx:floor(w/$tx)]" info:`
nrows=`convert -ping "$infile" -format "%[fx:floor(h/$ty)]" info:`
convert "$infile" -crop ${tx}x${ty} -set filename:row_col "%[fx:floor(t/$nrows)]%[fx:mod(t,$ncols)]" "lena_tile%[filename:row_col].png"

请参阅: https://imagemagick.org/Usage/basics/#set https://imagemagick.org/script/fx.php

答案 1 :(得分:0)

您是否正在尝试制作自己的东西以了解该过程?如果不是这样,dzsave之类的现有工具可以在一个命令中为您快速构建完整的金字塔。例如:

$ vipsheader wtc.jpg 
wtc.jpg: 10000x10000 uchar, 3 bands, srgb, jpegload
$ /usr/bin/time -f %M:%e vips dzsave wtc.jpg x --layout google
211224:1.52
$ ls -R x | wc
   2404    2316   15186

因此,这是根据10,000 x 10,000像素JPG图像在目录x中创建2400个图块的Google样式金字塔。大约需要1.5秒和210mb的内存。

手册中有一章介绍dzsave

http://libvips.github.io/libvips/API/current/Making-image-pyramids.md.html