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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
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).
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).colors_dict, keyed by the colors in the image, and with corresponding values of empty images.load()to make pixel access faster as we read through the image.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 incolors_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 ofcolors_dict):and swap:
for:
22 color image:
22 color isolated images: