(Note that you need to use np.uint8 as datatype for your image, since binary images obviously cannot represent different colors. Img_rgba = img.astype(np.uint8) * palette_colors # Fill R, G and B with appropriate colors Img_rgba = np.zeros((img.shape, img.shape, 4), dtype=np.uint8) Once you have palette_colors, you can pretty much use the code you already have to save the image, except you now add the different RGB values instead of copies of ~img to your img_rgba array. (Note that the axis argument for np.unique was added in numpy version 1.13.0, so you may need to upgrade numpy for this to work.) Coolors is an intuitive and robust color palette generator that allows you to explore palettes based on keywords, generate your own color palette from an uploaded image, see what your selections look like for people who are colorblind, generate gradients and more. Palette_colors = np.unique(palette, axis=0) Palette = palette.reshape(palette.shape*palette.shape, palette.shape) # Use `np.unique` following a reshape to get the RGB values Palette = io.imread(os.path.join(os.getcwd(), 'color_palette.png')) You can use a combination of a reshape and np.unique to extract the unique RGB values from your color palette image: # Load the color palette # For alpha just use the image again (makes background transparent) # Note: This creates a black object instead of this, I need the colors from the palette. # Fill R, G and B with inverted copies of the image Img_rgba = np.zeros((img.shape, img.shape, 4), dtype=np.bool) What I am struggling with is a good way of extracting the RGB colors so I can apply them to the image. My code so far (see below) can save the img as a black object with transparent background. (The real images are more complicated of course.)Įxtract all RGB colors from the color palette image.įor each color, save a copy of img in that color with a transparent background. I have a color palette image like this one and a binarized image in a numpy array, for example a square such as this: img = np.zeros((100,100), dtype=np.bool)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |