Apologies for inaccurate terminology or problem description. I am still fairly new to Python. For an eye-tracking experiment, I need to convert a set of stimuli (flower images) on grayscale and make them equiluminant. I also need to keep the alpha channel of the images, although this does seem to be fairly easy.
For the equiluminance, I currently have 3 steps:
Calculate the mean brightness of each image, using the alpha channel as a mask:
Compute the difference between the mean brightness of the current image and the mean across all images
Add this difference to each each pixel of the current image, and round to an integer.
This is my solution for this problem:
# For simplicity, I set this to a constant for now. Represents mean brightness across all imagesgrandMean = 138 for pic in myFiles: picPath = myPath +'/'+ pic img = Image.open(picPath).convert('LA') alpha, grayScale = img.split() # Will use alpha as a mask to compute the mean brightness imgStat = ImageStat.Stat(img, mask=alpha) meanBrightness = imgStat.mean[0] # Returns mean level of L-channel # Compute difference between grandMean and mean of the current img meanDiff = abs(grandMean - meanBrightness) # Now add meanDiff to each pixel in L-channel grayPx = grayScale.load() size = grayScale.size for row in range(0,size[0]): for col in range(0,size[1]): grayPx[row, col] = round(grayPx[row, col] + meanDiff) imFinal = Image.merge('LA', (grayScale, alpha))
As I said, while this solution does work, it is incredibly inefficient because it has to iterate over each pixel of every image. Is there any simpler way to do this?