title: Make 2 Images with the same brightness (part 2)
body: <p>I began working on this topic few days ago and i was able to transfer the brightness of an image to another image by the help of the code: <a href="https://www.pyimagesearch.com/2014/06/30/super-fast-color-transfer-images/" rel="nofollow noreferrer">https://www.pyimagesearch.com/2014/06/30/super-fast-color-transfer-images/</a> . I used only the &quot;l channel&quot; in LAB since it represents the luminance of the image.</p>
  <p>My purpose this time is to change both images to get the same brightness (i.e The dark image become brighter and the bright image become darker). I  got the average brightness of both images and calculated the ratio of both of them, then multiplyed it with each image. The code i came up with is written below.</p>
  <pre><code>import cv2
  import numpy as np
  
  i = cv2.imread('cardark.jpeg')
  i2 = cv2.imread('bright3.jpeg')
  img = np.array(i, dtype=np.float)
  img2 = np.array(i2, dtype=np.float)
  height, width = img.shape[:2]
  height2, width2 = img2.shape[:2]
  img = img / 255
  img2 = img2 / 255
  summation = 0
  summation2 = 0
  count = 0
  count2 = 0
  
  for i in range(height):
      for j in range(width):
          summation = summation + img[i, j]
          count += 1
  for x in range(height2):
      for y in range(width2):
          summation2 = summation2 + img2[x, y]
          count2 += 1
  
  summation = (summation[0] + summation[1] + summation[2])/3
  summation2 = (summation2[0] + summation2[1] + summation2[2])/3
  average = summation / count
  average2 = summation2 / count2
  new_avg = (average+average2)/2
  r = new_avg / average
  r2 = new_avg / average2
  
  brightness = r
  brightness2 = r2
  image_bright = np.ones(img.shape, dtype=np.float) * brightness
  image_bright2 = np.ones(img2.shape, dtype=np.float) * brightness2
  image = img * image_bright
  image2 = img2 * image_bright2
  
  cv2.imshow('img', image)
  cv2.imshow('img2', image2)
  cv2.waitKey(0)
  cv2.destroyAllWindows()
  
  </code></pre>
  <p>My code works fine and it gave me good results but only for the very dark or bright images, for the images that are not so dark or bright i can't see the difference (i can see it in calculations but not in the brightness when i show it). The ratios of those images are very close and i guess this is the reason i don't see the difference. However, if you look at the images u see that they are bright enough to be changed but their value in the array is not.</p>
  <p>I hope someone can help me find the problem in my code or a better approach to reach my goal.</p>
  <p><strong>EDIT</strong></p>
  <p>The example images are:</p>
  <p><a href="https://i.stack.imgur.com/BKOty.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BKOty.jpg" alt="Bright Image" /></a>
  <a href="https://i.stack.imgur.com/83ba7.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/83ba7.jpg" alt="Dark Image" /></a></p>
  <p>The Results:</p>
  <p><a href="https://i.stack.imgur.com/rNWnb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rNWnb.png" alt="Bright Result" /></a>
  <a href="https://i.stack.imgur.com/nQtOk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nQtOk.png" alt="Dark Result" /></a></p>
  <p>The difference is less than slight, maybe if you concentrate very well in the photos but as you can see the images do differ in brightness at the beginning so it should make bigger difference in brightness.</p>