【问题标题】:Create png rectangle with rulers along edges?沿着边缘创建带有标尺的png矩形?
【发布时间】:2017-05-20 21:16:34
【问题描述】:

我可以在 ImageMagick 中创建一个每个边缘都有一个像素标尺的白色 PNG 矩形吗?例如。每 50 个像素是一行和文字?或者至少一行。

有点像:

但是这样我就可以创建任意大小。

【问题讨论】:

标签: image imagemagick


【解决方案1】:

你可以做这样的事情。它只是实际代码的一半,因为我已将其逻辑展开并将其注释为块以便于理解:

#!/bin/bash
WIDTH=480        # Canvas width
HEIGHT=120       # Canvas height
TL=10            # Tick length

# Draw ticks using a loop across width of ruler
for ((i=0;i<$WIDTH;i+=10)); do

   # Decide tick length, doubling length if multiple of 100
   r=$TL
   [[ $((i%100)) -eq 0 ]] && ((r=2*r))

   # Draw ticks along top edge of ruler
   echo line $i,0 $i,$r

   # Draw ticks along bottom edge of ruler
   echo line $i,$HEIGHT $i,$((HEIGHT-r))

done | convert -size ${WIDTH}x${HEIGHT} xc:yellow -draw @- result.png

脚本中的for 循环生成如下绘图命令,并将它们传递给最后的单个convert 命令,由于-draw @-,该命令从其标准输入中读取它们

line 0,0 0,20
line 0,120 0,100
line 10,0 10,10
line 10,120 10,110
line 20,0 20,10
line 20,120 20,110
line 30,0 30,10
line 30,120 30,110

我没有一整天的时间来试验编号标签的位置,但是如果您在文件底部的 done 之前添加以下几行,您将获得标签:

# Add numbering labels
if [ $((i%100)) -eq 0 ]; then
   echo text $i,$((HEIGHT/2)) \"$i\"
fi


关于垂直边的刻度,有几种方法可以做到这一点,它们越来越依赖bash 功能 - 例如复合语句等。为了简单起见,我可能会从上面的原始部分获取图像并重新加载它以绘制垂直线,然后重新保存。它看起来像这样:

#!/bin/bash
WIDTH=480        # Canvas width
HEIGHT=120       # Canvas height
TL=10            # Tick length

# Draw ticks using a loop across width of ruler
for ((i=0;i<$WIDTH;i+=10)); do

   # Decide tick length, doubling length if multiple of 100
   r=$TL
   [[ $((i%100)) -eq 0 ]] && ((r=2*r))

   # Draw ticks along top edge of ruler
   echo line $i,0 $i,$r

   # Draw ticks along bottom edge of ruler
   echo line $i,$HEIGHT $i,$((HEIGHT-r))

   # Add numbering labels
   if [ $((i%100)) -eq 0 ]; then
      echo text $i,$((HEIGHT/2)) \"$i\"
   fi

done | convert -size ${WIDTH}x${HEIGHT} xc:yellow -draw @- result.png

### NEW PART FOR VERTICAL EDGES FOLLOWS 

# Draw ticks down sides of ruler
for ((i=10;i<$((HEIGHT-10));i+=10)); do

   # Decide tick length, doubling length if multiple of 100
   r=$TL
   [[ $((i%100)) -eq 0 ]] && ((r=2*r))

   # Draw ticks along left edge of ruler
   echo line 0,$i $r,$i

   # Draw ticks along right edge of ruler
   echo line $((WIDTH-r)),$i $WIDTH,$i

done | convert result.png -draw @- result.png


此版本中的更多改进:

#!/bin/bash
WIDTH=730        # Canvas width
HEIGHT=310       # Canvas height
TL=10            # Tick length

# Draw ticks using a loop across width of ruler
for ((i=0;i<$WIDTH;i+=10)); do

   # Decide tick length, doubling length if multiple of 100
   r=$TL
   [[ $((i%50)) -eq 0 ]] && ((r=3*TL/2))
   [[ $((i%100)) -eq 0 ]] && ((r=2*TL))

   # Draw ticks along top edge of ruler
   echo line $i,0 $i,$r

   # Draw ticks along bottom edge of ruler
   echo line $i,$HEIGHT $i,$((HEIGHT-r))

   # Add numbering labels
   if [ $((i%100)) -eq 0 ]; then
      echo text $i,$((HEIGHT/8)) \"$i\"
      echo text $i,$((7*HEIGHT/8)) \"$i\"
   fi

done | convert -size ${WIDTH}x${HEIGHT} xc:yellow -draw @- result.png

# Draw ticks down sides of ruler
for ((i=10;i<$((HEIGHT-10));i+=10)); do

   # Decide tick length, doubling length if multiple of 100
   r=$TL
   [[ $((i%50)) -eq 0 ]] && ((r=3*TL/2))
   [[ $((i%100)) -eq 0 ]] && ((r=2*TL))

   # Draw ticks along left edge of ruler
   echo line 0,$i $r,$i

   # Draw ticks along right edge of ruler
   echo line $((WIDTH-r)),$i $WIDTH,$i

   # Add numbering labels
   if [ $((i%100)) -eq 0 ]; then
      echo text 40,$i \"$i\"
      echo text $((WIDTH-80)),$i \"$i\"
   fi

done | convert result.png -draw @- result.png

【讨论】:

  • 这看起来很完美!如何扩展它以包括垂直测量?
  • 非常好的马克。要获得左侧和右侧,您也可以旋转图像并在顶部和底部使用相同的代码。然后转回来。侧面的标签也会旋转。我不知道这是否是一个问题。
【解决方案2】:

如果您运行的是类 Unix 操作系统:Linux/Mac OSX/Windows 10 unix/Windows w/Cygwin,可以使用我的脚本 grid 使用 ImageMagick 完成一些快速而肮脏的事情。这将创建一个间距为 10 的网格。

convert -size 499x149 xc:white miff:- | grid -s 10 -d 9 - grid.png 

convert grid.png \( -size 481x131 xc:white \) -gravity center -compose over -composite ruler.png

【讨论】:

猜你喜欢
  • 2019-05-19
  • 1970-01-01
  • 2022-12-09
  • 1970-01-01
  • 2020-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
相关资源
最近更新 更多