Advertisement
Guest User

Untitled

a guest
Jun 4th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.38 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from src.library import *
  3. import time as t
  4. from PIL import Image, ImageDraw, ImageOps
  5. import os, glob, sys
  6. import cv2
  7. import numpy as np
  8.  
  9. target = search.Pixels()
  10. home = os.path.dirname(os.path.abspath(__file__))
  11.  
  12. class ContourWork():
  13.     def findContours(self, image, xmax, ymax, colors):
  14.         pts = []
  15.         w,h = image.size
  16.         target.set_target(image)
  17.  
  18.         if 'white' in colors or 'black' in colors:
  19.             target.set_mode(3)
  20.             pts.extend(target.find(box=(0,0,w,h), color=(227,227,227), tolerance=13)) # White
  21.         if 'red' in colors:
  22.             target.set_mode(0)
  23.             pts.extend(target.find(box=(0,0,w,h), color=(238,17,17), tolerance=25)) # Red
  24.         target.set_mode(3)
  25.         if 'green' in colors:
  26.             pts.extend(target.find(box=(0,0,w,h), color=(17,204,17), tolerance=25)) # Dark Green
  27.             pts.extend(target.find(box=(0,0,w,h), color=(0,255,0), tolerance=5)) # Green
  28.         if 'blue' in colors:
  29.             pts.extend(target.find(box=(0,0,w,h), color=(17,17,238), tolerance=25)) # Blue
  30.         if 'yellow' in colors:
  31.             pts.extend(target.find(box=(0,0,w,h), color=(230,230,0), tolerance=25)) # Yellow
  32.         if 'orange' in colors:
  33.             pts.extend(target.find(box=(0,0,w,h), color=(227,120,20), tolerance=28)) # Orange
  34.         target.set_mode(0)
  35.         if 'cyan' in colors:
  36.             pts.extend(target.find(box=(0,0,w,h), color=(11,205,205), tolerance=25)) # Cyan
  37.         if 'purple' in colors:
  38.             pts.extend(target.find(box=(0,0,w,h), color=(105, 0, 230), tolerance=25)) # purple
  39.         if 'pink' in colors:
  40.             pts.extend(target.find(box=(0,0,w,h), color=(200, 0, 230), tolerance=25)) # pink
  41.            
  42.         grouped = pointtools.group_ex(pts, xmax, ymax) ##This one is slowing me down a lil'.
  43.         if grouped:
  44.             return pointtools.sort_npoints_by_mean(grouped, (0,0))
  45.         else:
  46.             return []
  47.  
  48.     def findWhiteContours(self, image):
  49.         target.set_target(image)
  50.         W,H = image.size
  51.         pts = target.find(color=(255,255,255), box=(0,0,W,H), tolerance=35)
  52.         contours = pointtools.group_ex(pts, 1, 5)
  53.         return contours[0]
  54.  
  55.     def drawContours(self, image, contours, size):
  56.         XS,YS,XE,YE = pointtools.bounds(contours)
  57.         XS,YS,XE,YE = XS,YS,XE,YE
  58.         draw = ImageDraw.Draw(image)
  59.  
  60.         for i in range(0,size):
  61.             i+=1
  62.             draw.rectangle((XS-i,YS-i,XE+i,YE+i), outline=(255,0,0))
  63.  
  64.         del draw
  65.         return image
  66.  
  67. def loadfonts(font="UpChars2"):
  68.     fonts = []
  69.     Contour = ContourWork()
  70.     folder = (home + os.sep + 'Fonts' + os.sep + font + os.sep)
  71.     owd = os.getcwd()
  72.     os.chdir(folder)
  73.     for char in glob.glob("*.bmp"):
  74.         t = char[:-4]
  75.         image = Image.open(char)
  76.         x1,y1,x2,y2 = pointtools.bounds(Contour.findWhiteContours(image))
  77.         char = image.crop((x1-1,y1-1,x2+1,y2+1))
  78.         fonts.append((t, char))
  79.     os.chdir(owd)
  80.     return fonts
  81.  
  82. def imageCompare(im1, im2):
  83.     if im2.size == im1.size:
  84.       im1 = np.array(im1)
  85.       im2 = np.array(im2)
  86.       result = cv2.matchTemplate(im1, im2, cv2.TM_CCOEFF_NORMED) #CCORR_NORMED
  87.       return result.max()
  88.  
  89.     return -1
  90.  
  91. def __imageToString(template, fonts, colors, space):
  92.     if 'black' in colors: template = ImageOps.invert(template)
  93.    
  94.     catch, result = [], ""
  95.     Contour = ContourWork()
  96.     contours = Contour.findContours(template, 1,5, colors)
  97.     #Debugging::
  98.     #for cnt in contours:
  99.     #    x = Contour.drawContours(template, cnt, 1)
  100.     #if contours: x.show()
  101.    
  102.     for i, cont in enumerate(contours):
  103.       x1,y1,x2,y2 = pointtools.bounds(cont)
  104.       char = template.crop((x1-1,y1-1,x2+1,y2+1))
  105.  
  106.       for font in fonts:
  107.         ident, font_im = font
  108.         similarity = imageCompare(font_im, char)
  109.        
  110.         if similarity>=-0.9:
  111.           catch.append([similarity, ident])
  112.           if similarity>=0.98: break;
  113.  
  114.       if catch:
  115.         _,_,nx,_ = pointtools.bounds(contours[i-1])
  116.         if x1-nx >= space: result += " "
  117.         result += chr(int(max(catch, key=lambda x: x[0])[1]))
  118.         catch = []
  119.  
  120.     return result
  121.  
  122. def imageToString(img, font_set, colors=['white'], spacing=5):
  123.     if isinstance(font_set, str):
  124.         font_set = loadfonts(font_set)
  125.     return __imageToString(img, font_set, colors, spacing)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement