Contrasting text over random image

Hello, I have a script that puts a text into an image. I want the text to be universaly readable in front of any input image (natural image, not random noise). Is there some way to calculate the most contrasting color from the background color average or something? My only though was making it negative but it looks really weird and in some cases it doesn’t help at all.

Please show us some examples.

There are ways to automatically calculate the best contrasting color but in the end the outcome always is just for the whole background and wouldn’t work an every spot.

If you can’t control it manually you have to control the background automatically (shadow, blur, box, etc)

Algorithms to analyze contrast and color differences between one object and another are, of course, possible. The same is true of using that information to add the necessary adjustments in those areas where it’s needed. Getting around Joe’s concern about the adjustments being the averaged results of the whole background would require localization of the averaging and subsequent adjustments within specified pixel distances of where the component objects lie, which is also doable but more complicated.

However, I know of no off-the-shelf software that does this.

The results would often be unattractive even if software like Photoshop plugins existed. For example, if the left half of the image was dark and the right half was light, the only solution I can think of would be to place a light or dark halo around the type. The same would be true of a salt & pepper background that contained multiple colors. Most of the time, the solution would likely be little more than lightening or darkening a halo of pixels around each glyph —in other words, adding the equivalent of a drop shadow.

Another possibility is surrounding the entire text box with a translucent dark area (a black or white box with adjusted transparency). Manually performing this task in commercial image editors would be easy enough. However, finding or writing software to analyze the photo and determine the ideal degree of opacity to apply while automatically creating the box with no human interaction is a different problem. Again, it’s doable, but I know of no existing software for it.

Even using the best possible software solution, the results will be aesthetically lacking much of the time and will, as you put it, “look weird.” I suppose an AI solution could eventually figure out what solutions were aesthetically unacceptable and make the appropriate judgment-call adjustments, but that’s another level of difficulty.

That was a whole lot of words to say that I know of no existing fully automated solutions.

It sounds like you need a filter like ‘Screen’ or ‘Difference’. These change the colour of the layer in response to the layer underneath. Use with caution, however, with text and avoid with small text.

Failing that a drop Shadow / Outer Glow on a layer in between that uses Screen or Difference, but that was not your question.

My preference would be a box underneath the text that tints or shades the layer below, but again that was not your question.