0

How can I go about retrieving a total luminance value of a .png image, better yet - a way to do this for every image contained within a folder?

I've come across two resources, however, for one (https://rdrr.io/github/mokazuma/imhistR/man/lrgbhist.html#heading-1) I'm struggling to even download the package and the other (Formula to determine perceived brightness of RGB color) seems to lack consensus on how luminance should be calculated?

Any help would be greatly appreciated, thank you!

zx8754
  • 52,746
  • 12
  • 114
  • 209
PBJB
  • 5
  • 1

1 Answers1

1

Here's how you can install the imhistR package.

  1. Install the remotes package.
install.packages('remotes')
  1. Install the datautils package:
remotes::install_github("holgerbrandl/datautils", dependencies=TRUE)
  1. Install the (removed from CRAN) patchPlot package:
remotes::install_url('https://cran.r-project.org/src/contrib/Archive/patchPlot/patchPlot_0.1.5.tar.gz', dependencies = TRUE)
  1. Install the (removed from CRAN) imhistR package:
remotes::install_github("mokazuma/imhistR", dependencies=TRUE)
  1. Test the imhistR package:
require("imhistR")
lrgbhist(input, mode = "file", output = input, hist = TRUE,
  resize = FALSE, endoff = FALSE, textsize = 16)

On my system, this yields:

trying URL 'https://www.r-project.org/Rlogo.png'
Content type 'image/png' length 32553 bytes (31 KB)
downloaded 31 KB

          Mean_Luminance SD_Luminance Skew_Luminance
Rlogo.png      0.5307022    0.1816393      0.1654525
          Kurt_Luminance Mean_Red    SD_Red   Skew_Red
Rlogo.png      -1.697913  0.42456 0.2849509 0.01362389
           Kurt_Red Mean_Green  SD_Green Skew_Green
Rlogo.png -1.875264   0.552168 0.1613232  0.2315529
          Kurt_Green Mean_Blue    SD_Blue Skew_Blue
Rlogo.png  -1.657691 0.7183205 0.05884395 -1.220589
          Kurt_Blue
Rlogo.png  2.128444
Warning messages:
1: The dot-dot notation (`..x..`) was deprecated in ggplot2
3.4.0.
ℹ Please use `after_stat(x)` instead.
ℹ The deprecated feature was likely used in the imhistR
  package.
  Please report the issue at
  <https://github.com/mokazuma/imhistR/issues>.
This warning is displayed once every 8 hours.
Call `lifecycle::last_lifecycle_warnings()` to see where
this warning was generated. 
2: Removed 2 rows containing missing values (`geom_bar()`). 
3: Removed 2 rows containing missing values (`geom_bar()`). 
4: Removed 2 rows containing missing values (`geom_bar()`). 
5: Removed 2 rows containing missing values (`geom_bar()`). 

and the plot:

Output from imhistR::lrgbhist()

As you can see, the package needs some TLC, but it seems to work for now.

Edit:

Looking at the package code (https://github.com/mokazuma/imhistR/blob/13319682f0ca0750f9c7e36b5c98c4a68b2e632b/R/lrgbhist.R#L122), the important bits seem to be these:

fn = "FILE_NAME_HERE.png"
img <- readbitmap::read.bitmap(fn)
red <- img[,,1]; green <- img[,,2]; blue  <- img[,,3]
luminance <- .298912 * red + .586611 * green + .114478 * blue
mean(luminance)

If you install the readbitmap package and if you're happy with the default settings in the imhistR::lrgbhist() function, this code will give you what you want.

richarddmorey
  • 976
  • 6
  • 19
  • Thanks for the installation help, it seems to of worked. I tried the following: lrgbhist(input = "C:\\Users\\phoen\\OneDrive\\PhD\\MAGMOT\\Split_stimuli\\01_H35_combined_small\\image_000001.png", mode = "file", output = input, hist = TRUE, resize = FALSE, endoff = FALSE, textsize = 16) but I get this error: !! Error has occurred. Please change some parameters object 'input' not found. Any ideas on how to rectify this? – PBJB Jun 29 '23 at 15:28
  • I noticed that in my code above, I didn't define "input" (I left it to you). Can you try `input="C:\\Users\\phoen\\OneDrive\\PhD\\MAGMOT\\Split_stimuli\\01_H35_combined_small\\image_000001.png"` before running my code above? – richarddmorey Jun 29 '23 at 21:02
  • See my edit in the answer at the bottom. I took the important code out of the package, so you don't actually need the package itself. That might make your life easier. – richarddmorey Jun 29 '23 at 21:16
  • Hi Richard, sorry for another comment. Both your suggestions worked and I ended up opting for the first strategy as I wanted to assign the function to an entire folder not just a file. I got it to work by assigning my file path to the object 'input' but after 10 folders it no longer works. I get the following error: !! Error has occurred. Please change some parameters invalid regular expression 'C:\Users\phoen\OneDrive - University of Reading\PhD\MAGMOT\Split_stimuli\36_Trick7_short_combined_small/', reason 'Invalid back reference' . – PBJB Jul 05 '23 at 16:01
  • An example of my code that errored: input <- "C:\\Users\\phoen\\OneDrive - University of Reading\\PhD\\MAGMOT\\Split_stimuli\\10_S27_combined_small" Lum10 <- lrgbhist(input, mode = "folder", output = input, hist = FALSE, resize = FALSE, endoff = FALSE, textsize = 16) – PBJB Jul 05 '23 at 16:01
  • And here's an example of one that did work: input <- "C:\\Users\\phoen\\OneDrive - University of Reading\\PhD\\MAGMOT\\Split_stimuli\\08_S12_combined_small" Lum8 <- lrgbhist(input, mode = "folder", output = input, hist = FALSE, resize = FALSE, endoff = FALSE, textsize = 16) – PBJB Jul 05 '23 at 16:03
  • To work out why that's not working would require knowledge of your file system and the imhistr package, which I don't have. But if you make a simple function out of the second approach, say, getHist = function(fn){ img <- readbitmap::read.bitmap(fn); red <- img[,,1]; green <- img[,,2]; blue <- img[,,3]; luminance <- .298912 * red + .586611 * green + .114478 * blue; return(mean(luminance)); } ...then you can do a whole folder with something like dir(FOLDERNAME) |> sapply(getHist). If you need help with this you can open a new question. – richarddmorey Jul 06 '23 at 07:44