Mathias_466
Posts: 1
Joined: Mon Jan 08, 2018 5:29 pm

Color in a picture

Mon Jan 08, 2018 5:33 pm

If I have 2 colors for example (0,0,0) and (255,255,255):

Can I make an average of all colors in an image?

Can I only acces the red part of the colors and make an average of them?

Or can I make an average of the red color in an picture in an other way?

gordon77
Posts: 3479
Joined: Sun Aug 05, 2012 3:12 pm

Re: Color in a picture

Mon Jan 08, 2018 7:17 pm

You could convert to a numpy array, and then use numpy.average, filter etc

User avatar
DougieLawson
Posts: 34166
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Color in a picture

Mon Jan 08, 2018 8:06 pm

Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1662
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: Color in a picture

Tue Jan 09, 2018 11:23 am

Something like this would do it;

Code: Select all

from PIL import Image
import numpy

file_name = "dave.jpg"

img = Image.open(file_name)

chan_red, chan_green, chan_blue = img.split()[0:3]
arr_red = numpy.asarray(chan_red).astype('float32')

print(numpy.mean(arr_red))

Return to “Astro Pi”