Page 1 of 1

Get the overall brightness level of a picture?

Posted: Mon May 06, 2019 7:49 pm
by forumuser
Hi,

is it possible to load e.g. a .jpg file and analyze it for the level of brightness?
The picture should NOT be displayed!

Reason: I have thousands of pictures that were taken with very! low light and
are 95% just... black.

I'd like to identify those without human interaction :mrgreen:

Re: Get the overall brightness level of a picture?

Posted: Mon May 06, 2019 8:58 pm
by Mijikai
Read every 2nd pixel and compare it its grayscale value to a defined thereshold.
Get the number (%) of pixels that passed.

Re: Get the overall brightness level of a picture?

Posted: Mon May 06, 2019 9:13 pm
by Sicro

Code: Select all

UseJPEGImageDecoder()
UseJPEG2000ImageDecoder()
UsePNGImageDecoder()
UseTIFFImageDecoder()
UseTGAImageDecoder()
UseGIFImageDecoder()

Procedure.d GetBrightnessOfColor(color)
  ; http://fseitz.de/blog/index.php?/archives/112-Helligkeit-von-Farben-des-RGB-Farbraums-berechnen.html
  ProcedureReturn Sqr(0.299 * Pow(Red(color), 2) + 0.587 * Pow(Green(color), 2) + 0.114 * Pow(Blue(color), 2))
EndProcedure

Define.d totalImageBrightness, averageBrightness
Define   x, y, countOfAllfPixels, pixelColor
Define   imageFilePath$

imageFilePath$ = OpenFileRequester("Open image file", "", "", 0)

image = LoadImage(#PB_Any, imageFilePath$)
If Not image
  Debug "Error"
  End
EndIf

countOfAllfPixels = ImageWidth(image) * ImageHeight(image)

If Not StartDrawing(ImageOutput(image))
  Debug "Error"
  End
EndIf

For x = ImageWidth(image) - 1 To 0 Step -1
  For y = ImageHeight(image) - 1 To 0 Step -1
    pixelColor = Point(x, y)
    totalImageBrightness + GetBrightnessOfColor(pixelColor)
  Next
Next

StopDrawing()

FreeImage(image)

averageBrightness = totalImageBrightness / countOfAllfPixels

Debug averageBrightness

Re: Get the overall brightness level of a picture?

Posted: Mon May 06, 2019 10:35 pm
by forumuser
@Mijikai
Thanks for the hint!

@Sicro
Holy cow, that's amazing!
I've changed the step value to -4 to make it faster
and the matching quote for the dark pictures is ~99% :D

Re: Get the overall brightness level of a picture?

Posted: Tue May 07, 2019 7:41 am
by Michael Vogel
I've used a CustomFilter from time to time in similar cases to speed up things...
...but keep in mind to use a global variable for summarizing the pixel info within the callback routine.

Code: Select all

UseJPEGImageDecoder()
UseJPEG2000ImageDecoder()
UsePNGImageDecoder()
UseTIFFImageDecoder()
UseTGAImageDecoder()

Define   x, y, countOfAllfPixels, pixelColor
Define   imageFilePath$

Global TotalImageBrightness.d

Procedure Brightness(x,y,c,d)

	x=c&$FF
	y=(c>>8)&$FF
	c=(c>>16)&$FF
	TotalImageBrightness+Sqr(x*x*0.299+y*y*0.587+c*c*0.114)

EndProcedure

imageFilePath$=	"IQ.png"
If FileSize(imageFilePath$)<0
	imageFilePath$=	OpenFileRequester("Open image file", "", "", 0)
EndIf

If FileSize(imageFilePath$)>0

	image = LoadImage(#PB_Any, imageFilePath$)
	If image>=0
		countOfAllfPixels = ImageWidth(image) * ImageHeight(image)
		TotalImageBrightness=0
		StartDrawing(ImageOutput(image))
		CustomFilterCallback(@Brightness())
		DrawingMode(#PB_2DDrawing_CustomFilter)
		DrawImage(ImageID(image),0,0)
		StopDrawing()
		FreeImage(image)

		Debug totalImageBrightness / countOfAllfPixels
	EndIf
EndIf

Re: Get the overall brightness level of a picture?

Posted: Sat Nov 19, 2022 9:41 pm
by novablue
I would like do dim down every picture to the same rough brightness level to use it as a background.

Could someone smart modify this code to also SET the overall brightness of a picture? :lol: