如何重新排序像素

时间:2013-01-29 22:45:21

标签: python image-processing

我在Google上搜索过但我找不到任何内容。

我想创建一个Python脚本,可以导入图像,更改像素的顺序,并保存输出图像。

我经常使用Python,但只使用内置库。因此,如果我必须使用新命令,请尽可能多地描述它。

3 个答案:

答案 0 :(得分:13)

import sys
import random
from PIL import Image

BLOCKLEN = 64 # Adjust and be careful here.

img = Image.open(sys.argv[1])
width, height = img.size

xblock = width / BLOCKLEN
yblock = height / BLOCKLEN
blockmap = [(xb*BLOCKLEN, yb*BLOCKLEN, (xb+1)*BLOCKLEN, (yb+1)*BLOCKLEN)
        for xb in xrange(xblock) for yb in xrange(yblock)]

shuffle = list(blockmap)
random.shuffle(shuffle)

result = Image.new(img.mode, (width, height))
for box, sbox in zip(blockmap, shuffle):
    c = img.crop(sbox)
    result.paste(c, box)
result.save(sys.argv[2])

示例输入,BLOCKLEN = 1BLOCKLEN = 64BLOCKLEN = 128

enter image description here enter image description here enter image description here enter image description here

答案 1 :(得分:0)

您应该查找PIL库link

答案 2 :(得分:0)

我建议使用ipython notebookmatplotlib image。这样,您可以在编码时看到您处理的图像。