-
Notifications
You must be signed in to change notification settings - Fork 0
/
aquatintScript.py
142 lines (117 loc) · 4.07 KB
/
aquatintScript.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
# coding: utf-8
import sys
filename = sys.argv[1]
greycut = float(sys.argv[2])
temperature = float(sys.argv[3])
totalsweeps = int(sys.argv[4])
import numpy as np
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import scipy.misc
import json
import imageio
def write_to_json(filename,string):
f = open(filename, 'w')
f.write(string)
f.close()
return True
status_dict = {"origin":False,"greycut":False,"temperature":False,"sweeps":dict(),"finished":0,"total":3+totalsweeps,"rewrite":0}
for i in range(0,totalsweeps):
status_dict['sweeps']["sweep"+str(i)] = False
write_to_json(filename.split('.')[-2]+'-status.json',json.dumps(status_dict))
#print(json.dumps(status_dict))
im2 = imageio.v2.imread(filename)
Nix=im2.shape[0]
Niy=im2.shape[1]
grayimage=np.zeros([Nix,Niy])
rewrite_switch = True
for i in range(0,Nix):
for j in range(0,Niy):
blueComponent = im2[i][j][0]
greenComponent = im2[i][j][1]
redComponent = im2[i][j][2]
grayValue = 0.07 * blueComponent + 0.72 * greenComponent + 0.21 * redComponent
grayimage[i][j] = grayValue
pass
status_dict['rewrite'] = i / Nix
if round((i * 100) / Nix) % 3 == 0:
if rewrite_switch == True:
write_to_json(filename.split('.')[-2]+'-status.json',json.dumps(status_dict))
rewrite_switch = False
else:
rewrite_switch = True
status_dict["rewrite"] = 0
dsqin=1-grayimage/255.0
hsimage=plt.imshow(dsqin,cmap='Greys',aspect=1,interpolation='none')
#cb = plt.colorbar(hsimage)
plt.savefig(filename.split('.')[-2]+'-origin.jpg',dpi=300)
status_dict["origin"] = True
status_dict['finished'] += 1
write_to_json(filename.split('.')[-2]+'-status.json',json.dumps(status_dict))
#################################
nan=np.ndarray.flatten(dsqin)
nsites=Nix*Niy
hhbw=np.zeros(nsites)
for jj in range(nsites):
if nan[jj]<greycut: hhbw[jj]=-1
else: hhbw[jj]=1
pass
dsq=np.reshape(hhbw,(Nix,Niy))
hsimage=plt.imshow(dsq,cmap='Greys',aspect=1,interpolation='none')
plt.savefig(filename.split('.')[-2]+'-greycut.jpg',dpi=300)
sth=1
status_dict['greycut'] = True
status_dict['finished'] += 1
write_to_json(filename.split('.')[-2]+'-status.json',json.dumps(status_dict))
#print(json.dumps(status_dict))
########################
Nx=Niy
Ny=Nix
nsites=Nx*Ny
beta=1/temperature
status_dict['temperature'] = True
status_dict['finished'] += 1
write_to_json(filename.split('.')[-2]+'-status.json',json.dumps(status_dict))
#print(json.dumps(status_dict))
v=np.zeros(nsites)
sig=2*v-1
sumen=0
for nsweeps in range(totalsweeps):
rewrite_switch = True
status_dict['rewrite'] = 0
for npick in range(nsites):
xx=np.random.randint(Nx)
yy=np.random.randint(Ny)
sumsig=sig[((xx+1)%Nx)+Nx*yy]+sig[((xx-1)%Nx)+Nx*yy]+sig[xx+Nx*((yy+1)%Ny)]+sig[xx+Nx*((yy-1)%Ny)]
local=sig[xx+Nx*yy]*(beta*sumsig+sth*hhbw[xx+Nx*yy])
if local<=0:
sig[xx+Nx*yy]*=(-1)
else:
pp=np.random.uniform(0,1)
probflip=np.exp(-2*local)
if pp<=probflip:
sig[xx+Nx*yy]*=(-1)
status_dict['rewrite'] = npick / nsites
if round((npick * 100) / nsites) % 3 == 0:
if rewrite_switch == True:
write_to_json(filename.split('.')[-2]+'-status.json',json.dumps(status_dict))
rewrite_switch = False
else:
rewrite_switch = True
pass
v=((sig+1)/2)
dsq=np.reshape(v,(Ny,Nx))
hsimage=plt.imshow(dsq,cmap='Greys',aspect=1,interpolation='none')
plt.savefig(filename.split('.')[-2]+'-sweep'+str(nsweeps)+'.jpg',dpi=300)
status_dict['sweeps']['sweep'+str(nsweeps)] = True
status_dict['finished'] += 1
write_to_json(filename.split('.')[-2]+'-status.json',json.dumps(status_dict))
#print(json.dumps(status_dict))
pass
hsimage=plt.imshow(dsq,cmap='Greys',aspect=1,interpolation='none')
#cb.remove()
plt.axis('off')
plt.savefig(filename.split('.')[-2]+'-aquatint.jpg',dpi=300)
plt.close()