Advertisement
Guest User

brightness

a guest
Dec 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. import numpy as np
  2. from sklearn.cluster import KMeans
  3. import matplotlib.pyplot as plt
  4. import requests
  5. import cv2
  6. from math import sqrt
  7.  
  8. def urlToImage(url):
  9.     global image
  10.     response = requests.get(url)
  11.     image = np.asarray(bytearray(response.content), dtype='uint8')
  12.     image = cv2.imdecode(image, cv2.IMREAD_COLOR)
  13.  
  14.     return image
  15.  
  16. def imageProcessing(image):
  17.     global img
  18.     image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  19.  
  20.     height, width = image.shape[:2]
  21.     img = cv2.resize(image, (int(round(width/10)), int(round(height/10))), interpolation = cv2.INTER_AREA)
  22.     img = img.reshape((img.shape[0]*img.shape[1], 3))
  23.  
  24.     return img
  25.  
  26. def findBrightness(img):
  27.     global lighter, darker
  28.     lighter = 0
  29.     darker = 0
  30.     for c in img:
  31.         r = c[0]
  32.         g = c[1]
  33.         g = c[2]
  34.  
  35.         brightness = sqrt(0.299*r*r + 0.587*g*g + 0.114*b*b)
  36.  
  37.         if brightness > 0.5:
  38.             lighter = lighter+1
  39.         if brightness < 0.5:
  40.             darker = darker+1
  41.         if lighter>darker:
  42.             return print('image is higher in brightness')
  43.         if lighter<darker:
  44.             return print('image is lower in brightness')
  45. urlToImage('https://www.gardapost.it/wp-content/uploads/2019/04/foresta-bosco.jpg')
  46. imageProcessing(image)
  47. findBrightness(img)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement