from PIL import Image import random import math import os, sys import numpy import csv #here the input: name of the image (in bmp format); number of cells; list of coordinates of the centroids csv file Ima=sys.argv[1] cells=int(sys.argv[2]) # coord=sys.argv[3] # try file centroids.txt: may be the number of cells can by assumed from the number of centroids; try midmed.bmp as image # i have coordinates already splitted # #nomex="centroids_cluster"+str(cells)+"x.txt" #nomey="centroids_cluster"+str(cells)+"y.txt" nomex="centroids"+str(cells)+"clustersimuldata.csv"+"x.txt" nomey="centroids"+str(cells)+"clustersimuldata.csv"+"y.txt" #-------------------------------------- #ccsv = open(centroidsdatacsv, 'r') # # separation of first line to x coordinates and second line to y coordinates and transformation of string data in lists (of strings of the numbers) # #nomex=[] #nomey=[] #with open(centroidsdatacsv, 'rb') as fcsv: # reader = csv.reader(fcsv) # for row in reader: # acsv, bcsv= row # nomex.append(acsv) # nomey.append(bcsv) # pesi.append(peso) #---------------------------------------- # cx = open(nomex, 'r') cy = open(nomey, 'r') #separation of first line to x coordinates and second line to y coordinates and transformation of string data in lists (of strings of the numbers) coocentroidx=numpy.loadtxt(cx, delimiter=" ") coocentroidy=numpy.loadtxt(cy, delimiter=" ") def generate_voronoi_diagram(ima, num_cells): im2 = Image.open(ima) imgx, imgy = im2.size image = Image.new("RGB", (imgx, imgy)) putpixel = image.putpixel nx = [] ny = [] nr = [] ng = [] nb = [] for i in range(num_cells): # it appears that the number of tassels? sa num_cells nx=coocentroidx # nx.append(random.randrange(imgx)) # coordinate x # ny.append(random.randrange(imgy)) # coord y ny=coocentroidy nr.append(random.randrange(256)) # nr channel r of rgb ng.append(random.randrange(256)) # ng channel g of rgb nb.append(random.randrange(256)) # nb channel b of rgb # may be here i should insert the coordinates? probably not for y in range(imgy): for x in range(imgx): dmin = math.hypot(imgx-1, imgy-1) j = -1 for i in range(num_cells): d = math.hypot(int(nx[i])-x, int(ny[i])-y) if d < dmin: dmin = d j = i putpixel((x, y), (nr[j], 0, nb[j])) # now insert crosses at centroids coordinates for cross in range (num_cells): putpixel((int(coocentroidx[cross]),int(coocentroidy[cross])),(0, 255, 0)) if int(coocentroidx[cross])<= (imgx-2): # if 3 spaces are free before the end of the line it puts 3 black dots on the right of the coordinate # putpixel((int(coocentroidx[cross])+3,int(coocentroidy[cross])),(0, 255, 0)) putpixel((int(coocentroidx[cross])+2,int(coocentroidy[cross])),(0, 255, 0)) putpixel((int(coocentroidx[cross])+1,int(coocentroidy[cross])),(0, 255, 0)) elif int(coocentroidx[cross])<= (imgy-1): # if 2 spaces are free before the end of the line it puts 2 black dots on the right of the coordinate # putpixel((int(coocentroidx[cross])+2,int(coocentroidy[cross])),(0, 255, 0)) putpixel((int(coocentroidx[cross])+1,int(coocentroidy[cross])),(0, 255, 0)) # elif int(coocentroidx[cross])< (imgy-1): #if 1 space is free before the end of the line it puts 1 black dot on the right of the coordinate # putpixel((int(coocentroidx[cross])+1,int(coocentroidy[cross])),(0, 255, 0)) #from here the left part of the cross if int(coocentroidx[cross])>= 2: # putpixel((int(coocentroidx[cross])-3,int(coocentroidy[cross])),(0, 255, 0)) putpixel((int(coocentroidx[cross])-2,int(coocentroidy[cross])),(0, 255, 0)) putpixel((int(coocentroidx[cross])-1,int(coocentroidy[cross])),(0, 255, 0)) # elif int(coocentroidx[cross])> 1: elif int(coocentroidx[cross])>= 1: # putpixel((int(coocentroidx[cross])-2,int(coocentroidy[cross])),(0, 255, 0)) putpixel((int(coocentroidx[cross])-1,int(coocentroidy[cross])),(0, 255, 0)) # elif int(coocentroidx[cross])!= 0: # putpixel((int(coocentroidx[cross])-1,int(coocentroidy[cross])),(0, 255, 0)) #from here up and down as above if int(coocentroidy[cross])<= (imgy-2): #upper part of the cross # putpixel((int(coocentroidx[cross]),int(coocentroidy[cross])+3),(0, 255, 0)) putpixel((int(coocentroidx[cross]),int(coocentroidy[cross])+2),(0, 255, 0)) putpixel((int(coocentroidx[cross]),int(coocentroidy[cross])+1),(0, 255, 0)) # elif int(coocentroidy[cross])< (imgy-2): # putpixel((int(coocentroidx[cross]),int(coocentroidy[cross])+2),(0, 255, 0)) # putpixel((int(coocentroidx[cross]),int(coocentroidy[cross])+1),(0, 255, 0)) # elif int(coocentroidy[cross])< (imgy-1): elif int(coocentroidy[cross])<= (imgy-1): putpixel((int(coocentroidx[cross]),int(coocentroidy[cross])+1),(0, 255, 0)) #down part of the cross # if int(coocentroidy[cross])> 2: # putpixel((int(coocentroidx[cross]),int(coocentroidy[cross])-3),(0, 255, 0)) # putpixel((int(coocentroidx[cross]),int(coocentroidy[cross])-2),(0, 255, 0)) # putpixel((int(coocentroidx[cross]),int(coocentroidy[cross])-1),(0, 255, 0)) if int(coocentroidy[cross])>= 2: putpixel((int(coocentroidx[cross]),int(coocentroidy[cross])-2),(0, 255, 0)) putpixel((int(coocentroidx[cross]),int(coocentroidy[cross])-1),(0, 255, 0)) elif int(coocentroidy[cross])>= 1: # if int(coocentroidy[cross])!= 0: putpixel((int(coocentroidx[cross]),int(coocentroidy[cross])-1),(0, 255, 0)) # im4.save(i4) # im1=Image.open(i2) # im2=Image.open(i4) im3=Image.blend(image, im2, 0.6 ) nameimage="vorondiagblend_"+str(cells)+"_clust.bmp" im3.save(nameimage, "bmp") nameimage="vorondiag_"+str(cells)+"_clust.bmp" image.save(nameimage, "bmp") image.show() generate_voronoi_diagram(Ima, int(cells))