Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from src.library import *
- import time as t
- from PIL import Image, ImageDraw, ImageOps
- import os, glob, sys
- import cv2
- import numpy as np
- target = search.Pixels()
- home = os.path.dirname(os.path.abspath(__file__))
- class ContourWork():
- def findContours(self, image, xmax, ymax, colors):
- pts = []
- w,h = image.size
- target.set_target(image)
- if 'white' in colors or 'black' in colors:
- target.set_mode(3)
- pts.extend(target.find(box=(0,0,w,h), color=(227,227,227), tolerance=13)) # White
- if 'red' in colors:
- target.set_mode(0)
- pts.extend(target.find(box=(0,0,w,h), color=(238,17,17), tolerance=25)) # Red
- target.set_mode(3)
- if 'green' in colors:
- pts.extend(target.find(box=(0,0,w,h), color=(17,204,17), tolerance=25)) # Dark Green
- pts.extend(target.find(box=(0,0,w,h), color=(0,255,0), tolerance=5)) # Green
- if 'blue' in colors:
- pts.extend(target.find(box=(0,0,w,h), color=(17,17,238), tolerance=25)) # Blue
- if 'yellow' in colors:
- pts.extend(target.find(box=(0,0,w,h), color=(230,230,0), tolerance=25)) # Yellow
- if 'orange' in colors:
- pts.extend(target.find(box=(0,0,w,h), color=(227,120,20), tolerance=28)) # Orange
- target.set_mode(0)
- if 'cyan' in colors:
- pts.extend(target.find(box=(0,0,w,h), color=(11,205,205), tolerance=25)) # Cyan
- if 'purple' in colors:
- pts.extend(target.find(box=(0,0,w,h), color=(105, 0, 230), tolerance=25)) # purple
- if 'pink' in colors:
- pts.extend(target.find(box=(0,0,w,h), color=(200, 0, 230), tolerance=25)) # pink
- grouped = pointtools.group_ex(pts, xmax, ymax) ##This one is slowing me down a lil'.
- if grouped:
- return pointtools.sort_npoints_by_mean(grouped, (0,0))
- else:
- return []
- def findWhiteContours(self, image):
- target.set_target(image)
- W,H = image.size
- pts = target.find(color=(255,255,255), box=(0,0,W,H), tolerance=35)
- contours = pointtools.group_ex(pts, 1, 5)
- return contours[0]
- def drawContours(self, image, contours, size):
- XS,YS,XE,YE = pointtools.bounds(contours)
- XS,YS,XE,YE = XS,YS,XE,YE
- draw = ImageDraw.Draw(image)
- for i in range(0,size):
- i+=1
- draw.rectangle((XS-i,YS-i,XE+i,YE+i), outline=(255,0,0))
- del draw
- return image
- def loadfonts(font="UpChars2"):
- fonts = []
- Contour = ContourWork()
- folder = (home + os.sep + 'Fonts' + os.sep + font + os.sep)
- owd = os.getcwd()
- os.chdir(folder)
- for char in glob.glob("*.bmp"):
- t = char[:-4]
- image = Image.open(char)
- x1,y1,x2,y2 = pointtools.bounds(Contour.findWhiteContours(image))
- char = image.crop((x1-1,y1-1,x2+1,y2+1))
- fonts.append((t, char))
- os.chdir(owd)
- return fonts
- def imageCompare(im1, im2):
- if im2.size == im1.size:
- im1 = np.array(im1)
- im2 = np.array(im2)
- result = cv2.matchTemplate(im1, im2, cv2.TM_CCOEFF_NORMED) #CCORR_NORMED
- return result.max()
- return -1
- def __imageToString(template, fonts, colors, space):
- if 'black' in colors: template = ImageOps.invert(template)
- catch, result = [], ""
- Contour = ContourWork()
- contours = Contour.findContours(template, 1,5, colors)
- #Debugging::
- #for cnt in contours:
- # x = Contour.drawContours(template, cnt, 1)
- #if contours: x.show()
- for i, cont in enumerate(contours):
- x1,y1,x2,y2 = pointtools.bounds(cont)
- char = template.crop((x1-1,y1-1,x2+1,y2+1))
- for font in fonts:
- ident, font_im = font
- similarity = imageCompare(font_im, char)
- if similarity>=-0.9:
- catch.append([similarity, ident])
- if similarity>=0.98: break;
- if catch:
- _,_,nx,_ = pointtools.bounds(contours[i-1])
- if x1-nx >= space: result += " "
- result += chr(int(max(catch, key=lambda x: x[0])[1]))
- catch = []
- return result
- def imageToString(img, font_set, colors=['white'], spacing=5):
- if isinstance(font_set, str):
- font_set = loadfonts(font_set)
- return __imageToString(img, font_set, colors, spacing)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement