我想置乱,也就是说,随机置换由布尔掩码界定的区域的像素,在本例中,仅置换面部的区域(省略背景)。
在整个图像上进行随机排列的代码是有效的,但是当我将其应用于掩码数组时,它也会改变颜色...如何仅在非颜色轴旁边执行洗牌?
from PIL import Image
import requests
from io import BytesIO
import numpy as np
response = requests.get("https://www.4dface.io/wp-content/uploads/2018/10/4DFM_sample2.jpg")
img = Image.open(BytesIO(response.content))
img.show()
def _shuffle_2D(x):
return _shuffled(_shuffled(x.swapaxes(0, 1)).swapaxes(0, 1))
def _shuffled(x):
"""Return a shuffled array. Because python does it in-place."""
np.random.shuffle(x)
return x
array = np.array(img)
PIL.Image.fromarray(_shuffle_2D(array))
下面,同样的方法也适用于掩码数组:如你所见,它也改变了颜色
# Get mask
mask = np.array(img.convert("L")) != 255
array[mask] = _shuffle_2D(array[mask])
PIL.Image.fromarray(array)
欢迎任何指针!
1条答案
按热度按时间bmvo0sr51#
我还没有研究过是否有任何洗牌的方法,允许轴被设置或从洗牌排除,但一个快速的方法可能是将每个RGB888像素转换成一个单一的
uint32
之前洗牌,然后分裂回RGB888之后。因为3字节将被打包在一起,成为一个单一的实体,他们不会得到分离,从他们的小朋友。使用
np.dot()
可以非常快速地完成此操作,如下所示:所以我的具体建议是: