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

Line/Shape colors and weights #60

Closed
blasky opened this issue Feb 5, 2020 · 3 comments
Closed

Line/Shape colors and weights #60

blasky opened this issue Feb 5, 2020 · 3 comments
Labels
question Further information is requested

Comments

@blasky
Copy link

blasky commented Feb 5, 2020

Morning. Doing some drawing today on my NetVips images. Text and shapes.

Am I correct in my reading of the API that Image.Text() does not take an ink color? Do I need to write the text to my image and then somehow modify the color of what was just written?

And the shape methods, DrawLine(), DrawCircle(), etc., do take an ink color but they do not take a dpi value or line "thickness"? If I want a thicker line do I need to write multiple lines/shapes next to one another?

Thanks.

@kleisauke kleisauke added the question Further information is requested label Feb 12, 2020
@kleisauke
Copy link
Owner

Hello @blasky,

Image.Text() makes a one-band mask image. It has no support for color. For colored text, you need to combine it with other operations. For example:

var text = Image.Text("<i>red</i> text color", dpi: 300);
text = text.Ifthenelse(new[] { 255, 0, 0 }, new[] { 0, 0, 0 }, blend: true);

var text2 = Image.Text("<i>red</i> background", dpi: 300);
text2 = text2.Ifthenelse(new[] { 0, 0, 0 }, new[] { 255, 0, 0 }, blend: true);

Image.Arrayjoin(new[] { text, text2 },
    across: 1, shim: 10, background: new double[] { 255, 255, 255 }).WriteToFile("text.png");

To make:
text


The image.Draw* operations are only really useful for very simple "paintbox"-style programs. I'd be cautious about using them anywhere else. The simplest solution for general drawing tasks is to load an SVG. For example:

var svg = @"
  <svg height='300' width='300'>
    <line x1='0' y1='0' x2='300' y2='300' stroke='black' stroke-width='3' />
    <circle cx='150' cy='150' r='140' stroke='black' stroke-width='3' fill='red' />
    <text x='50%' y='50%' dy='.5em' font-family='Arial' font-weight='bold' font-size='2em' text-anchor='middle' fill='white'>I love SVG!</text>
  </svg>";
var image = Image.SvgloadBuffer(Encoding.UTF8.GetBytes(svg), access: Enums.Access.Sequential);
image.WriteToFile("shapes-and-text.png");

To make:
shapes-and-text

The libvips SVG loader is very fast and can make images of any size. It renders progressively, so it doesn't need much memory either.

Use the boolean operators to mask other images with the result, or use image.Composite to layer images together with the PDF blend modes.

@blasky
Copy link
Author

blasky commented Feb 12, 2020

Makes perfect sense. Will do. Thank you.

@kleisauke
Copy link
Owner

Hope this helped, please feel free to re-open if questions remain.

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

No branches or pull requests

2 participants