如何将图像切割成碎片,将它们随机化并放在一起

ber*_*sel -2 bash image

我想将图像 (orig.jpg) 切成 16px x 16px 的块,随机排列它们的顺序并将它们放在一起大小相同的新图像 (jpg) 中。基本上是马赛克效果,但没有顺序。

分裂不是问题

convert -crop 16x16@ orig.jpg  tile_%d.jpg
Run Code Online (Sandbox Code Playgroud)

但我不知道如何将它们随机组合在一起......

montage 
Run Code Online (Sandbox Code Playgroud)

我猜应该可以解决问题。我以前做过,但找不到脚本:-S

全部使用:我需要具有完全相同颜色和亮度的图像,但应该无法识别原始图像。

Mar*_*ell 5

你可以用bashand这样做ImageMagick

#!/bin/bash
convert -crop 16x16@ input.jpg tile.jpg
montage -geometry +0+0 $(ls tile*jpg | awk 'BEGIN{srand()}{print rand() "\t" $0}' | sort -n | cut -f2-) output.png

# Remember to remove the tile*jpg before you do another one :-)
# rm tile*jpg
Run Code Online (Sandbox Code Playgroud)

基本上按照您的建议,使用-cropmontage。里面的位$()进程替换,它将运行进程的结果放在括号内并将其放入montage命令中。它列出了所有被调用的文件tile*jpg和管道,awk在每个文件的前面附加一个随机数,然后按随机数排序并将其砍掉。

所以它是这样的:

在此处输入图片说明

进入这个:

在此处输入图片说明

我一直在对此进行进一步的试验(即四处玩耍),我看到您可以在瓷砖之间看到白线和间隙。我不确定这些是否会打扰您,但如果它们打扰您,一个可能的解决方案是记录原始图像几何形状,然后将其调整为 16x16 平铺尺寸的精确倍数。然后像以前一样继续,最后将大小调整为奇​​数 0-15 个像素,回到原始大小。

如果有必要,我想出了这个:

#!/bin/bash
# Get original image geometry
origgeom=$(identify -format %g input.jpg)
echo $origgeom
# Calculate new geometry as exact multiple of tilesize
newgeom=$(convert input.jpg -format "%[fx:int(w/16)*16]x%[fx:int(h/16)*16]" info:)
echo $newgeom

# Resize to new geometry and tile
convert input.jpg -resize $newgeom -crop 16x16@ tile.jpg

# Rebuild in random order then correct geometry
montage -background none -geometry +0+0 $(ls tile*jpg | awk 'BEGIN{srand()}{print rand() "\t" $0}' | sort -n | cut -f2-) JPG:- | convert JPG: -resize ${origgeom}! output.jpg
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明