def get_tags(driver, path, filter=False): tags = set() frames = [] video = path.suffix in ('.gif', '.webm', '.mp4') if video: tags.add('animated') if path.suffix in ('.webm', '.mp4'): try: for stream in FFProbe(str(path)).streams: if stream.codec_type == 'audio': tags.add('audio') break except: pass vidcap = VideoCapture(str(path)) frame_count = int(vidcap.get(CAP_PROP_FRAME_COUNT)) vidcap.release() elif path.suffix in ('.gif'): gifcap = GifImagePlugin.GifImageFile(str(path)) frame_count = gifcap.n_frames gifcap.close() step = 90 * log((frame_count * .002) + 1) + 1 frames = video_generator(path, round(step)) else: frames.append(path) for frame in frames: driver.get('http://dev.kanotype.net:8003/deepdanbooru/') driver.find('//*[@id="exampleFormControlFile1"]', str(frame)) driver.find('//button[@type="submit"]', click=True) for _ in range(4): html = bs4.BeautifulSoup(driver.page_source(), 'lxml') try: tags.update([ tag.text for tag in html.find('tbody').findAll(href=True) ]) break except AttributeError: if driver.current_url().endswith('deepdanbooru/'): driver.find('//*[@id="exampleFormControlFile1"]', str(frame)) driver.find('//button[@type="submit"]', click=True) driver.refresh() if filter: tags.difference_update(REMOVE) return ' '.join(tags)
def level_22(): f=GifImagePlugin.GifImageFile(r'white.gif') my=Image.new('RGB',(640,480)) # 用于画字符的图片 draw=ImageDraw.Draw(my) curpoint=[0,0] pointlist=[tuple(curpoint)] frameno=1 while True: print ('frame=%d'%(frameno,)) try: for y in range(98,103):#f.size[1]): for x in range(98,103):#f.size[0]): if f.getpixel((x,y))!=0: print ('%d: %d,%d=%d'%(frameno,x,y,f.getpixel((x,y)))) # 可知每帧里面都有个调色板索引为8的点,坐标范围在(98,98)-(102,102)之间且坐标为偶数,相当于在3×3的九宫格中 k='%d-%d'%(x,y) # 到此为止没有思路了。参考攻略,原来是网页上那个游戏摇杆的图片是暗示你九宫格的点是用矢量方法记录的,可以据此划出线条来,太有想象力了! #(98,98) (100,98) (102,98) (-1,-1) (0,-1) (1,-1) #(98,100)(100,100)(102,100) == (-1,0) (0,0) (1,0) #(98,102)(100,102)(102,102) (-1,1) (0,1) (1,1) d={ '98-98':(-5,-5),'100-98':(0,-5),'102-98':(5,-5), '98-100':(-5,0),'100-100':(0,0),'102-100':(5,0), '98-102':(-5,5),'100-102':(0,5),'102-102':(5,5)} if d[k]==(0,0): print ('回到原点,开始画新字符') if len(pointlist)>1: draw.line(pointlist) # 画出字符 del pointlist[:] # 清空列表 curpoint[0]+=50 # 设定开始画字符的坐标,以免把上一个字符覆盖 curpoint[1]+=50 pointlist.append(tuple(curpoint)) continue curpoint[0]+=d[k][0] curpoint[1]+=d[k][1] pointlist.append(tuple(curpoint)) f.seek(f.tell()+1) # 跳到下一帧 frameno+=1 except EOFError as e: # 最后一帧后会触发此异常 print ('end of frame.%d'%(frameno,)) # 可知此GIF有133帧 draw.line(pointlist) # 画出最后一个字符 my.save(r'23_white.png','png') # 能看出画的字符为 bonus ==> http://www.pythonchallenge.com/pc/hex/bonus.html break
def level_16(): ## # 尝试把不是粉短线的颜色都去掉 ## f=GifImagePlugin.GifImageFile(ur'd:\mozart.gif') ## mf=PIL.Image.new('P',f.size) ## for y in range(f.size[1]): ## for x in range(f.size[0]): ## if f.getpixel((x,y))==195: ## mf.putpixel((x,y),195) ## mf.save(ur'd:\16.gif','gif') # 啥都看不出来 查看攻略 原来所谓get this straight是说把这些粉短线都按行对齐 f=GifImagePlugin.GifImageFile(r'mozart.gif') mf=Image.new('P',f.size) mf.putpalette(f.getpalette()) # 用原图的调色板 for y in range(f.size[1]): for x in range(f.size[0]): if f.getpixel((x,y))==195: # 找到头一个粉色像素 以此为对准线 for mx in range(f.size[0]): mf.putpixel((mx,y),f.getpixel(((mx+x)%f.size[0],y))) #往前走mx<--mx+x之前替换 # 将从这个粉色像素开始的一行复制到新图片中 break mf.save(r'mozart16.gif','gif')
def test_invalid_file(self): invalid_file = "Tests/images/flower.jpg" self.assertRaises(SyntaxError, lambda: GifImagePlugin.GifImageFile(invalid_file))
def test_invalid_file(): invalid_file = "Tests/images/flower.jpg" with pytest.raises(SyntaxError): GifImagePlugin.GifImageFile(invalid_file)
def level_27(): zig=GifImagePlugin.GifImageFile(r'zigzag.gif') zigdata=zig.tostring() import array data=[] for i in zigdata[:10]: data.append(i) data = array.array("B", data).tostring() print("zigdata,10:",data[:10]) print (''.join(['%X'%(i,) for i in zigdata[:20]])) # 查看前面几个字节,没有头绪) #print (len(zig.getcolors())) palette=zig.palette.getdata()[1][::3] # 获取其调色板 data1=[] for i in palette[:10]: data1.append(i) data1 = array.array("B", data1).tostring() print("palette,10:",palette[:10]) #print(data1,type(data1)) print (''.join(['%X'%(i,) for i in palette[:20]])) #t=string.maketrans(''.join([chr(i) for i in range(256)]),palette) ch1=b''.join([bytes([i]) for i in range(256)]) t=bytes.maketrans(ch1,palette) zigtrans=zigdata.translate(t) # 用调色板值转换像素值 print("zigtrans,10",zigtrans[:10]) print (''.join(['%X'%(i,) for i in zigtrans[:20]])) #实现hex(zigtrans[:20],python3下,前面为python2版 # 还是看不出来什么,不过似乎转换后的数据除了第一个字节外都与原数据很相似) print (zigdata[1:]==zigtrans[:-1]) # 尝试将两组数据中所有不相同的字节放在一起 deltas=filter(lambda p:p[0]!=p[1],zip(zigdata[1:],zigtrans[:-1])) diffs=[b''.join([bytes([p[i]]) for p in deltas]) for i in range(2)] print (diffs[0][:20]) # 看起来是个bz文件) print (diffs[1][:20]) #这一句结果与别人的不同也. bz=bz2.BZ2Decompressor().decompress(diffs[0]) #bz=bz2.decompress(diffs[0])#在python3下要用上面这一句,真是奇怪 #也许比较的是字节吧 print (len(bz)) print (bz[:100]) # 输出是python的关键字和地址 ../ring/bell.html keywords=bz.split(b' ') keys={} for k in keywords: keys[k]=1 print (keys.keys()) print (len(keywords)) print (len(keys.keys())) # 很多关键字 print (len(keywords)/len(keys.keys())) # 将不一样的像素按位置显示出来 im=Image.new('1',zig.size,0) im.putdata([ p[0]==p[1] for p in zip(zigdata[1:],zigtrans[:-1])]) #im.show() # 有个钥匙图样,左边是not,右边是word,下面是busy? # 暗示 not key word # 那么找找上面看到的那些关键字里面哪些不是key word im.save("27_notkeyword.png","png") for k in keys.keys(): k=str(k)[2:-1] if not keyword.iskeyword(k): print (k) # 打印出来 switch 和 repeat 不是关键字,../ring/bell.html不算。 # 用switch 和 repeat 分别做用户名和密码,还有print,exec,当然这是python3下的 k1,k2='switch','repeat' t=((k1+":"+k2),(k2+":"+k1)) n1 = urllib.request.FancyURLopener() for i in t: try: url='http://'+i+'@www.pythonchallenge.com/pc/ring/bell.html' r=n1.open(url) if r: print ('got, %s '%(i)) # 正确的用户名是repeat 密码是 switch ==> http://www.pythonchallenge.com/pc/ring/bell.html break except urllib.request.HTTPError: pass
def gif_image(): return GifImagePlugin.GifImageFile(create_test_image('gif'))