Search code examples
rendertransparencypixel

How to change the transparency of an image pixel by pixel


What I want to do is adjust the transparency of every pixel in a picture according to its ink ratio. I will take an example: A node-link graph

In this picture, I want to calculate every pixel's ink ratio(That is, the ratio of the part covered by the line in each pixel to the size of the entire pixel). Then assign a transparency value to each pixel according to this ratio, the higher the ratio is, the lower the transparency. Finally,rendering the picture.

But now I don't know how to achieve it. Can someone help me or give me some advice to solve it? I will be very grateful!


Solution

  • I have very limited experience with any kind of image processing so these ideas may be far too simplistic to help. In the simplest case if you are working on some kind of raw data AND with a single colour background (e.g. black line on white background) then any pixel values diverging from the background pixel value represent one or more lines passing through the pixel.

    If this is not the case I think it's down to lots of calculations. I suggest that first you have to calculate which pixels are covered in part or whole by one or more lines which depends on the line width and direction. I assume that the length and direction of each line is known so you can calculate one or more lines that represent the boundaries of the line and use those to give the affected pixels.

    Now it's necessary to calculate the proportion of the pixel that is covered by the total line i.e. the part of the pixel that lies between the calculated line boundaries. At this point I leave it to you - but I visualise a pixel as a square of known position and dimensions with a boundary line passing through it at known points to cut off a triangle or, for a horizontal or vertical line, a rectangle either within the line boundary or outside it.

    Again - this assumes that the line width is greater than the diagonal of the pixel - if that is not so the calculation has to be done for both boundary lines.

    That deals with one line passing through the pixel but of course multiple lines may have to be considered and the resultant value calculated.

    If this is too simplistic and I have not understood the problem correctly I apologise.