我怎样才能用随机打乱的像素重现图像?

How can i reproduce an image out of randomly shuffled pixels?

my output my input 您好,我正在使用这个 python 代码来生成随机像素图像,有什么方法可以使这个过程相反吗?例如,我将此代码输出的照片提供给程序,它会再次复制原始照片。

我正在尝试生成静态样式图像并将其反转回原始图像,我愿意接受任何其他替换此代码的想法

from PIL import Image
import numpy as np

orig = Image.open('lena.jpg')
orig_px = orig.getdata()

orig_px = np.reshape(orig_px, (orig.height * orig.width, 3))
np.random.shuffle(orig_px)

orig_px = np.reshape(orig_px, (orig.height, orig.width, 3))

res = Image.fromarray(orig_px.astype('uint8'))
res.save('out.jpg')

据我所知,不可能可靠地做到这一点。从理论上讲,您可以通过一遍又一遍地打乱像素并将结果输入 Amazon Rekognition 来暴力破解它,但您最终会得到一笔巨大的 AWS 账单,而且可能只有与原始图片近似的东西。

首先,请记住 JPEG 是有损的 - 因此您将永远无法恢复使用 JPEG 写入的内容 - 它会更改您的数据!因此,如果您想无损地读回刚开始的内容,请使用 PNG。

你可以这样做:

#!/usr/bin/env python3

import numpy as np
from PIL import Image

def shuffleImage(im, seed=42):
    # Get pixels and put in Numpy array for easy shuffling
    pix = np.array(im.getdata())

    # Generate an array of shuffled indices
    # Seed random number generation to ensure same result
    np.random.seed(seed)
    indices = np.random.permutation(len(pix))

    # Shuffle the pixels and recreate image
    shuffled = pix[indices].astype(np.uint8)
 
    return Image.fromarray(shuffled.reshape(im.width,im.height,3))

def unshuffleImage(im, seed=42):

    # Get shuffled pixels in Numpy array
    shuffled = np.array(im.getdata())
    nPix = len(shuffled)

    # Generate unshuffler
    np.random.seed(seed)
    indices = np.random.permutation(nPix)
    unshuffler = np.zeros(nPix, np.uint32)
    unshuffler[indices] = np.arange(nPix)

    unshuffledPix = shuffled[unshuffler].astype(np.uint8)
    return Image.fromarray(unshuffledPix.reshape(im.width,im.height,3))

# Load image and ensure RGB, i.e. not palette image
orig = Image.open('lena.png').convert('RGB')

result = shuffleImage(orig)
result.save('shuffled.png')

unshuffled = unshuffleImage(result)
unshuffled.save('unshuffled.png')

这让莉娜变成了这个: