2 # -*- coding: utf-8 -*-
8 from functools
import reduce
11 from PIL
import Image
as PilImage
, ImageChops
13 from manga_py
.crypt
import sunday_webry_com
14 from manga_py
.crypt
.puzzle
import Puzzle
15 from manga_py
.crypt
import mangago_me
16 from manga_py
.crypt
import viz_com
18 root_path
= path
.dirname(path
.realpath(__file__
))
21 class TestMatrix(unittest
.TestCase
):
23 def _rmsdiff(im1
, im2
):
24 """Calculate the root-mean-square difference between two images"""
25 h
= ImageChops
.difference(im1
, im2
).histogram()
30 map(lambda h
, i
: h
* (i
** 2), h
, range(256))
31 ) / (float(im1
.size
[0]) * im1
.size
[1])
35 file_src
= root_path
+ '/mosaic/tonarinoyj_jp_orig.jpg' # tonarinoyj.jp image
36 file_ref
= root_path
+ '/mosaic/tonarinoyj_jp_reference.jpg'
37 file_dst
= root_path
+ '/temp/tonarinoyj_jp_mosaic.jpg'
41 for i
in range(div_num
* div_num
):
42 matrix
[i
] = (i
% div_num
) * div_num
+ int(i
/ div_num
)
43 p
= Puzzle(div_num
, div_num
, matrix
, 8)
44 p
.need_copy_orig
= True
45 p
.de_scramble(file_src
, file_dst
)
47 src
= PilImage
.open(file_dst
)
48 ref
= PilImage
.open(file_ref
)
50 deviation
= self
._rmsdiff
(src
, ref
)
53 self
.assertTrue(deviation
< 10)
56 file_src
= root_path
+ '/mosaic/tonarinoyj_jp_orig.png' # tonarinoyj.jp image
57 file_ref
= root_path
+ '/mosaic/tonarinoyj_jp_reference.png'
58 file_dst
= root_path
+ '/temp/tonarinoyj_jp_mosaic.png'
62 for i
in range(div_num
* div_num
):
63 matrix
[i
] = (i
% div_num
) * div_num
+ int(i
/ div_num
)
64 p
= Puzzle(div_num
, div_num
, matrix
, 8)
65 p
.need_copy_orig
= True
66 p
.de_scramble(file_src
, file_dst
)
68 src
= PilImage
.open(file_dst
)
69 ref
= PilImage
.open(file_ref
)
71 deviation
= self
._rmsdiff
(src
, ref
)
74 self
.assertTrue(deviation
< 10)
76 def test_sunday_webry_com(self
):
77 decoder
= sunday_webry_com
.SundayWebryCom()
79 with
open(root_path
+ '/mosaic/sunday_reference_matrix.json') as f
:
80 result
= json
.loads(f
.read())
83 for _i
, _r
in enumerate(result
):
85 result_py
= decoder
.solve(848, 1200, 64, 64, _i
+ 1)
87 for i
, r
in enumerate(_r
):
90 r
['srcX'] != p
['srcX'] or
91 r
['srcY'] != p
['srcY'] or
92 r
['destX'] != p
['destX'] or
93 r
['destY'] != p
['destY'] or
94 r
['width'] != p
['width'] or
95 r
['height'] != p
['height']
99 self
.assertTrue(n
< 1)
101 def test_solve_sunday_webry_com(self
):
102 decoder
= sunday_webry_com
.SundayWebryCom()
103 puzzle
= sunday_webry_com
.MatrixSunday()
105 src
= root_path
+ '/mosaic/sunday_orig.jpg'
106 file_dst
= root_path
+ '/temp/sunday_mosaic2.jpg'
107 file_ref
= root_path
+ '/mosaic/sunday_reference.jpg'
109 result_py2
= decoder
.solve_by_img(src
, 64, 64, 2)
111 puzzle
.de_scramble(src
, file_dst
, result_py2
)
113 src
= PilImage
.open(file_dst
)
114 ref
= PilImage
.open(file_ref
)
116 deviation
= self
._rmsdiff
(src
, ref
)
118 self
.assertTrue(deviation
< 10)
120 def test_solve_plus_comico_js(self
):
121 src
= root_path
+ '/mosaic/plus_comico_jp_orig.jpg'
122 file_dst
= root_path
+ '/temp/plus_comico_jp_mosaic.jpg'
123 file_ref
= root_path
+ '/mosaic/plus_comico_jp_reference.jpg'
125 _matrix
= '3,14,5,8,10,12,4,2,1,6,15,13,7,11,0,9'.split(',')
134 p
= Puzzle(div_num
, div_num
, matrix
, 8)
135 p
.need_copy_orig
= True
136 p
.de_scramble(src
, file_dst
)
138 src
= PilImage
.open(file_dst
)
139 ref
= PilImage
.open(file_ref
)
141 deviation
= self
._rmsdiff
(src
, ref
)
144 self
.assertTrue(deviation
< 10)
146 def test_solve_mangago(self
):
149 'mangago1_orig.jpeg',
150 'mangago1_reference.jpeg',
151 'http://iweb7.mangapicgallery.com/r/cspiclink/make_me_bark/418858/962c5915bbe6f4ab0903149b5d94baba796a5cf059389458858fdeb74ddc02a4.jpeg'
154 'mangago2_orig.jpeg',
155 'mangago2_reference.jpeg',
156 'http://iweb7.mangapicgallery.com/r/cspiclink/make_me_bark/418858/53e50ae9291f4ab0903149b5d94baba796a5cf059383846d7d1f4dc72e9f75f9.jpeg'
159 'mangago3_orig.jpeg',
160 'mangago3_reference.jpeg',
161 'http://iweb7.mangapicgallery.com/r/cspiclink/make_me_bark/418859/c56e8e1f5baf770212313f5e9532ec5e6103b61e956e06496929048f98e33004.jpeg'
164 'mangago4_orig.jpeg',
165 'mangago4_reference.jpeg',
166 'http://iweb7.mangapicgallery.com/r/cspiclink/make_me_bark/418859/5af9065f5b2e2169a4bfd805e9aa21d3112d498d68c6caa9046af4b06a723170.jpeg'
169 'mangago5_orig.jpeg',
170 'mangago5_reference.jpeg',
171 'http://iweb7.mangapicgallery.com/r/cspiclink/make_me_bark/418859/34f05a15df5ae2169a4bfd805e9aa21d3112d498d68c765523c20c307fa0fda2.jpeg'
174 'mangago6_orig.jpeg',
175 'mangago6_reference.jpeg',
176 'http://iweb7.mangapicgallery.com/r/cspiclink/make_me_bark/418864/0d76361a3cf5baf770212313f5e9532ec5e6103b616618337cb81b6acf9c1912.jpeg'
179 'mangago7_orig.jpeg',
180 'mangago7_reference.jpeg',
181 'http://iweb7.mangapicgallery.com/r/cspiclink/lookism/443703/dbdf873a11bafad56c41ff7fbed622aa76e19f3564e5d52a6688d6d9e3c57fb2.jpeg'
186 img
= path
.join(root_path
, 'mosaic', i
[0])
187 dst
= path
.join(root_path
, 'temp', i
[0])
189 ref
= path
.join(root_path
, 'mosaic', i
[1])
191 mangago_me
.MangaGoMe
.puzzle(img
, dst
, i
[2])
194 src
= PilImage
.open(ref
)
195 ref
= PilImage
.open(dst
)
196 deviation
= self
._rmsdiff
(src
, ref
)
199 self
.assertTrue(deviation
< 10)
201 def test_solve_viz_com(self
):
204 src_path
= root_path
+ '/mosaic/viz/index{}.jfif'.format(i
)
205 ref_path
= root_path
+ '/temp/canvas{}.png'.format(i
)
206 solved_path
= root_path
+ '/mosaic/viz/canvas{}.png'.format(i
)
207 ref
= viz_com
.solve(src_path
, {'width': 800, 'height': 1200})
209 solved
= PilImage
.open(solved_path
)
210 deviation
= self
._rmsdiff
(solved
, ref
)
212 print(f
"Deviation: {deviation}")
214 deviations
.append(deviation
< 10)
216 self
.assertTrue(all(deviations
))