Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. from PIL import Image
  2. import os, sys
  3. import colorsys
  4. import time
  5. import picamera
  6. import RPi.GPIO as GPIO
  7.  
  8.  
  9. class Process_Picture(object):
  10. ''' loop through each pixel and average rgb '''
  11. def __init__(self, imageName):
  12.  
  13. self.pic = imageName
  14. self.imgData = self.pic.load()
  15.  
  16. def averagePixels(self):
  17. r, g, b = 0, 0, 0
  18. count = 0
  19. for x in range(int(self.pic.size[0])):
  20. for y in range(int(self.pic.size[1])):
  21. tempr,tempg,tempb = self.imgData[x,y]
  22. r += tempr
  23. g += tempg
  24. b += tempb
  25. count += 1
  26. # calculate averages
  27. ravg = int(r/count)
  28. gavg = int(g/count)
  29. bavg = int(b/count)
  30. # print(ravg, gavg, bavg, count)
  31. return (ravg,gavg,bavg)
  32.  
  33. def computeDGCI(self, rg, gg, bg):
  34. h,s,v =colorsys.rgb_to_hsv( rg/255, gg/255, bg/255 )
  35. print (h, s, v)
  36. HUE = 360*h
  37. print (HUE)
  38. DGCI = (((HUE-60)/60 + (1-s) + (1-v))/3)
  39. print (DGCI)
  40. return DGCI
  41.  
  42. if __name__ == '__main__':
  43. with picamera.PiCamera() as camera:
  44. camera.resolution = (400, 400)
  45. camera.start_preview
  46. time.sleep ( 2 )
  47. camera.capture('test.jpg')
  48. picture1 = Image.open("test.jpg")
  49.  
  50. # picture1.imgData = picture1.load()
  51. pc1 = Process_Picture(picture1)
  52. a,b,c = pc1.averagePixels()
  53. print(a,b,c)
  54. pc1.computeDGCI(a, b, c)
  55.  
  56.  
  57. # Divide picture in four parts
  58.  
  59. x = int(picture1.size[0])
  60. y = int(picture1.size[1])
  61. x1 = int(picture1.size[0]/2)
  62. y1 = int(picture1.size[1]/2)
  63.  
  64. picture1_1 = picture1.transform(picture1.size, Image.EXTENT, (0,0,x1,y1))
  65. picture1_2 = picture1.transform(picture1.size, Image.EXTENT, (x1,0,x,y1))
  66. picture1_3 = picture1.transform(picture1.size, Image.EXTENT, (0,y1,x1,y))
  67. picture1_4 = picture1.transform(picture1.size, Image.EXTENT, (x1,y1,x,y))
  68.  
  69. # Compute DGCI for image segment1
  70. # picture1_1.imgData = picture1_1.load()
  71. pc1_1 = Process_Picture(picture1_1)
  72. R1_1,G1_1,B1_1 = pc1_1.averagePixels()
  73. print(R1_1,G1_1,B1_1)
  74. DGCI_1_1 = pc1_1.computeDGCI(R1_1,G1_1,B1_1)
  75. # Compute DGCI for image segment2
  76. # picture1_2.imgData = picture1_2.load()
  77. pc1_2 = Process_Picture(picture1_2)
  78. R1_2,G1_2,B1_2 = pc1_2.averagePixels()
  79. print(R1_2,G1_2,B1_2)
  80. DGCI_1_2 = pc1_2.computeDGCI(R1_2,G1_2,B1_2)
  81. # Compute DGCI for image segment3
  82. # picture1_3.imgData = picture1_3.load()
  83. pc1_3 = Process_Picture(picture1_3)
  84. R1_3,G1_3,B1_3= pc1_3.averagePixels()
  85. print(R1_3,G1_3,B1_3)
  86. DGCI_1_3 = pc1_3.computeDGCI(R1_3,G1_3,B1_3)
  87. # Compute DGCI for image segment4
  88. # picture1_4.imgData = picture1_4.load()
  89. pc1_4 = Process_Picture(picture1_4)
  90. R1_4,G1_4,B1_4 = pc1_4.averagePixels()
  91. print(R1_4,G1_4,B1_4)
  92. DGCI_1_4 = pc1_4.computeDGCI(R1_4,G1_4,B1_4)
  93. GPIO.setmode(GPIO.BOARD)
  94. RPI.GPIO.setup(2, RPi.GPIO.OUT)
  95. GPIO.output(24, 1)
  96. sleep(3)
  97. GPIO.output(24, 0)
  98. sleep(3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement