What is a good algorithm for getting color to output text to an image?

For example, under the image, the photograph is the background, and in the center of the photograph is the character Iniesta. But the character is hard to read, because the color is not very good. Is there a good algorithm for getting color to create a character in an image?

enter image description here

+4
source share
3 answers

Instead of creating a rectangular background (which really doesn't look very good) you can use this trick:

  • Choose two contrasting colors (e.g. white and black)
  • Circle the text using the first color and a wide pen (for example, 5 pixels).
  • Draw text filled with a second color

, ; (Python)

for dy in range(-3, 4):
    for dx in range(-3, 4):
        draw_text(color1, x+dx, y+dy, message)
draw_text(color2, x, y, message)

, ... enter image description here

+2

, . , . :

    • ,
  • < > Stroke, , 3D

    • 2
    • ,
    • / .
    • +/- ,
    • , ,
    • - .
  • XOR

    • XOR

:

  • example

VCL/++:

void TMain::draw()
    {
    if (!_redraw) return;

    // needed variables
    AnsiString txt,a;
    TColor c0=clBlack,c1=clWhite;
    int tx=50,ty=50,tys=30,dty=tys*1.5;
    int x,y,x0,x1,y0,y1,i,b;

    // clear whole image
    bmp->Canvas->Brush->Color=clBlack;
    bmp->Canvas->FillRect(TRect(0,0,xs,ys));
    // copy photo
    bmp->Canvas->Draw(0,0,in);

    // render texts ...
    txt="Paper area ";
    bmp->Canvas->Font ->Size=tys;
    bmp->Canvas->Font ->Color=c1;
    bmp->Canvas->Brush->Color=c0;
    bmp->Canvas->Brush->Style=bsSolid;
    bmp->Canvas->TextOutA(tx,ty,txt); ty+=dty;

    txt="Stroke";
    bmp->Canvas->Brush->Style=bsClear;
    for (x=tx,y=ty,i=1;i<=txt.Length();i++)
        {
        a=txt[i];
        // stroked char
        bmp->Canvas->Font ->Size=tys+3;
        bmp->Canvas->Font ->Color=c0;
        bmp->Canvas->Font ->Style=TFontStyles()<<fsBold;
        x0=bmp->Canvas->TextWidth(a);
        y0=bmp->Canvas->TextHeight(a);
        bmp->Canvas->TextOutA(x,y,a);
        // inside char
        bmp->Canvas->Font ->Size=tys;
        bmp->Canvas->Font ->Color=c1;
        bmp->Canvas->Font ->Style=TFontStyles();
        x1=bmp->Canvas->TextWidth(a);
        y1=bmp->Canvas->TextHeight(a);
        bmp->Canvas->TextOutA(x+((x0-x1)>>1),y+((y0-y1)>>1),a);
        // next char position
        x+=x0;
        } ty+=dty;

    txt="Shadow/3D text";
    bmp->Canvas->Brush->Style=bsClear;
    bmp->Canvas->Font ->Size=tys;
    bmp->Canvas->Font ->Color=c0;
    for (i=0;i<5;i++)
     bmp->Canvas->TextOutA(tx-i,ty+i,txt);
    bmp->Canvas->Font ->Color=c1;
    bmp->Canvas->TextOutA(tx,ty,txt); ty+=dty;

    Graphics::TBitmap *tmp=new Graphics::TBitmap;
    tmp->PixelFormat=pf32bit;
    tmp->HandleType=bmDIB;

    txt="Transparent";
    tmp->Canvas->Brush->Style=bsSolid;
    tmp->Canvas->Brush->Color=0x00000000; // max black
    tmp->Canvas->Font ->Size=tys;
    tmp->Canvas->Font ->Color=0x00808080; // color offset
    x=bmp->Canvas->TextWidth(txt);
    y=bmp->Canvas->TextHeight(txt);
    tmp->SetSize(x,y);
    tmp->Canvas->FillRect(TRect(0,0,x,y));
    tmp->Canvas->TextOutA(0,0,txt);
    union col { DWORD dd; BYTE db[4]; } *p0,*p1;
    for (y0=0,y1=ty;y0<y;y0++,y1++)
        {
        p0=(col*)tmp->ScanLine[y0];
        p1=(col*)bmp->ScanLine[y1];
        for (x0=0,x1=tx;x0<x;x0++,x1++)
         for (i=0;i<4;i++)
            {
            b=WORD(p0[x0].db[i])+WORD(p1[x1].db[i]);
            if (b>255) b=255;
            p1[x1].db[i]=b;
            }
        } ty+=dty;

    txt="XOR";
    tmp->Canvas->Brush->Style=bsSolid;
    tmp->Canvas->Brush->Color=0x00000000; // max black
    tmp->Canvas->Font ->Size=tys;
    tmp->Canvas->Font ->Color=0x00FFFFFF; // max white
    x0=bmp->Canvas->TextWidth(txt);
    y0=bmp->Canvas->TextHeight(txt);
    tmp->SetSize(x0,y0);
    tmp->Canvas->FillRect(TRect(0,0,x0,y0));
    tmp->Canvas->TextOutA(0,0,txt);
    bmp->Canvas->CopyMode=cmSrcInvert;
    bmp->Canvas->Draw(tx,ty,tmp); ty+=dty;
    bmp->Canvas->CopyMode=cmSrcCopy;

    delete tmp;

    // render backbuffer
    Main->Canvas->Draw(0,0,bmp);
    _redraw=false;
    }
  • VCL GDI, ...
  • bmp -
  • in - .
  • tmp - .
+1

: , . .

, . , (255-r,255-g,255-b), . , 128-, .

- HSL HSV, ; , 180 / "" (/). , (, 0).

+1
source

All Articles