在图像上绘制具有特定大小的单元格的网格 [英] draw grid with cells of specific size over an image

查看:197
本文介绍了在图像上绘制具有特定大小的单元格的网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在图像上绘制网格,1px黑色边框没有填充?

How can I draw a grid, 1px black borders no fill, over an image?

网格的每个部分应为480x360px。

Each section of the grid should be 480x360px.

例如,鉴于此1440x1080px纯白色输入:

E.g., given this 1440x1080px solid white input:

它应该有一个3x3网格(因为9 480x360px矩形适合它),以产生这样的输出:

It should have a 3x3 grid drawn onto it (because 9 480x360px rectangles fit into it) to make an output kind of like this:

这不像我想要的那样准确(我只是用眼睛画矩形),但我希望它能说明我所追求的目标。

That's not as accurate as I'd like the command to be (I was just drawing rectangles by eye), but I hope it illustrates what I'm after.

推荐答案

这是一个读取输入图像的命令,创建一个带有黑色b的480x360透明单元顺序,创建输入图像大小的那些单元格的网格,并在输入上复合该网格......

Here is a command that will read an input image, create a 480x360 transparent cell with a black border, create a grid of those cells the size of the input image, and composite that grid over the input...

infile="MyPic.png"

convert "$infile" -size 480x360 -set option:distort:viewport "%[w]x%[h]" \
   \( xc:none -shave 1x1 -compose copy -bordercolor black -border 1x1 \) \
   -virtual-pixel tile -distort SRT 0 -compose over -composite output.png

这将使网格的线条粗细2像素。如果线条必须是1像素厚,那么可以使用这样的命令完成同样的事情......

That will make the lines of the grid 2 pixels thick. If the lines must be 1 pixel thick, the same sort of thing can be done with a command like this...

convert "$infile" -size 480x360 -set option:distort:viewport "%[w]x%[h]" \
   \( xc:none -chop 1x1 -background black -splice 1x1 \) \
   -virtual-pixel tile -distort SRT 0 -compose over -composite \
   -bordercolor black -shave 1x1 -compose copy -border 1x1 output.png

编辑添加:如果目标是将网格划分为特定数量的单元格而不是指定尺寸的单元格,则此类命令应该有效...

EDITED TO ADD: If the objective is to divide the grid into a particular number of cells rather than cells of specified dimensions, a command like this should work...

convert "$infile" \( +clone -channel A -evaluate set 0 +channel \
  -crop 3x4@ -chop 1x1 -background black -splice 1x1 \) -background none \
  -flatten -shave 1x1 -bordercolor black -border 1x1  output.png

创建输入图像的克隆并使其透明,我们es-crop 3x4 @将其裁剪成3x4单元格的网格,在单元格的顶部和左侧边缘放置黑色边框,然后通过将它们展平到输入图像上将它们重新组合成网格。它通过在右边和底边添加边框来完成,同时保留输入图像的原始尺寸。

That creates a clone of the input image and makes it transparent, uses "-crop 3x4@" to crop it into a grid of 3x4 cells, puts a black border on the top and left edges of the cells, then reassembles them into a grid by flattening them onto the input image. It finishes by adding a border to the right and bottom edges while preserving the input image's original dimensions.

显然使用此方法,单元格可能不会都具有完全相同的尺寸输入图像不能按单元格数均匀划分。

Obviously using this method the cells may not all have the exact same dimensions if the the input image can't be divided evenly by the number of cells.

再次编辑:如果你实际上不需要网格作为叠加,你可以直接将图像裁剪成图块,在图块周围放置黑色线条,然后重新组合它们以创建所需的输出图像。这可以通过这样一个简单的命令来完成480x360大小的单元格...

EDITED AGAIN: If you don't actually need the grid to be an overlay, you can directly crop the image into tiles, put black lines around the tiles, and reassemble them to create the required output image. That can be done with a simple command like this to make 480x360 sized cells...

convert "$infile" -background black -bordercolor black \
   -crop 480x360 -chop 1x1 -splice 1x1 -flatten -shave 1x1 -border 1x1 output.png

或者可以像这样使用-crop 3x3 @来制作一个3行3列的网格,让ImageMagick尽可能地计算尺寸......

Or it can be done with "-crop 3x3@" like this to make a grid of 3 rows and 3 columns letting ImageMagick calculate the sizes as nearly as possible...

convert "$infile" -background black -bordercolor black \
   -crop 3x3@ -chop 1x1 -splice 1x1 -flatten -shave 1x1 -border 1x1 output.png

同样,如果图像大小不能被数量整除单元格中,某些单元格的大小会有一个像素差异。

Again, if the image size isn't evenly divisible by the number of cells, there will be one pixel difference in the sizes of some of the cells.

这篇关于在图像上绘制具有特定大小的单元格的网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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