11
11
# http://www.pythonchallenge.com/pc/hex/decent.html
12
12
13
13
from auth import read_riddle
14
- from wand .image import Image
15
- from zipfile import crc32 # NOQA used by _update_crc recompiled in patch_update_crc
14
+ from image import image_to_text
15
+ from importlib import import_module
16
+ from io import BytesIO
17
+ from PIL import Image
18
+ from wand .exceptions import BaseError as WandBaseError
19
+ from wand .image import Image as WandImage
16
20
17
21
import inspect
18
22
import zipfile
19
23
20
24
25
+ def get_ambiguity_data ():
26
+ ambiguity = import_module ("24-ambiguity" )
27
+ return ambiguity .data
28
+
29
+
21
30
def extract_source (func ):
22
31
"""Extracts source of func"""
23
32
source = inspect .getsource (func ).splitlines ()
@@ -26,45 +35,42 @@ def extract_source(func):
26
35
27
36
28
37
def patch_update_crc ():
29
- """Monkey-patch `ZipExtFile._update_crc` to bypass any bad CRC"""
38
+ """Monkey-patches `ZipExtFile._update_crc` to bypass any bad CRC"""
30
39
update_crc_src = extract_source (zipfile .ZipExtFile ._update_crc ).replace (
31
40
"raise" , "return #"
32
41
)
33
- exec (compile (update_crc_src , "<string>" , "exec" ))
34
- zipfile .ZipExtFile ._update_crc = locals ()["_update_crc" ] # defined at runtime above
42
+ exec_globals = {"crc32" : zipfile .crc32 }
43
+ exec (compile (update_crc_src , "<string>" , "exec" ), exec_globals )
44
+ zipfile .ZipExtFile ._update_crc = exec_globals ["_update_crc" ]
35
45
36
46
37
- def reveal_mybroken_gif ( ):
38
- """Extract a gif from a corrupt zip file and displays it as text """
47
+ def extract_image ( zip_data ):
48
+ """Extracts an image from a corrupt zip file inside `zip_data` """
39
49
patch_update_crc ()
40
- try :
41
- with zipfile .ZipFile ("mybroken.zip" , "r" ) as zip_file , zip_file .open (
42
- zip_file .namelist ()[0 ]
43
- ) as gif_file :
44
- # Pillow couldn't read the corrupt gif, even with
45
- # PIL.ImageFile.LOAD_TRUNCATED_IMAGES set to True
46
- mybroken , text = Image (file = gif_file ), ""
47
- for y in range (0 , mybroken .size [1 ], 2 ):
48
- line = ""
49
- for x in range (0 , mybroken .size [0 ], 2 ):
50
- if not mybroken [x , y ].blue :
51
- line += " "
52
- else :
53
- line += "*"
54
- if "*" in line :
55
- text += line + "\n "
56
- return text
57
- except FileNotFoundError :
58
- return "Please run mission 24 and rerun this mission"
59
-
60
-
61
- print (reveal_mybroken_gif ())
50
+ with zipfile .ZipFile (BytesIO (zip_data )) as outer :
51
+ for name in outer .namelist ():
52
+ try :
53
+ with zipfile .ZipFile (outer .open (name )) as inner , inner .open (
54
+ inner .namelist ()[0 ]
55
+ ) as img_file :
56
+ # Pillow couldn't read the corrupt gif, even with
57
+ # PIL.ImageFile.LOAD_TRUNCATED_IMAGES set to True
58
+ return WandImage (file = img_file )
59
+ except (zipfile .BadZipFile , WandBaseError ):
60
+ pass
61
+
62
+
63
+ ambiguity_data = get_ambiguity_data ()
64
+ image = extract_image (ambiguity_data )
65
+ image_data = image .export_pixels ()
66
+ pil_image = Image .frombytes ("RGBA" , image .size , bytes (image_data ))
67
+ print (image_to_text (pil_image , skip = 3 ))
62
68
63
69
for line in read_riddle (
64
70
"http://www.pythonchallenge.com/pc/hex/decent.html"
65
71
).splitlines ():
66
- if "<" in line :
67
- continue
68
72
if not line :
69
73
break
70
- print (line )
74
+ if "<" in line :
75
+ continue
76
+ print (line .rsplit (maxsplit = 1 )[- 1 ])
0 commit comments