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?

Posts: 4235
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
Posts: 36322
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Color in a picture

Mon Jan 08, 2018 8:06 pm

Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
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 =

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


Return to “Astro Pi”