Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add red or purple color text on the image? #537

Closed
chancein007 opened this issue Aug 9, 2018 · 10 comments
Closed

How to add red or purple color text on the image? #537

chancein007 opened this issue Aug 9, 2018 · 10 comments

Comments

@chancein007
Copy link

var Jimp = require('jimp');
Jimp.read('test.jpg', (err, image) => {
if (err) throw err;
Jimp.loadFont(Jimp.FONT_SANS_32_BLACK).then(function(font) {
image.print(font, 10, 10, 'Hello world!');
image.write('lena-small-bw.jpg');
});
});

I can use the above source to write 'Hello world!' on the test.jpg picture, however, it seems that Jimp only can write white or black text on the image rather than other color: for example, red or purple. How to add red or purple color text on the image?

Any help is much appreciated!!!

@hipstersmoothie
Copy link
Collaborator

hipstersmoothie commented Aug 9, 2018

I think you have to find a bitmap font that is already colored. Printing seems to work by having a bitmap font file that maps characters to a special png with all the characters on it already. You might have to play around with a few tools to get your fonts to have color

some examples after a little googling

https://www.colorfonts.wtf/
https://learn.fontself.com/create-your-first-color-font-step-by-step-270ced3f0745

You could try using the tool mentioned in the readme http://kvazars.com/littera/ to create a color font.

A bunch of other tools are also here https://github.com/libgdx/libgdx/wiki/Hiero

hipstersmoothie added a commit to hipstersmoothie/jimp that referenced this issue Aug 10, 2018
@chancein007
Copy link
Author

@hipstersmoothie Many thanks

@Adron55
Copy link

Adron55 commented Jul 31, 2019

U can use Hiero for change color and size

@LukaszWiktor
Copy link

LukaszWiktor commented Sep 21, 2019

You don't have to create a color font.
Use black font on a transparent background and XOR if with your desired color:

 textImage.color([{ apply: 'xor', params: ['#ff0000'] }]); // red text

If you need a non-transparent background then create one as a separate image and blit it with the text image:

backgroundImage.blit(textImage, x, y);

@maitrungduc1410
Copy link

You don't have to create a color font.
Use black font on a transparent background and XOR if with your desired color:

 textImage.color([{ apply: 'xor', params: ['#ff0000'] }]); // red text

If you need a non-transparent background then create one as a separate image and blit it with the text image:

backgroundImage.blit(textImage, x, y);

Works perfectly. Thank you

@abdel78900
Copy link

You don't have to create a color font.
Use black font on a transparent background and XOR if with your desired color:

 textImage.color([{ apply: 'xor', params: ['#ff0000'] }]); // red text

If you need a non-transparent background then create one as a separate image and blit it with the text image:

backgroundImage.blit(textImage, x, y);

Works perfectly. Thank you

Hello

can you tell me where to insert this line of code.
thank you

@ghost
Copy link

ghost commented May 10, 2022

Hello @LukaszWiktor @maitrungduc1410, could you explain more where to insert this?

import jimp from 'jimp'

async function main() {
  const font = await jimp.loadFont(jimp.FONT_SANS_32_BLACK)

  const image = await jimp.read('test1.jpg')

  image.print(font, 0, 0,   {
    text: 'Hello, Dexter Morgan!',
    alignmentX: jimp.HORIZONTAL_ALIGN_CENTER,
    alignmentY: jimp.VERTICAL_ALIGN_MIDDLE
  },
    image.getWidth(),
    image.getHeight()
  )

  image.write('test2.jpg')
}

main()

@codesport
Copy link

codesport commented Jul 15, 2022

@abdel78900 @gugamacedo

This stackOverflow answer has this example:

Jimp.read('testImage.jpg', (err, baseImage) => {
    if (err) throw err;

    let textImage = new Jimp(1000,1000, 0x0, (err, textImage) => {  
        //((0x0 = 0 = rgba(0, 0, 0, 0)) = transparent)
        if (err) throw err;
    })

    Jimp.loadFont(Jimp.FONT_SANS_32_BLACK).then(font => {
        textImage.print(font, 0, 0, "My Text")
        textImage.color([{ apply: 'xor', params: [#00ff00] }]); 
        image.blit(textImage, 0, 0)
        image.write('testOutput.jpg'); // save
    });
});

@YMC-GitHub
Copy link

@abdel78900 @maitrungduc1410 @LukaszWiktor
Works perfectly. Thank you

@hipstersmoothie
Copy link
Collaborator

If any changes to the docs are needed and wants to submit a PR to fix I'm able to review and merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants