imageMagick: 多个不同图像的六边形平铺(具有相同的大小)

huangapple go评论61阅读模式
英文:

imageMagick: hexagonal tiling of many different images (having the same size)

问题

I have many different PNG images all with the same size (142x163). These are badges for students. You can see three examples below, but there are more of them. I would like to create a single large image where all individual badges are arranged in a hexagonal grid to maximize the printed area. I just need to leave a little space between them to cut them out safely.

I'm wondering what the best way is to go about this.

I have a list of how many badges of each kind I need to print, and I can write code to specify the position (offset) of each individual badge in the hexagonal grid. However, I'm not sure about the correct way to feed this information into a script for ImageMagick to execute.

I guess I need something like: create an image placing badge1.png at position 1, badge2.png at position 2, badge3.png at position 3, badge3.png at position 4, and so on. This could go on for a bit, with 50 to 100 badges (I'm thinking of filling an A3 sheet with 3cm x 3cm badges).

What would the correct syntax be? Would ImageMagick handle this?

Thanks a lot for any insight.

Francesco

EDIT: as required, I'm attaching the script I'm currently using:

magick -background none \
\( ph4campoBstatico.png ph5EqMaxOndeEM.png MMprodVett.png ph4UeV_elettrici.png +smush 8 +repage -set page +0+0 \) \
\( MMsimboli.png ph3dinamicaSNI.png ph1prefissi.png ph1calibroVentesimale.png +smush 8 +repage -set page +76+130 \) \
-layers merge +repage \
-background white -flatten \
result.png

This command works, but it converts an entire "row" to grayscale whenever the first image is grayscale (see output below).

Since the number of files involved is much larger than 8, I tried to write this command in a file "script" and run it as magick -script script. What I find is that I have to prepend -write to the filename. Also, it seems to me that the backslashes at the end of each line are unimportant in the file.

The output of the command is

imageMagick: 多个不同图像的六边形平铺(具有相同的大小)

Only the first image in the second row should be black and white. The others should be in color. Colors are "removed" from a row only when the first image is grayscale.

英文:

I have many different PNG images all with the same size (142x163). These are badges for students. You can see three examples below, but there are more of them. I would like to create a single large image where all individual badges are arranged in a hexagonal grid to maximize the printed area. I just need to leave a little space between them to cut them out safely.

I'm wondering what the best way is to go about this.

I have a list of how many badges of each kind I need to print, and I can write code to specify the position (offset) of each individual badge in the hexagonal grid. However, I'm not sure about the correct way to feed this information into a script for ImageMagick to execute.

I guess I need something like: create an image placing badge1.png at position 1, badge2.png at position 2, badge3.png at position 3, badge3.png at position 4, and so on. This could go on for a bit, with 50 to 100 badges (I'm thinking of filling an A3 sheet with 3cm x 3cm badges).

What would the correct syntax be? Would ImageMagick handle this?

Thanks a lot for any insight.

Francesco

[![badge1][1]][1]
[![badge2][2]][2]
[![badge3][3]][3]

imageMagick: 多个不同图像的六边形平铺(具有相同的大小)
imageMagick: 多个不同图像的六边形平铺(具有相同的大小)
imageMagick: 多个不同图像的六边形平铺(具有相同的大小)

EDIT: as required, I'm attaching the script I'm currently using:

magick -background none \
\( ph4campoBstatico.png ph5EqMaxOndeEM.png MMprodVett.png ph4UeV_elettrici.png +smush 8 +repage -set page +0+0 \) \
\( MMsimboli.png ph3dinamicaSNI.png ph1prefissi.png ph1calibroVentesimale.png +smush 8 +repage -set page +76+130 \) \
-layers merge +repage \
-background white -flatten \
result.png

This command works, but it converts an entire "row" to grayscale whenever the first image is grayscale (see output below).

Since the number of files involved is much larger than 8, I tried to write this command in a file "script" and run it as magick -script script. What I find is that I have to prepend -write to the filename. Also, it seems to me that the backslashes at the end of each line are unimportant in the file.

The output of the command is

imageMagick: 多个不同图像的六边形平铺(具有相同的大小)

Only the first image in the second row should be black and white. The others should be in color. Colors are "removed" from a row only when the first image is grayscale.

答案1

得分: 3

以下是使用Imagemagick的另一种方法来完成这个操作的示例。使用smush X来创建每一行。Smush X类似于带有偏移的append。然后使用一个-page +X+Y来设置两行之间的间距。最后使用-layers merge根据-page偏移组合它们。然后将其展平到所需的背景颜色上,或保留为透明背景。

magick -background none \
\( img1.png img2.png img3.png +smush 4 +repage \) \
\( img3.png img1.png img2.png +smush 4 +repage -set page +72+126 \) \
-layers merge +repage \
-background white -flatten \
result.png
magick -background none \
\( img1.png img2.png img3.png +smush 4 +repage \) \
\( img3.png img1.png +smush 4 -set page +72+126 \) \
-layers merge +repage \
-background white -flatten \
result2.png

imageMagick: 多个不同图像的六边形平铺(具有相同的大小)

imageMagick: 多个不同图像的六边形平铺(具有相同的大小)


<details>
<summary>英文:</summary>

Here is an alternate way to do that in Imagemagick. Use smush X to create each row. Smush X is like append with an offset.  Then use one -page +X+Y to set the spacing between the two rows. And finally use -layers merge to combine according to the -page offsets. Then flatten over the desired background color or leave as transparent background.

    magick -background none \
    \( img1.png img2.png img3.png +smush 4 +repage \) \
    \( img3.png img1.png img2.png +smush 4 +repage -set page +72+126 \) \
    -layers merge +repage \
    -background white -flatten \
    result.png

[![enter image description here][1]][1]


    magick -background none \
    \( img1.png img2.png img3.png +smush 4 +repage \) \
    \( img3.png img1.png +smush 4 -set page +72+126 \) \
    -layers merge +repage \
    -background white -flatten \
    result2.png

[![enter image description here][2]][2]


  [1]: https://i.stack.imgur.com/uqShe.png
  [2]: https://i.stack.imgur.com/hJB9k.png

</details>



# 答案2
**得分**: 3

给定一些具有相同尺寸和透明背景的图像,命名为"tile-*.png",你可以使用ImageMagick v7在Windows命令中将它们排列成六边形方向的网格,命令如下...

首先,这个命令在一个200x200的瓷砖上绘制一个六边形,然后复制它以创建更多相同的瓷砖,例如在这个示例中是35个,以模拟一堆要排列的瓷砖...

```shell
magick -size 200x200 -background none -fill none ^
   -strokewidth 4 -stroke black xc:none ^
   -draw "polygon 22,54 100,10 178,54 178,146 100,190 22,146" ^
   -duplicate 34 tile-%02d.png

这个下一个命令将读取上述命令创建的所有瓷砖,并将它们排列成指定列数的网格...

set COLS=5

magick tile-*.png ^
   -background none ^
   -gravity center ^
   -extent %[h]x%[h] ^
   +gravity ^
   +append ^
   +repage ^
   -crop %[fx:%COLS%*h]x%[h] ^
   -splice %[fx:(t%2)*h/2]x0 ^
   -set page +0+%[fx:t*h*0.866] ^
   -layers merge ^
      result.png

这将设置你想要的列数作为Windows变量%COLS%。

然后,ImageMagick命令将读取所有名为"tile-*.png"的图像,并确保它们居中在正方形瓷砖中。

它将它们全部水平拼接在一起,将其裁剪成所需的行数以形成网格,并调整行末的间距。

接下来,它在所有这些水平图像条带上设置了页面几何属性,从而将每个图像条带相对于前一个向下移动"h*0.866"的距离。

当所有几何属性都设置好后,"-layers merge"操作将它们合并成最终的网格。

附加说明:我现在不能将这个命令转换成*nix命令,但将插入符号"^"转换成反斜杠"\",并修复shell变量的语法,应该能让你完成大部分工作。

英文:

Given a number of images with the same dimensions, named "tile-*.png", and with transparent backgrounds, you can use ImageMagick v7 to arrange them into a hexagon-oriented grid with a Windows command like the one below...

First, this command draws a hexagon on a 200x200 tile, duplicates it to make several more, 35 in this example, to simulate a bunch of tiles to arrange...

magick -size 200x200 -background none -fill none ^
   -strokewidth 4 -stroke black xc:none ^
   -draw &quot;polygon 22,54 100,10 178,54 178,146 100,190 22,146&quot; ^
   -duplicate 34 tile-%02d.png

imageMagick: 多个不同图像的六边形平铺(具有相同的大小)

This next command will read in all the tiles created by the above command, and arrange them into a grid of a specified number of units wide...

set COLS=5

magick tile-*.png ^
   -background none ^
   -gravity center ^
   -extent %[h]x%[h] ^
   +gravity ^
   +append ^
   +repage ^
   -crop %[fx:%COLS%*h]x%[h] ^
   -splice %[fx:(t%2)*h/2]x0 ^
   -set page +0+%[fx:t*h*0.866] ^
   -layers merge ^
      result.png

That sets the number of columns you want as a Windows variable %COLS%.

Then the ImageMagick command reads in all the images named "tile-*.png" and makes sure they're centered in square tiles.

It appends them all horizontally, crops that into the number of rows required to make the grid, and adjusts the spacing on the ends of the rows.

Next it sets the page geometry on all these horizontal strips of images, in essence moving each one "h*0.866" farther down the page than the previous.

With all the geometry set, the "-layers merge" operation flattens them into the finished grid.

imageMagick: 多个不同图像的六边形平铺(具有相同的大小)

Edisted to add: I'm not able to translate this into a *nix command right now, but converting the carets "^" to backslashes "\" and fixing the syntax on that shell variable should get you most of the way.

答案3

得分: 2

这应该是GeeMack的Imagemagick解决方案的Unix等效版本。

COLS=5

magick tile-*.png \
   -background none \
   -gravity center \
   -extent "%wx%w" \
   +gravity \
   +append \
   +repage \
   -crop "%[fx:$COLS*h]x%h" \
   -splice "%[fx:(t%2)*h/2]x0" \
   -set page "+0+%[fx:t*h*0.866]" \
   -layers merge +repage \
      result.png
英文:

This should be the Unix equivalent of GeeMack's solution for Imagemagick

COLS=5

magick tile-*.png \
   -background none \
   -gravity center \
   -extent &quot;%wx%w&quot; \
   +gravity \
   +append \
   +repage \
   -crop &quot;%[fx:$COLS*h]x%h&quot; \
   -splice &quot;%[fx:(t%2)*h/2]x0&quot; \
   -set page &quot;+0+%[fx:t*h*0.866]&quot; \
   -layers merge +repage \
      result.png

答案4

得分: 1

我认为我找到了实现我想要的方式。虽然我不确定这是否是最佳方式。

所以,受到这个帖子的启发,我编写了一个名为hexScript的脚本,类似于以下内容:

#!/path/to/magick -script
-define png:color-type=6
( MMderivate.png -repage +0+0 )
( MMderivate.png -repage +150+0 )
( MMderivate.png -repage +300+0 )
( MMsimboli.png -repage +75+130 )
( MMsimboli.png -repage +225+130 )
( MMpercentuali.png -repage +375+130 )
-layers merge -write out.png

我可以使用magick -script hexScript来运行它。输出基本上就是我正在寻找的(见下文)。

当然,在我的情况下,像( file.png -repage +X+Y )这样的行数将远远超过六行,但我认为ImageMagick可以处理(这个帖子中的原帖作者谈到了成千上万个图像)。

现在我只需要编写一个根据我需要的每种徽章数量的信息自动生成所需脚本的代码。

欢迎提供改进这种方法的任何见解。

非常感谢

Francesco

imageMagick: 多个不同图像的六边形平铺(具有相同的大小)

英文:

I think I found a way to achieve what I want. I'm not sure it's the best way, though.

So, inspired by this post, I wrote a script hexScript like this

#!/path/to/magick -script
-define png:color-type=6
( MMderivate.png -repage +0+0 )
( MMderivate.png -repage +150+0 )
( MMderivate.png -repage +300+0 )
( MMsimboli.png -repage +75+130 )
( MMsimboli.png -repage +225+130 )
( MMpercentuali.png -repage +375+130 )
-layers merge -write out.png

which i can run with magick -script hexScript. The output is pretty much what I'm looking for (see below).

Of course, in my case the number of lines like ( file.png -repage +X+Y ) is going to be way larger than six, but I think imagemagick can handle that (the OP in this post talks about thousands of images).

Now I just have to write a code that automatically produces the script I need, based on the information of how many badges of each kind I need.

Any insight for improving this approach is welcome.

Thanks a lot

Francesco

imageMagick: 多个不同图像的六边形平铺(具有相同的大小)

答案5

得分: 1

尝试在Imagemagick中读取图像后添加-colorspace sRGB或-type truecolor。如果前者单独不起作用,但仅适用于PNG输出,请在输出文件名之前加上PNG24:

magick -background none \
\( ph4campoBstatico.png ph5EqMaxOndeEM.png MMprodVett.png ph4UeV_elettrici.png -colorspace sRGB +smush 8 +repage -set page +0+0 \) \
\( MMsimboli.png ph3dinamicaSNI.png ph1prefissi.png ph1calibroVentesimale.png -colorspace sRGB +smush 8 +repage -set page +76+130 \) \
-layers merge +repage \
-background white -flatten \
PNG24:result.png

不要翻译代码部分。

英文:

Try adding -colorspace sRGB or -type truecolor after reading the images in Imagemagick. Also prepend PNG24: to the output filename if the previous does not work by itself but only for PNG output.

magick -background none \
\( ph4campoBstatico.png ph5EqMaxOndeEM.png MMprodVett.png ph4UeV_elettrici.png -colorspace sRGB +smush 8 +repage -set page +0+0 \) \
\( MMsimboli.png ph3dinamicaSNI.png ph1prefissi.png ph1calibroVentesimale.png -colorspace sRGB +smush 8 +repage -set page +76+130 \) \
-layers merge +repage \
-background white -flatten \
PNG24:result.png

huangapple
  • 本文由 发表于 2023年6月16日 06:36:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定