def do_compute(self): # step 1, get prompt information from web source print TextHelper.find_text_between_tag(self.web_source, '<!-- ', '-->') wav_suffix = 'wav' print "Above prompt words ask us to replace the suffix of jpg to %s" % wav_suffix wave_img_url = WebHelper.join_url( self.url, TextHelper.find_text_between_tag(self.web_source, '<img src="', '">')) print "wave image web url is %s" % wave_img_url ImageHelper.show_image_from_web( WebHelper.join_url(self.url, wave_img_url), self.user, self.password) print "This picture prompt us: there is 25 block, we need to combine them" # step 2, download waves wave_url = WebHelper.change_suffix_url(wave_img_url, wav_suffix) print "After change suffix: %s" % wave_url local_directory = 't25' FileHelper.mkdir(local_directory) waves = [] for i in range(25): store_path = FileHelper.join_path(local_directory, WebHelper.get_url_page(wave_url)) WebHelper.download_with_auth(wave_url, store_path, self.user, self.password) waves.append(wave.open(store_path)) wave_url = WebHelper.url_add(wave_url) # step 3, Create combine image from waves img = Image.new('RGB', (300, 300), 'black') frames = waves[0].getnframes() print "Totally waves: %d" % len(waves) for i in range(len(waves)): tmp_img = Image.frombytes('RGB', (60, 60), waves[i].readframes(frames)) current_pos = (60 * (i % 5), 60 * (i // 5)) print "Put image to position%s" % str(current_pos) img.paste(tmp_img, current_pos) img.show() # It shows a words 'decent' # step 4, set prompt words prompt_word = 'decent' print "Get prompt words from combined picture: %s" % prompt_word self.set_prompt(prompt_word) # step 5, clean unused file FileHelper.remove_folder(local_directory)
def do_compute(self): # step 1. get information from web source print TextHelper.find_text_between_tag(self.web_source, '<!--\n', '\n-->') beer_url = TextHelper.find_text_between_tag(self.web_source, '<img src="', '" border="0"') beer_url = WebHelper.join_url(self.url, beer_url) print beer_url beer_url = WebHelper.url_add(beer_url) beer_url = WebHelper.change_suffix_url(beer_url, 'png') # step 2. try out this picture beer_img = ImageHelper.create_image_from_web(beer_url, self.user, self.password) data = list(beer_img.getdata()) local_dir = 'img_data' FileHelper.mkdir(local_dir) print len(data) count = 0 while len(data) > 0: if self.is_int_sqrt(len(data)) and len(data) > 0: count += 1 size = int(math.sqrt(len(data))) print "data length = %i, sqrt value = %i" % (len(data), size) new_img = Image.new('RGB', (size, size)) new_img.putdata(data) new_img.save(os.path.join(local_dir, 'img_%i.png' % count)) max_value = max(data) data = [x for x in data if x != max_value] # step 3, Get hint from these pictures print "check png under %s, you will find many characters!" % local_dir prompt = 'gremlins' print "but only characters %s are in square" % str(list(prompt)) self.set_prompt(prompt) FileHelper.remove_folder(local_dir)