Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7784417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:05:30+00:00 2026-06-01T20:05:30+00:00

Given an image (.tiff or geotiff file) with exactly 22 colors (each with a

  • 0

Given an image (.tiff or geotiff file) with exactly 22 colors (each with a distinct RGB value), what are the approaches to separate (“filter”) them out into 22 separate images, each containing only those pixels with a specific RGB value?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:05:32+00:00Added an answer on June 1, 2026 at 8:05 pm

    Here is a pixelwise way of doing it, which can work for any number of colors in the image (although it can get slow for many colours and large images). It also will work for paletted images (it converts them).

    import Image
    
    def color_separator(im):
        if im.getpalette():
            im = im.convert('RGB')
    
        colors = im.getcolors()
        width, height = im.size
        colors_dict = dict((val[1],Image.new('RGB', (width, height), (0,0,0))) 
                            for val in colors)
        pix = im.load()    
        for i in xrange(width):
            for j in xrange(height):
                colors_dict[pix[i,j]].putpixel((i,j), pix[i,j])
        return colors_dict
    
    im = Image.open("colorwheel.tiff")
    colors_dict = color_separator(im)
    #show the images:
    colors_dict.popitem()[1].show()
    colors_dict.popitem()[1].show()
    
    1. Calling im.getcolors() returns a list of all the colors in the image and the number of times they occur, as a tuple, unless the number of colors exceeds a max value (which you can specify, and defaults to 256).
    2. We then build a dictionary colors_dict, keyed by the colors in the image, and with corresponding values of empty images.
    3. We then iterate over the image for all pixels, updating the appropriate dictionary entry for each pixel. Doing it like this means we only need to read through the image once. We use load() to make pixel access faster as we read through the image.
    4. color_separator() returns a dictionary of images, keyed by every unique colour in the image.

    To make it faster you could use load() for every image in colors_dict, but you may need to be a little careful as it could consume a lot of memory if the images have many colours and are large. If this isn’t an issue then add (after the creation of colors_dict):

    fast_colors = dict((key, value.load()) for key, value in colors_dict.items())
    

    and swap:

    colors_dict[pix[j,i]].putpixel((j,i), pix[j,i])
    

    for:

    fast_colors[pix[j,i]][j,i] = pix[j,i]
    

    22 color image: enter image description here

    22 color isolated images:

    enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here
    enter image description here enter image description here enter image description here enter image description here enter image description here
    enter image description here enter image description here enter image description here enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a SSRS report definition file with an embedded image in it, just wondering
Is there a way to convert a HTML string into a Image .tiff file?
I need to upload a given image using Amazon S3 I have this PHP:
I have grid of images, when the mouse is over any given image a
Question: Given an image in PNG format, what is the simplest way to programmatically
Is there some reasonably cross platform way to create a thumbnail image given a
Given a bitmap image with some blots of solid color on it, what algorithm
Does anyone know how I can create a Completely transparent image or given am
Given a url to an image is there a way in Django/Python to pull
Given a URL to an image (and not the image itself), what's the most

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.