Advertisement
Guest User

Untitled

a guest
Jun 4th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from src.library import *
  4. from libraries import OCR
  5. import Global as G
  6. from PIL import Image
  7.  
  8. all_colors = ['white','orange','cyan','red','green','blue','yellow','purple','pink']
  9. target = search.Pixels()
  10.  
  11. UpChars2  = OCR.loadfonts('UpChars2')
  12. UpChars   = OCR.loadfonts('UpChars')
  13. ChatChars = OCR.loadfonts('ChatChars')
  14. MenuChars = OCR.loadfonts('MenuChars')
  15.  
  16. '''
  17. .......... A lot of missing shit here ...........
  18. ......
  19. ...
  20.  
  21.  
  22. '''
  23.  
  24. #Bool / Rect
  25. def __extractTextRect(box, colors):
  26.     ''' I down scale the image for faster rect extraction,
  27.        Then I upscale the found rects again.
  28.    '''
  29.     scale = 0.7
  30.     size = (box[2]-box[0]), (box[3]-box[1])
  31.  
  32.     points = list()
  33.     for cl in colors:
  34.         points += target.find(cl, box, 2)
  35.     if not points: return False
  36.    
  37.     im = Image.new('RGB', size, 0)
  38.     pix = im.load()
  39.     for pt in points:
  40.         x,y = (pt[0]-box[0]), (pt[1]-box[1])
  41.         pix[x,y] = (255,255,255)
  42.  
  43.     W = int(round(size[0]*scale))
  44.     H = int(round(size[1]*scale))
  45.     im = im.resize((W,H), Image.NEAREST)
  46.    
  47.     target.set_target(im)
  48.     colors = target.find((255,255,255), [0,0,W,H], 0)
  49.     target.set_target(None)
  50.     if colors:
  51.         clusters = pointtools.group_ex(colors, 9,1)
  52.        
  53.         rects = list()
  54.         for pts in clusters:
  55.             (x1,y1,x2,y2) = pointtools.bounds(pts)
  56.             x1 /= scale;  y1 /= scale
  57.             x2 /= scale;  y2 /= scale
  58.             W,H = (x2-x1), (y2-y1)
  59.             if W > 7 and H > 7:
  60.                 rects.append((round(box[0]+x1)-4, round(box[1]+y1)-1,
  61.                               round(box[0]+x2)+4, round(box[1]+y2)+1))
  62.          
  63.         return rects, points
  64.     return False
  65.  
  66. #Bool / String
  67. def rs07_isScreenText(text, box=G.SCAPE['MAIN'], colors=[(255,255,0)]):
  68.     data = __extractTextRect(box, colors)
  69.     if not data: return False
  70.     rectangles, points = data
  71.     if rectangles:
  72.         strings = list()
  73.         im = Image.new('RGB', box[2:4], 0)
  74.         pix = im.load()
  75.         for pt in points:
  76.             pix[pt[0], pt[1]] = (255,255,255)
  77.  
  78.         for rect in rectangles:
  79.             rect = [int(round(x)) for x in rect]
  80.             string = OCR.imageToString2(im.crop(rect), UpChars2, ['white'], 5)
  81.             strings.append(string)
  82.        
  83.         if strings:
  84.             if isinstance(text, str):
  85.                 for haystack in strings:
  86.                     if text in haystack.lower():
  87.                         return haystack
  88.             else:
  89.                 for haystack in strings:
  90.                     for part in text:
  91.                         if part in haystack.lower():
  92.                             return haystack
  93.     return False
  94.    
  95. def rs07_getScreenText(box=G.SCAPE['MAIN'], colors=[(255,255,0)]):
  96.     data = __extractTextRect(box, colors)
  97.     if not data: return False
  98.     rectangles, points = data
  99.     if rectangles:
  100.         strings = list()
  101.         im = Image.new('RGB', box[2:4], 0)
  102.         pix = im.load()
  103.         for pt in points:
  104.             pix[pt[0], pt[1]] = (255,255,255)
  105.  
  106.         for rect in rectangles:
  107.             rect = [int(round(x)) for x in rect]
  108.             string = OCR.imageToString2(im.crop(rect), UpChars2, ['white'], 5)
  109.             strings.append(string)
  110.        
  111.         return strings
  112.     return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement