我想在通过命令行使用ImageMagick创建的图像中绘制许多形状,从一开始就是空的。我希望在内存中创建空图像,在其中绘制形状,并在完成所有操作后保存到文件中。我不知道如何管理命令,以便在保存文件之前执行它们。
基本上,这是我希望实现的流程:
编辑:我正在尝试使用bash脚本生成随机方块图案的图像。为此,我生成了一个大小为1280x720px的白色图像,并在其上粘贴了黑色方块。这些方块具有随机大小,位置和旋转。在这里,我附上了我的图像https://drive.google.com/open?id=0BypRKoRnoTbbWkMyLXJDYl9xT3c,我成功了。我需要帮助才能使这些方块彼此不重叠。我想只将它们粘贴在背景图像的空白处。 我正在开发基于Linux的系统。
这是我的bash脚本:
#!/bin/bash
# PX position of squares on X-axis
# PY position of squares on Y-axis
# ROT rotation angle of squres
# generating background image of size 1280x720
convert -size 1260x720 canvas:white bg.jpg
# generating 100 squares of random size in between 20x20 px
for (( count=1; count <= 1000; count++ ))
do
X=$((RANDOM%10+10))
convert -size "$X"x"$X" canvas:black square.jpg
# copying squares on background image
PX=$((RANDOM%1260+1))
PY=$((RANDOM%720+1))
ROT=$((RANDOM%90+0))
convert \( bg.jpg \) \( square.jpg -background none -rotate "$ROT" \) -geometry +"$PX"+"$PY" -composite bg.jpg
done
答案 0 :(得分:1)
我有一个试错法类型的解决方案,效果很好:
#!/bin/bash
# PX position of squares on X-axis
# PY position of squares on Y-axis
# ROT rotation angle of squres
# generating background image of size 1260x720
convert -size 1260x720 canvas:white bg.mpc
# Get quantum range (likely 255, or 65535)
QR=$(convert xc: -format "%[fx:quantumrange]" info:)
# generating 100 squares of random size in between 20x20 px
for (( count=1; count <= 1000; count++ ))
do
X=$((RANDOM%10+10))
ROT=$((RANDOM%90+0))
# Create square, and rotate. Work out space needed to draw it
w=$(convert -size "$X"x"$X" canvas:black -background none -rotate $ROT +repage -format "%w" -write info:- square.mpc)
echo "DEBUG: Square $count is ${X}x${X} rotated $ROT, making ${w}x${w}"
# Keep choosing random places in image till we find big enough hole
n=1
while :; do
PX=$((RANDOM%(1260-w)))
PY=$((RANDOM%(720-w)))
echo "DEBUG: Attempt $n - trying at $PX,$PY"
# Crop out the corresponding square and check it is all white, if so, ok
min=$(convert bg.mpc -crop ${w}x${w}+${PX}+${PY} -colorspace gray -format "%[min]" info:)
[ $min -eq $QR ] && break
((n=n+1))
done
convert bg.mpc square.mpc -geometry +"$PX"+"$PY" -composite bg.mpc
done
convert bg.mpc bg.png
这是第二轮:
我已经在bash
中完成了,因为这是你尝试的方式 - 然而,它在C ++ / PHP中要快得多,因为它可以在内存中完成,而不是必须继续将方块写入磁盘合成
示例输出
DEBUG: Square 1 is 15x15 rotated 15, making 21x21
DEBUG: Attempt 1 - trying at 900,360
DEBUG: Square 2 is 13x13 rotated 36, making 21x21
DEBUG: Attempt 1 - trying at 87,499
DEBUG: Square 3 is 15x15 rotated 22, making 21x21
DEBUG: Attempt 1 - trying at 324,194
DEBUG: Square 4 is 14x14 rotated 48, making 22x22
DEBUG: Attempt 1 - trying at 469,203
DEBUG: Square 5 is 16x16 rotated 53, making 24x24
DEBUG: Attempt 1 - trying at 1104,328
DEBUG: Square 6 is 18x18 rotated 27, making 26x26
DEBUG: Attempt 1 - trying at 541,385
DEBUG: Square 7 is 15x15 rotated 0, making 15x15
DEBUG: Attempt 1 - trying at 377,432
DEBUG: Square 8 is 19x19 rotated 69, making 27x27
DEBUG: Attempt 1 - trying at 108,647
DEBUG: Square 9 is 14x14 rotated 64, making 20x20
DEBUG: Attempt 1 - trying at 95,238
DEBUG: Square 10 is 12x12 rotated 45, making 18x18
DEBUG: Attempt 1 - trying at 764,588
DEBUG: Square 11 is 18x18 rotated 3, making 20x20
DEBUG: Attempt 1 - trying at 1048,101
DEBUG: Square 12 is 17x17 rotated 77, making 23x23
DEBUG: Attempt 1 - trying at 1086,316
DEBUG: Attempt 2 - trying at 915,554
DEBUG: Square 13 is 19x19 rotated 5, making 23x23
...
对方块的大小进行排序并将最大的大小排在第一位可能会更快,因此由于丢失而重新计算的次数会减少,因为它会越来越难以放置方块。
也可能有更聪明的算法,但我可能很懒; - )
我已经制作了一个关于它如何工作的动画。一个正方形的好位置显示绿色,位置冲突显示红色。