FME Version
Files
Introduction
A palette represents each of the pixels in a raster and lists each of the RGB colors associated with each pixel. FME has several transformers that can be used to manipulate whole palettes such as RasterPaletteInterpretationCoercer and RasterPaletteGenerator. However, sometimes there can be a need for accessing and editing each pixel. FME has two transformers that enable the extraction of palettes and the ability to add palettes to/from an attribute - RasterPaletteExtractor and RasterPaletteAdder.
Scenario
Let's take a simple raster with several distinct colors:
1. Open the extractPalette.fmwt workspace
Run the extractPallete.fmwt template workspace which will take the initial raster (pictured above) and extract the palette using the RasterPaletteExtractor transformer.
2. View _palette
In the FME Data Inspector, which should open after the workspace is run, highlight the entire raster to view the attributes. In the Feature Information Window, double-click on the attribute value for the _palette attribute, this will open up the palette information.
3. Understanding the palette
The first line is the interpretation of the raster palette, in this case, it's RGBA32. All other lines start with a key value followed by the color expressed according to the interpretation, that is, the first three numbers define red, green, and blue, and the fourth number controls alpha values and sets transparency.
Now imagine we would like to replace the pure blue color 0,0,255 (located in the second row from the bottom, third column) with pure yellow, which is 255,255,0. In our palette, it is the key number 4.
To determine which key is associated with which square, in the FME Data Inspector, click on the square of interest and then in the Feature Information Window, the key will be identified by Band 0
Step-by-step Instructions
Now that the key has been identified in the palette, there are two ways we can manipulate it. First, the attribute can be written to a text file, change the attribute manually in the text file, and then apply a new version of the palette to the raster (Part 1). Alternatively, this can all be done in a single workspace (Part 2). Depending on the use case, either method can be used.
Part 1: Editing Palette in a Text Editor
This scenario requires two workspaces. The first workspace extracts the palette. Then the palette is edited manually in a text editor and then saved. Then the second workspace is used to read in the new palette and update the raster.
1. Open the unloadPalette.fmwt workspace (workspace 1)
In FME Workbench open up the unloadPalette.fmwt workspace. In this workspace, a raster is read in (the one pictured above). A RasterPaletteExtractor transformer is then used to extract the palette and store it as an attribute. Using the AttributeFileWriter transformer, that attribute (_palette) is written out to disk.
2. Run the workspace
When running the workspace, a dialog will appear asking for a save location for the palette attribute. Ensure this location is easy to find as the file will be opened in the next step.
3. Open palette.txt
Find the save location that was set in step 2 and then open palette.txt in a text editor. An advanced text editor is recommended, but any text editor will do.
4. Change the palette
In an advanced text editor, locate line 6. Then change the line from 4 0,0,255,255 to 4 255,255,0,255. This will change the color from blue to yellow. Save the file.
5. Open the loadPalette.fmwt workspace (workspace 2)
The last step is to apply our modified palette to the source raster. Open the loadPalette.fmwt template workspace and then run the workspace. When the workspace is run, another dialog will appear, select the save location of palette.txt.
This workspace reads in the original raster and also reads in the new palette using the AttributeFileReader transformer. Then using the RasterPaletteRemover transformer, the old palette is removed, and the new one is added using the RasterPaletteAdder transformer.
6. View the new raster
After running the workspace, FME Data Inspector will open, and the new raster can be viewed.
Part 2: Changing a Palette with Workbench
The previous solution does not look very nice. Changes have to be made to two workspaces or make changes to a single workspace between steps. There is a way this can be done using only one workspace.
1. Open the manipulatePalette.fmwt workspace
Open the manipulatePalette.fmwt template workspace. This workspace reads in the initial raster and then extracts the palette and stores it as an attribute. Then using the AttributeSplitter transformer line of the palette is split into a list element. An AttributeCreator transformer is used to assign _list{5} which is key 4, the yellow RGB value 4 255,255,0,255. Then a ListConcatenator transformer is used to rebuild the palette. Finally, just like Part 1, the old palette is removed with the RasterPaletteRemover, and the new palette is added with the RasterPaletteAdder.
2. Run the workspace
Run the workspace and view the new raster in the FME Data Inspector. The output should be the same as Part 1.
Conclusion
Which way is better depends on your needs in palette editing. Complex, conditional changes to big palettes may be quite tricky within workbench using the visual aid of a text editor might be a better option than a long chain of transformers. On the other hand, regular production tasks may be worth placing all the modification into a single process.
Comments
0 comments
Please sign in to leave a comment.