question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generating text over transparent background

See original GitHub issue

It looks like one of the most basic tasks and yet it seems quite hard to do at least without reading through all of the documentation and looking at only the related methods and classes.

def output(filename, text, color):
    image, feedback = pyvips.Image.text(
        text, font='Liberation-Sans-Narrow', width=32, height=32,
        autofit_dpi=True)
    image = image.colourspace('srgb')
    image = image.addalpha()
    image = image.ifthenelse([0, 0, 0, 255], [255, 255, 255, 0], blend=True)
    image.write_to_file(f'{DIR}/{filename}.png')

I guess I’m not working correctly with ifthenelse. But what are the right values to make the image transparent? Where is the alpha channel supposed to be?

Also, the resulting image is not 32x32, it may be good to add hints on how to make it the required size somewhere around here.

Additional context #203 https://github.com/libvips/libvips/issues/1208

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
jcupittcommented, Aug 25, 2020

a.embed(x, y, width, height) will position a at (x, y) within an image of size (width, height).

1reaction
jcupittcommented, Aug 24, 2020

Hello again, you could make this a little simpler: use the text for the alpha and have a solid colour in RGB.

For example:

alpha, feedback = pyvips.Image.text('Hello World!', 
        font='Liberation-Sans-Narrow', width=32, height=32,
        autofit_dpi=True)
alpha = alpha.gravity('centre', 32, 32)
image = alpha.new_from_image([0, 0, 255]).bandjoin(alpha)

The gravity will expand the alpha up to 32x32 if it’s smaller.

text uses 0 - 255 for black - white. If you want to make the text semi-transparent, you’ll need to set a smaller range, perhaps 0 - 64? You could write:

image = alpha.new_from_image([0, 0, 255]).bandjoin(alpha * 0.25)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Transparent Text Generator - Create transparent PNG images ...
Free Online Transparent Text Editor. Design high quality transparent texts as PNG images.
Read more >
Simple Text Logo Generator
Simple Text Logo Generator. Blue Shadow Plain Gradient. Move and rotate elements by dragging them. Resize by dragging this corner. Rendered Image Rendering....
Read more >
Text-Only PNG with a Transparent Background for Printing
Quick steps for creating and saving text on a transparent background: · Open a blank canvas in the dimensions you desire. · In...
Read more >
Free Online Transparent Image & Text Maker Tool - Canva
1. Open Canva. Start a new project when you upload an image file right away or choose a design template. You can also...
Read more >
Text To Image Generator - GeneratorMix
The generate text to an image, type your text below, select what font you would like to use, set some other options and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found