#-*-Coding:utf-8-*-""" @author: amtsing""" ''' Googletranslation ''' ImportExecjsclassPy4js(): def__init__(Self): Self.ctx= Execjs.compile(""" function TL (a) {var k = ""; var b = 406644; var b1 = 3293161072; var JD = "."; var $b = "+-a^+6"; var Zb = "+-3^+b+-f"; for (var e = [], f = 0, g = 0; g < a.length; g++) {var m = a.charcodeat (g); 128 > M e[f++] = m: (2048 > M e[f++] = m >> 6 |: (55296 = = (M & 64512)
( (M & 1023) <<) + (A.charcodeat (++g) & 1023),e[f++] = M >> |e[f++] = M >> A & | 128): e[f++] = m >> 12 | 224,e[f++] = M >> 6 & 128 |e[f++] = M & | 128) } a = b; for (f = 0; F < e.length f++) A + = E[f],a = RL (A, $b); a = RL (A, Zb); a ^= B1 | | 0; 0 > A && (A = (A & 2147483647) + 2147483648); a%= 1E6; return a.tostring () + JD + (a ^ b) }; function RL (A, b) {var t = "a"; var Yb = "+"; For (var c = 0; c < b.length-2; C + + 3) {var d = B.charat (c + 2),d = d >= t d.charcodeat (0)-87:number (d),d = B.charat (c + 1) = = Yb >>> d:a << D; a = B.charat (c) = = Yb? A + D & 4294967295:a ^ D } Return a } """) defGETTK(Self, text): returnSelf.ctx.call("TL", text) ImportUrllib.request, Urllib.parsedefOpen_url(Url): '''open a link to a Web page''' Headers= {' User-agent ': ' mozilla/5.0 (Windows NT 6.1; wow64;rv:23.0) gecko/20100101
firefox/23.0 '} Req= Urllib.request.Request(Url=URL, headers=Headers) Response= Urllib.request.urlopen(Req) Data= Response.read(). decode(' Utf-8 ') returnDatadefTranslate(Content): '''Define translation Functions''' ifLen(Content) >4891: Print("the length of the translation exceeds the limit ... ") return # get TK valueifLen(Content) >4891: Print("the length of the translation exceeds the limit ... ") return Js= Py4js() tk= Js.gettk(Content) # Encode content for input= Urllib.parse.quote(Content) Url= "Http://translate.google.cn/translate_a/single?client=t&sl=en&tl=zh-CN&hl=zh-CN&dt
=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=utf-8 &oe=UTF-8&
Source(tk, content)
# return value is a multi-layer nested list of string form, parse up also quite laborious, wrote a few regular, found also very not ideal, # later feel, use is simply to complicate things, here directly slices on OK result= Open_url(Url) End= Result.find("\",") ifEnd>4: Print(' '+ Result[4:End]) Output= (' '+ Result[4:End]) returnStr(Output) ''' Identify the authentication code ''' ImportOsImportRequests fromPiLImportImage,imagegrabImportPytesseract fromCollectionsImportCOUNTER,ORDEREDDICT # def downimg (URL): # ' download picture ' # with open (' verifycodepage.jpg ', ' WB ') as f: # s = Requests. Session () # response = S.get (URL) # f.write (response.content)defacumulate_colors(Image): '''statistics on color pixels''' Img= Image.open(Image) Pixdata= Img.load() # c = Counter () # Print (pixdata) colors= {} forY inRange(Img.size[1]): forX inRange(Img.size[0]): #print (pixdata[x, y]) # c.update (pixdata[x, y])ifPixdata[X, y] inColors: Colors[Pixdata[X, y]] += 1Else: Colors[Pixdata[X, y]] = 1 colors= Sorted(Colors.items(), key=LambdaD:D[1], reverse=True) # c = Ordereddict (c) # Print (C.values ())Print(Colors[2][0]) returnColors # def Gray (): # ' grayscale ' ' # img = Image.open (' verifycodepage.jpg ') # Img.convert (' L '). Save (' gray figure. jpg ')''' convert ()is a method of an image instance object that accepts aModeparameter to specify a color pattern,Modethe values can be as follows: • 1 (1-bit pixels, black and white, stored with one pixel per byte)· L (8-bit pixels, black and white)· P (8-bit pixels, mapped to no other mode using a colour palette)· RGB (3x8-bit pixels, true colour)· RGBA (4x8-bit pixels, true colour with transparency mask)· CMYK (4x8-bit pixels, colour separation)· YCbCr (3x8-bit pixels, Colour video format)· I (32-bit signed integer pixels)· F (32-bit floating point pixels)How about, rich enough. In fact, this is wherePiLThere are also limited support for the following relatively rare color patterns: LA (L with Alpha), RGBX (true colour with padding) and RGBa. ''' defbinary(Image): '''two of the value''' Img= Image.open(Image) Pixdata= Img.load() forY inRange(Img.size[1]): forX inRange(Img.size[0]): # if pixdata[x, y]!= colors[0][0]: # pixdata[x, y] = (255,255,255) # Else: # Pixdata[x, Y] = (0,0,0)ifPixdata[X, y][0] <115: Pixdata[X, y] = (0, 0, 0)# Black forY inRange(Img.size[1]):