问题描述
我想将图像(orig.jpg)剪切成16px x 16px的片段,随机化它们的顺序并将它们放在一起形成相同大小的新图像(jpg)。基本上是马赛克效果,但没有订单。
I want to cut an image (orig.jpg) into 16px x 16px pieces, randomize their order and put them to a new image (jpg) of same size together. Basically a mosaic effect, but with no order.
拆分不是问题
convert -crop 16x16@ orig.jpg tile_%d.jpg
但我有不知道如何将它们随机放在一起......
but I have no clue how to randomly put them together...
montage
我应该这样做。我之前做过,但找不到脚本:-S
should do the trick I guess. I did it before, but cannot find the script :-S
使用全部:我需要具有完全相同颜色和亮度的图像,但它不应该是可以识别原始图像。
The use of all: I need images with exact the same colors and brightness, but it shouldn't be possible to recognize the original image.
推荐答案
你可以用 bash $ c这样做$ c>和
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
基本上如你所知,使用 -crop
和蒙太奇
。 $()
中的位是进程替换,它会在括号内运行该进程并将其放入蒙太奇
命令。它列出了所有名为 tile * jpg
的文件,并将其输入 awk
,以便在每个文件的前面附加一个随机数,然后按随机数排序并将其剔除。
Basically as you suggest, using -crop
and montage
. The bit inside $()
is process substitution and it takes the result of running the process inside the parentheses and puts it into the montage
command. It lists all files called tile*jpg
and pipes that into awk
to append a random number to the front of each file, then it sorts by the random number and chops it off.
所以这样做:
进入:
我一直在进一步尝试这个(即玩耍),我发现你可以在瓷砖之间获得白线和间隙。我不确定这些是否会让您感到困扰,但如果他们这样做,可能的解决方案是记下原始图像几何体,然后将其调整为16x16瓷砖大小的精确倍数。然后像以前一样继续,并按结束时的奇数0-15像素调整大小,恢复到原始大小。
I have been experimenting further with this (i.e. playing around) and I see you can get white lines and gaps between the tiles. I am not sure if these bother you, but if they do, a possible solution is to note the original image geometry and then resize it to an exact multiple of your 16x16 tile-size. Then proceed as before, and resize by the odd 0-15 pixels at the end, back to the original size.
如果有必要,我想出了这个:
If that is necessary, I came up with this:
#!/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
这篇关于如何将图像切割成碎片,随机化并将它们放在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!