How to add/run a script that changes the colors of the final image shown with F12?

How to add and run a script that changes the colors of the final image shown with F12?

What I want, is that I want to see, how a rendered image (F12) would look like, if I change the color palette from default (32 bit colors?), to a 8 bit indexed color palette, used by older games like Fallout.

I could probably code the mathematical part, but I could not find out, how to make and run such a script. A simple example that would let’s say replace every color on an image (not texture, not material, but final image) to black if the alpha or one of the colors value is less than a limit, would help me a lot, I could work from there.

Doesn’t matter if phyton script or open shader language or whatever, I just want it to be able to run in Blender and not externally on a saved image, for convenience ofc.

Note: again, I do not want to change texture colors, but the final image presented with F12.

Easiest way to solve this would probably be to create a node for the compositor. It works in the same way as cycles node editor but you work with an image (your render). So you could just separate each color channel and do the calculation using math nodes and then merge it back to an image.

Then just insert your node to any render you want. Super simple!

Compositor is in: Node Editor ->Bottom bar there is a row of three image buttons -> click the middle one: ‘Compositing’ -> Also check the checkbox ‘Use Nodes’ allocated right of the image buttons!

You’ll then have access to the raw render image for all render layers as nodes!
Note that the method will not change the format of the image, but it will work directly in your viewport.

I have searched the internet for some time about this problem, I have found the compositior, and I have been trying create a node there, a new one, that uses a script. I have found no such option yet using Shift+A (While, if I am at Shader, I can add Script with Shift+A)

So, my question is, how do I run a script for compositor, or how do I make a new type of node for compositor that runs my script. Basically I need a custom node, that has the input of a color and output of a color, but how do I add such a node?

Is it even possible?

Using the default nodes made compositor, will not suit my needs, as the script might be too complex, with loops, etc.

No, sadly is not possible to use some kind of scripts in the compositor, it is a very different system than OSL with Cycles.
If you have the script you could use the post render handler to plug it in there to work on the image, either calling a external application or using PIL if you want to use python I guess.
www.blender.org/api/blender_python_api_2_77_1/bpy.app.handlers.html

However many many things can be done in compositor without loops so be sure you that actually need a loop.

@Linusy Ty, I came to the same conclusion searching for answers, that what I wanted cannot be done like that. I found a workaround for my problem, but will investigate your direction with callbacks for a better solution.

@Osares: Please elaborate on how to make a node for the compositor or remove your answer to not confuse others. (To me it seems that you didn’t try what you suggested before posting it, living up to your signature.)

Workaround:

Meanwhile I found a workaround for my problem here: https://ammous88.wordpress.com/2015/01/16/blender-access-render-results-pixels-directly-from-python-2/
With this, I cannot see the effects on F12 screen, but it suggest to add a viewer node using the compositor, which can be connected as background and there I can see the effects I make on the viewer pixel data, which can be accessed like this:

pixels = bpy.data.images[‘Viewer Node’].pixels

The rendered image (F12) can be accessed (bpy.data.images[‘Render Result’].pixels), but it’s bugged, or not supported and all values are 0. Basically it seems only an empty object appears instead of an image.

So what I do now, is run a script either after each render, or call the rendering from script, and on the node editor, composition, I can see in the background the changes the script does, which is a half solution for me.

Will report back if I found a better one.

I don’t know the exact calculation for how to convert 32Bit color to 8Bit and then back but to show the example you asked for where each pixel with alpha < value is turned to black i made this image:


I also highlighted the basic math operations that is available, i think they should be enough. Also note that if you only want to make each pixel black you dont need so separate the channels just use the color mix node.

Note* The yellow dots are combination of 4 values (rgba) in range 0-1 (usually), and gray are a single float value. Nodes is also constructed with left as input and right as output.

This is pretty much writing a script for each pixel (pixel shader? :slight_smile: ) with nodes instead of text :slight_smile: Don’t know if you can access adjacent pixels but i think you are only applying a calculation to each pixel separately and then this should definately be enough.
You can also group a set of nodes with Ctrl+G (that was the reference for creating a node in my first comment :P) and duplicate it if you want to repeat a equation over each channel etc.

Make sure you have a rendered image and a viewer window open and it should all update when editing.