Update workflows/publish_pypi.yml
[manga-dl.git] / tests / matrix.py
blobc67e28a025e25726d4ba7e7b5d483bddcc0a78dc
1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
4 import json
5 import math
6 import operator
7 import unittest
8 from functools import reduce
9 from os import path
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):
22 @staticmethod
23 def _rmsdiff(im1, im2):
24 """Calculate the root-mean-square difference between two images"""
25 h = ImageChops.difference(im1, im2).histogram()
26 # calculate rms
27 return math.sqrt(
28 reduce(
29 operator.add,
30 map(lambda h, i: h * (i ** 2), h, range(256))
31 ) / (float(im1.size[0]) * im1.size[1])
34 def test_jpg(self):
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'
39 div_num = 4
40 matrix = {}
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)
51 src.close()
52 ref.close()
53 self.assertTrue(deviation < 10)
55 def test_png(self):
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'
60 div_num = 4
61 matrix = {}
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)
72 src.close()
73 ref.close()
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())
82 n = 0
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):
88 p = result_py[i]
89 if (
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']
97 n += 1
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(',')
127 div_num = 4
128 matrix = {}
129 n = 0
130 for i in _matrix:
131 matrix[int(i)] = n
132 n += 1
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)
142 src.close()
143 ref.close()
144 self.assertTrue(deviation < 10)
146 def test_solve_mangago(self):
147 urls = [
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'
185 for i in urls:
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])
193 # compare
194 src = PilImage.open(ref)
195 ref = PilImage.open(dst)
196 deviation = self._rmsdiff(src, ref)
197 src.close()
198 ref.close()
199 self.assertTrue(deviation < 10)
201 def test_solve_viz_com(self):
202 deviations = []
203 for i in range(7):
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})
208 ref.save(ref_path)
209 solved = PilImage.open(solved_path)
210 deviation = self._rmsdiff(solved, ref)
211 solved.close()
212 print(f"Deviation: {deviation}")
214 deviations.append(deviation < 10)
216 self.assertTrue(all(deviations))