Iwangelion

Untitled

Apr 26th, 2015
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.08 KB | None | 0 0
  1. import math
  2. from PIL import Image
  3.  
  4. #from http://rosettacode.org/wiki/Map_range#Python
  5. def maprange( a, b, s):
  6.     (a1, a2), (b1, b2) = a, b  
  7.     r =  b1 + ((s - a1) * (b2 - b1) / float((a2 - a1)))
  8.     return r
  9.  
  10. #from http://www.roguebasin.com/index.php?title=Bresenham%27s_Line_Algorithm#Python
  11. def get_line(x1, y1, x2, y2):
  12.     points = []
  13.     issteep = abs(y2-y1) > abs(x2-x1)
  14.     if issteep:
  15.         x1, y1 = y1, x1
  16.         x2, y2 = y2, x2
  17.     rev = False
  18.     if x1 > x2:
  19.         x1, x2 = x2, x1
  20.         y1, y2 = y2, y1
  21.         rev = True
  22.     deltax = x2 - x1
  23.     deltay = abs(y2-y1)
  24.     error = int(deltax / 2)
  25.     y = y1
  26.     ystep = None
  27.     if y1 < y2:
  28.         ystep = 1
  29.     else:
  30.         ystep = -1
  31.     for x in range(x1, x2 + 1):
  32.         if issteep:
  33.             points.append((y, x))
  34.         else:
  35.             points.append((x, y))
  36.         error -= deltay
  37.         if error < 0:
  38.             y += ystep
  39.             error += deltax
  40.     # Reverse the list if the coordinates were reversed
  41.     if rev:
  42.         points.reverse()
  43.     return points
  44.  
  45. def polar(r,angle):
  46.     x=r*math.cos(angle)
  47.     y=r*math.sin(angle)
  48.     return (x,y)
  49.  
  50.  
  51. def readValue(image,value,debug=False):
  52.     angleStep = math.pi/5
  53.     target = polar(175,angleStep*value)
  54.     target = (int(target[0]+320),int(target[1]+200))
  55.     if debug: print target
  56.        
  57.     x = get_line(320,200,*target)
  58.     if debug: print len(x)
  59.  
  60.     pxList = []
  61.     for p in x:
  62.         pxList.append(image.getpixel(p)[0])
  63.     if debug: print pxList
  64.  
  65.     val = pxList.index(min(pxList))
  66.     if debug: print val
  67.     if debug: print x[val]
  68.    
  69.  
  70.     blackList = [i for i,j in enumerate(pxList) if j<10]
  71.     if debug: print blackList
  72.     val = blackList[-1]
  73.    
  74.     if debug: print x[val]
  75.     mappedVal = maprange((0,len(x)),(0,10),val)
  76.     mappedVal = round(mappedVal,1)
  77.     if debug: print 'val: {0}, mappedVal: {1}'.format(val, mappedVal)
  78.    
  79.     return mappedVal
  80.        
  81.  
  82. im = Image.open("img01.jpg")
  83.        
  84. for i in range(10):
  85.     print readValue(im,i)
  86. print
  87.  
  88. #print readValue(im,0,True)
Add Comment
Please, Sign In to add comment