在Python

时间:2017-11-15 18:16:08

标签: image python-3.x

我正在尝试编写一个脚本,该脚本生成随机像素的二进制图像,该图像优先将相似值的像素组合在一起(即标题中的“聚集”),类似于第一个附加图像。我的代码名义上接近这个,因为我有多组条件,当另一个白色像素位于其直接邻域(黑色相同)时,白色像素的概率更高,但它输出的图像类似于第二个附加图像。我可以利用任何库或方法来生成第一个图像吗?

import numpy as np
import random
import timeit
import multiprocessing as mp
from multiprocessing import Process, Pipe, JoinableQueue
import math
from PIL import Image

arr_size = 200
arr = np.zeros((200,200))

X = 200
Y = 200

num_sections = 10

def phase_neighborhoods(section_number):
    #section size definitions go here

    neighborhood = lambda m, n: [(m2,n2) for m2 in range(m-1,m+2) for n2 in range(n-1,n+2) if (-1 < m <= X and -1 < n <= section_size and (m != m2 or n != n2) and (0 <= m2 <= X) and (0 <= n2 <= section_size))]
    #this function defines the immediate neighborhood for each pixel

        for y in range(section_size):
            for x in range(arr_size):

                rnd_e1 = random.random()
                rnd_e2 = random.random()
                counter_e1 = np.zeros(num_sections)
                counter_e2 = np.zeros(num_sections)

                if rnd_e1 <= 0.11892865364043818:
                    arr_section[y,x] = 255

                for i in range(len(neighborhood(y,x))):
                    if arr_section[neighborhood(y,x)[i][0]-1, neighborhood(y,x)[i][1]-1] == 255:
                        counter_e1[section_number] += 0.15

                if rnd_e1 <= 0.11892865364043818+(counter_e1[section_number]*0.2):
                    arr_section[y,x] = 255

                for i in range(len(neighborhood(y,x))):
                    if arr_section[neighborhood(y,x)[i][0]-1, neighborhood(y,x)[i][1]-1] == 0:
                        counter_e2[section_number] += 0.25
                if rnd_e1 >= 1-0.11892865364043818-(counter_e2[section_number]*0.02):
                        arr_section[y,x] = 0

#if __name__ == '__main__' etc etc

我删除了代码的多余部分,因为大部分是并行化代码(因此section_sizesection_number变量)

This kind of clumping is the goalThis is the code's result

0 个答案:

没有答案