Reputation: 915
Intention is to create all possible combination of 2x2 binary array filled with 1's and 0's and create relevant image for each array and store it as individual image.
Effort so far:
import itertools
import numpy as np
import matplotlib.pyplot as plt
lst = list(itertools.product([0, 1], repeat=4))
print (lst)
plt.imshow(lst)
plt.show()
Current Output:
[(0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 1, 1), (0, 1, 0, 0), (0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 1, 1), (1, 0, 0, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 1, 1), (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 1, 0), (1, 1, 1, 1)]
Expected output:
00
00
00
01
and so on
More importantly to save those array as individual .png images For 2x2 array it would be 16 images in this example format.
10
01
Help!
Edit1:
In higher matrix saving throws a runtime error it can be solved by adding
plt.close(fig)
within the for statement
RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (matplotlib.pyplot.figure
) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam figure.max_open_warning
).
Upvotes: 0
Views: 160
Reputation: 8800
Printing, graphing, and saving:
for c,i in enumerate(lst):
print(i[0],i[1],'\n',i[2],i[3],'\n',sep='')
array = np.reshape(i,(2,2))
fig, ax = plt.subplots(figsize=(5,5))
ax.imshow(array, cmap='gray')
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
fig.savefig('figure_{}.png'.format(c))
plt.close(fig)
Upvotes: 1
Reputation: 150785
Try:
for i in np.array(lst).reshape(-1,2,2):
plt.imshow(i, cmap='gray')
plt.show()
# other save image here
Upvotes: 1
Reputation: 159
You can reshape each element of the list by doing this
binary_images = [np.reshape(np.asarray(l), (2,2)) for l in lst]
and then save the images by using matplotlib
for i in binary_images:
plt.imshow(i)
plt.savefig('<location>.png')
plt.close()
Upvotes: 1