Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- from PIL import Image
- #from http://rosettacode.org/wiki/Map_range#Python
- def maprange( a, b, s):
- (a1, a2), (b1, b2) = a, b
- r = b1 + ((s - a1) * (b2 - b1) / float((a2 - a1)))
- return r
- #from http://www.roguebasin.com/index.php?title=Bresenham%27s_Line_Algorithm#Python
- def get_line(x1, y1, x2, y2):
- points = []
- issteep = abs(y2-y1) > abs(x2-x1)
- if issteep:
- x1, y1 = y1, x1
- x2, y2 = y2, x2
- rev = False
- if x1 > x2:
- x1, x2 = x2, x1
- y1, y2 = y2, y1
- rev = True
- deltax = x2 - x1
- deltay = abs(y2-y1)
- error = int(deltax / 2)
- y = y1
- ystep = None
- if y1 < y2:
- ystep = 1
- else:
- ystep = -1
- for x in range(x1, x2 + 1):
- if issteep:
- points.append((y, x))
- else:
- points.append((x, y))
- error -= deltay
- if error < 0:
- y += ystep
- error += deltax
- # Reverse the list if the coordinates were reversed
- if rev:
- points.reverse()
- return points
- def polar(r,angle):
- x=r*math.cos(angle)
- y=r*math.sin(angle)
- return (x,y)
- def readValue(image,value,debug=False):
- angleStep = math.pi/5
- target = polar(175,angleStep*value)
- target = (int(target[0]+320),int(target[1]+200))
- if debug: print target
- x = get_line(320,200,*target)
- if debug: print len(x)
- pxList = []
- for p in x:
- pxList.append(image.getpixel(p)[0])
- if debug: print pxList
- val = pxList.index(min(pxList))
- if debug: print val
- if debug: print x[val]
- blackList = [i for i,j in enumerate(pxList) if j<10]
- if debug: print blackList
- val = blackList[-1]
- if debug: print x[val]
- mappedVal = maprange((0,len(x)),(0,10),val)
- mappedVal = round(mappedVal,1)
- if debug: print 'val: {0}, mappedVal: {1}'.format(val, mappedVal)
- return mappedVal
- im = Image.open("img01.jpg")
- for i in range(10):
- print readValue(im,i)
- print
- #print readValue(im,0,True)
Add Comment
Please, Sign In to add comment