Guest User

Untitled

a guest
Sep 29th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.65 KB | None | 0 0
  1. from matplotlib import pyplot as plt
  2. from matplotlib import image as img
  3. import numpy as np
  4. import glob, os
  5.  
  6. shapes = ['D', 'T', 'C', 'S']
  7. threshs = [20, 125, 180, 235]
  8. bandwidth = 18
  9.  
  10. def determine_symbol(inCnt):
  11. x = max([thresh for thresh in threshs if inCnt > thresh])
  12. return shapes[threshs.index(x)]
  13.  
  14. def strornone(x):
  15. if x:
  16. return ''.join(x)
  17. else:
  18. return None
  19.  
  20. # pass in the image filename when you declare the class instance
  21. class puzzleImage():
  22. def __init__(self, imgFile, verbose=False):
  23. self.image = img.imread(imgFile).astype('int')
  24. self.parseSymbols(verbose)
  25. self.filename = imgFile
  26. self.setcoord(0,0)
  27.  
  28. def setcoord(self,x,y):
  29. self.x=x
  30. self.y=y
  31.  
  32. def parseSymbols(self,verbose=False):
  33. #start on left side column, track which row you see non-white pixels on
  34. #and which row you go back to white pixels on, this will allow us to box each symbol.
  35. myimage = self.image
  36.  
  37. boxtops = []
  38. boxbots = []
  39.  
  40. switch = 0
  41. for row in range(myimage.shape[0]):
  42. whitecnt = np.count_nonzero( np.amax(myimage[row,:bandwidth], axis=1) > 0xF0)
  43. #print(whitecnt)
  44. if (switch == 0) and (whitecnt < bandwidth - 4):
  45. boxtops.append(row)
  46. switch = 1
  47. if (switch == 1) and (whitecnt > bandwidth - 2):
  48. boxbots.append(row-1)
  49. switch = 0
  50. if abs(boxbots[-1] - boxtops[-1]) < 5:
  51. boxbots = boxbots[:-1]
  52. boxtops = boxtops[:-1]
  53. if len(boxbots) == 5:
  54. break
  55.  
  56. if len(boxbots) < 5:
  57. L_symbols = None
  58. else:
  59. black_pixel_cnts = [np.count_nonzero( np.amax(myimage[boxtops[j]:boxbots[j],:bandwidth], axis=2) < 0xF0) for j in range(5)]
  60. L_symbols = [determine_symbol(x) for x in black_pixel_cnts]
  61. if verbose: print(black_pixel_cnts)
  62. #Now right side
  63.  
  64. boxtops = []
  65. boxbots = []
  66.  
  67. switch = 0
  68. for row in range(myimage.shape[0]):
  69. whitecnt = np.count_nonzero( np.amax(myimage[row,-bandwidth:], axis=1) > 0xF0)
  70.  
  71. if (switch == 0) and (whitecnt < bandwidth - 4):
  72. boxtops.append(row)
  73. switch = 1
  74. if (switch == 1) and (whitecnt > bandwidth - 2):
  75. boxbots.append(row-1)
  76. switch = 0
  77. if abs(boxbots[-1] - boxtops[-1]) < 5:
  78. boxbots = boxbots[:-1]
  79. boxtops = boxtops[:-1]
  80. if len(boxbots) == 5:
  81. break
  82.  
  83. if len(boxbots) < 5:
  84. R_symbols = None
  85. else:
  86. black_pixel_cnts = [np.count_nonzero( np.amax(myimage[boxtops[j]:boxbots[j],-bandwidth:], axis=2) < 0xF0) for j in range(5)]
  87. R_symbols = [determine_symbol(x) for x in black_pixel_cnts]
  88. if verbose: print(black_pixel_cnts)
  89.  
  90. #Now top
  91.  
  92. boxtops = []
  93. boxbots = []
  94.  
  95. switch = 0
  96. for col in range(myimage.shape[1]):
  97. whitecnt = np.count_nonzero( np.amax(myimage[:bandwidth,col], axis=1) > 0xF0)
  98.  
  99. if (switch == 0) and (whitecnt < bandwidth - 4):
  100. boxtops.append(col)
  101. switch = 1
  102. if (switch == 1) and (whitecnt > bandwidth - 2):
  103. boxbots.append(col-1)
  104. switch = 0
  105. if abs(boxbots[-1] - boxtops[-1]) < 5:
  106. boxbots = boxbots[:-1]
  107. boxtops = boxtops[:-1]
  108. if len(boxbots) == 5:
  109. break
  110.  
  111. if len(boxbots) < 5:
  112. T_symbols = None
  113. else:
  114. black_pixel_cnts = [np.count_nonzero( np.amax(myimage[:bandwidth, boxtops[j]:boxbots[j]], axis=2) < 0xF0) for j in range(5)]
  115. T_symbols = [determine_symbol(x) for x in black_pixel_cnts]
  116. if verbose: print(black_pixel_cnts)
  117.  
  118. #Now bot
  119.  
  120. boxtops = []
  121. boxbots = []
  122.  
  123. switch = 0
  124. for col in range(myimage.shape[1]):
  125. whitecnt = np.count_nonzero( np.amax(myimage[-bandwidth:,col], axis=1) > 0xF0)
  126.  
  127. if (switch == 0) and (whitecnt < bandwidth - 4):
  128. boxtops.append(col)
  129. switch = 1
  130. if (switch == 1) and (whitecnt > bandwidth - 2):
  131. boxbots.append(col-1)
  132. switch = 0
  133. if abs(boxbots[-1] - boxtops[-1]) < 5:
  134. boxbots = boxbots[:-1]
  135. boxtops = boxtops[:-1]
  136. if len(boxbots) == 5:
  137. break
  138.  
  139. if len(boxbots) < 5:
  140. B_symbols = None
  141. else:
  142. black_pixel_cnts = [np.count_nonzero( np.amax(myimage[-bandwidth:, boxtops[j]:boxbots[j]], axis=2) < 0xF0) for j in range(5)]
  143. B_symbols = [determine_symbol(x) for x in black_pixel_cnts]
  144. if verbose: print(black_pixel_cnts)
  145.  
  146.  
  147. self.Lsym = strornone(L_symbols)
  148. self.Rsym = strornone(R_symbols)
  149. self.Bsym = strornone(B_symbols)
  150. self.Tsym = strornone(T_symbols)
  151.  
  152. #print(self.Lsym, self.Rsym, self.Bsym, self.Tsym)
  153.  
  154. # actually doesn't print the chunk out any more
  155. # instead pastes together a set of images using their coords.
  156. def printchunk(expset):
  157. minx = min([elt.x for elt in expset])
  158. maxx = max([elt.x for elt in expset])
  159. miny = min([elt.y for elt in expset])
  160. maxy = max([elt.y for elt in expset])
  161.  
  162. for elt in expset:
  163. elt.setcoord(elt.x - minx, elt.y - miny)
  164.  
  165. maxx -= minx
  166. maxy -= miny
  167.  
  168. big = np.ones((130*(maxx+1),130*(maxy+1),3))*255
  169. print(big.shape, elt.image.shape)
  170. for elt in expset:
  171. big[130*elt.x : 130*elt.x + 130, 130*elt.y:130*elt.y + 130] = elt.image[25:-25, 25:-25]
  172.  
  173. return big
  174.  
  175. if __name__ == '__main__':
  176. imagefiles = glob.glob('*JPEG')
  177. print('Found {0} image files'.format(len(imagefiles)))
  178. parsedimages = [puzzleImage(x) for x in imagefiles]
  179.  
  180. expandingsets = [[x] for x in parsedimages]
  181.  
  182. prev = len(expandingsets)
  183. new = 0
  184.  
  185. while new < prev:
  186. prev = len(expandingsets)
  187. for i in range(len(expandingsets)):
  188. remlist = []
  189. expset1 = expandingsets[i]
  190. for j in range(i+1, len(expandingsets)):
  191. expset2 = expandingsets[j]
  192.  
  193. #TSYM
  194. if set([x.Tsym for x in expset1 if x.Tsym]) & set([x.Rsym for x in expset2 if x.Rsym]):
  195. remlist.append(j)
  196. cstr = list(set([x.Tsym for x in expset1 if x.Tsym]) & set([x.Rsym for x in expset2 if x.Rsym]))[0]
  197. chosen1 = [q for q in expset1 if q.Tsym == cstr][0]
  198. chosen2 = [q for q in expset2 if q.Rsym == cstr][0]
  199. for q in expset2:
  200. q.setcoord(q.x + chosen1.x - 1 - chosen2.x, q.y + chosen1.y - 1 - chosen2.y)
  201. expset1 += expset2
  202. elif set([x.Tsym for x in expset1 if x.Tsym]) & set([x.Bsym[::-1] for x in expset2 if x.Bsym]):
  203. remlist.append(j)
  204. cstr = list(set([x.Tsym for x in expset1 if x.Tsym]) & set([x.Bsym[::-1] for x in expset2 if x.Bsym]))[0]
  205. chosen1 = [q for q in expset1 if q.Tsym == cstr][0]
  206. chosen2 = [q for q in expset2 if q.Bsym[::-1] == cstr][0]
  207. for q in expset2:
  208. q.setcoord(q.x + chosen1.x - 2 - chosen2.x, q.y + chosen1.y - chosen2.y)
  209. expset1 += expset2
  210. elif set([x.Tsym for x in expset1 if x.Tsym]) & set([x.Lsym[::-1] for x in expset2 if x.Lsym]):
  211. remlist.append(j)
  212. cstr = list(set([x.Tsym for x in expset1 if x.Tsym]) & set([x.Lsym[::-1] for x in expset2 if x.Lsym]))[0]
  213. chosen1 = [q for q in expset1 if q.Tsym == cstr][0]
  214. chosen2 = [q for q in expset2 if q.Lsym[::-1] == cstr][0]
  215. for q in expset2:
  216. q.setcoord(q.x + chosen1.x - 1 - chosen2.x, q.y + chosen1.y + 1 - chosen2.y)
  217. expset1 += expset2
  218.  
  219. #RSYM
  220. elif set([x.Rsym for x in expset1 if x.Rsym]) & set([x.Tsym for x in expset2 if x.Tsym]):
  221. remlist.append(j)
  222. cstr = list(set([x.Rsym for x in expset1 if x.Rsym]) & set([x.Tsym for x in expset2 if x.Tsym]))[0]
  223. chosen1 = [q for q in expset1 if q.Rsym == cstr][0]
  224. chosen2 = [q for q in expset2 if q.Tsym == cstr][0]
  225. for q in expset2:
  226. q.setcoord(q.x + chosen1.x + 1 - chosen2.x, q.y + chosen1.y + 1 - chosen2.y)
  227. expset1 += expset2
  228. elif set([x.Rsym for x in expset1 if x.Rsym]) & set([x.Lsym[::-1] for x in expset2 if x.Lsym]):
  229. remlist.append(j)
  230. cstr = list(set([x.Rsym for x in expset1 if x.Rsym]) & set([x.Lsym[::-1] for x in expset2 if x.Lsym]))[0]
  231. chosen1 = [q for q in expset1 if q.Rsym == cstr][0]
  232. chosen2 = [q for q in expset2 if q.Lsym[::-1] == cstr][0]
  233. for q in expset2:
  234. q.setcoord(q.x + chosen1.x - chosen2.x, q.y + chosen1.y + 2 - chosen2.y)
  235. expset1 += expset2
  236. elif set([x.Rsym for x in expset1 if x.Rsym]) & set([x.Bsym[::-1] for x in expset2 if x.Bsym]):
  237. remlist.append(j)
  238. cstr = list(set([x.Rsym for x in expset1 if x.Rsym]) & set([x.Bsym[::-1] for x in expset2 if x.Bsym]))[0]
  239. chosen1 = [q for q in expset1 if q.Rsym == cstr][0]
  240. chosen2 = [q for q in expset2 if q.Bsym[::-1] == cstr][0]
  241. for q in expset2:
  242. q.setcoord(q.x + chosen1.x - 1 - chosen2.x, q.y + chosen1.y + 1 - chosen2.y)
  243. expset1 += expset2
  244.  
  245.  
  246. #LSYM
  247. elif set([x.Lsym for x in expset1 if x.Lsym]) & set([x.Tsym[::-1] for x in expset2 if x.Tsym]):
  248. remlist.append(j)
  249. cstr = list(set([x.Lsym for x in expset1 if x.Lsym]) & set([x.Tsym[::-1] for x in expset2 if x.Tsym]))[0]
  250. chosen1 = [q for q in expset1 if q.Lsym == cstr][0]
  251. chosen2 = [q for q in expset2 if q.Tsym[::-1] == cstr][0]
  252. for q in expset2:
  253. q.setcoord(q.x + chosen1.x + 1 - chosen2.x, q.y + chosen1.y - 1 - chosen2.y)
  254. expset1 += expset2
  255. elif set([x.Lsym for x in expset1 if x.Lsym]) & set([x.Rsym[::-1] for x in expset2 if x.Rsym]):
  256. remlist.append(j)
  257. cstr = list(set([x.Lsym for x in expset1 if x.Lsym]) & set([x.Rsym[::-1] for x in expset2 if x.Rsym]))[0]
  258. chosen1 = [q for q in expset1 if q.Lsym == cstr][0]
  259. chosen2 = [q for q in expset2 if q.Rsym[::-1] == cstr][0]
  260. for q in expset2:
  261. q.setcoord(q.x + chosen1.x - chosen2.x, q.y + chosen1.y - 2 - chosen2.y)
  262. expset1 += expset2
  263. elif set([x.Lsym for x in expset1 if x.Lsym]) & set([x.Bsym for x in expset2 if x.Bsym]):
  264. remlist.append(j)
  265. cstr = list(set([x.Lsym for x in expset1 if x.Lsym]) & set([x.Bsym for x in expset2 if x.Bsym]))[0]
  266. chosen1 = [q for q in expset1 if q.Lsym == cstr][0]
  267. chosen2 = [q for q in expset2 if q.Bsym == cstr][0]
  268. for q in expset2:
  269. q.setcoord(q.x + chosen1.x - 1 - chosen2.x, q.y + chosen1.y - 1 - chosen2.y)
  270. expset1 += expset2
  271.  
  272.  
  273. #BSYM
  274. elif set([x.Bsym for x in expset1 if x.Bsym]) & set([x.Rsym[::-1] for x in expset2 if x.Rsym]):
  275. remlist.append(j)
  276. cstr = list(set([x.Bsym for x in expset1 if x.Bsym]) & set([x.Rsym[::-1] for x in expset2 if x.Rsym]))[0]
  277. chosen1 = [q for q in expset1 if q.Bsym == cstr][0]
  278. chosen2 = [q for q in expset2 if q.Rsym[::-1]== cstr][0]
  279. for q in expset2:
  280. q.setcoord(q.x + chosen1.x + 1 - chosen2.x, q.y + chosen1.y + 1 - chosen2.y)
  281. expset1 += expset2
  282. elif set([x.Bsym for x in expset1 if x.Bsym]) & set([x.Tsym[::-1] for x in expset2 if x.Tsym]):
  283. remlist.append(j)
  284. cstr = list(set([x.Bsym for x in expset1 if x.Bsym]) & set([x.Tsym[::-1] for x in expset2 if x.Tsym]))[0]
  285. chosen1 = [q for q in expset1 if q.Bsym == cstr][0]
  286. chosen2 = [q for q in expset2 if q.Tsym[::-1]== cstr][0]
  287. for q in expset2:
  288. q.setcoord(q.x + chosen1.x + 2 - chosen2.x, q.y + chosen1.y - chosen2.y)
  289. expset1 += expset2
  290. elif set([x.Bsym for x in expset1 if x.Bsym]) & set([x.Lsym for x in expset2 if x.Lsym]):
  291. remlist.append(j)
  292. cstr = list(set([x.Bsym for x in expset1 if x.Bsym]) & set([x.Lsym for x in expset2 if x.Lsym]))[0]
  293. chosen1 = [q for q in expset1 if q.Bsym == cstr][0]
  294. chosen2 = [q for q in expset2 if q.Lsym == cstr][0]
  295. for q in expset2:
  296. q.setcoord(q.x + chosen1.x + 1 - chosen2.x, q.y + chosen1.y + 1 - chosen2.y)
  297. expset1 += expset2
  298.  
  299. remlist.reverse()
  300. for j in remlist:
  301. expandingsets.pop(j)
  302. if remlist:
  303. break
  304.  
  305. new = len(expandingsets)
  306. print(new)
  307.  
  308. bigs = []
  309. for elt in expandingsets:
  310. if len(elt) > 1:
  311. bigs.append(printchunk(elt))
  312.  
  313. #I knew there were only 2, could have done this more generally
  314. bigall = bigs[0] + bigs[1]
  315.  
  316. im1 = bigs[0]/255.
  317. im2 = bigs[1]/255.
  318. im3 = bigall/255.
  319.  
  320. img.imsave('testplot1.jpg',im1,format='JPEG')
  321. img.imsave('testplot2.jpg',im2,format='JPEG')
  322. img.imsave('testplot3.jpg',im3,format='JPEG')
  323. plt.imshow(bigall/255.)
  324. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment