Graphicsmagick: How to draw text to fit a specific box (width/height) - node.js

I'm using GraphicsMagick's node.js module.
In my Node JS application I need to add a text label to an existing image, such that:
1) If the text is longer than the image size, the text auto-wraps around to the next line
2) If the text is not able to fit into the image, the font size of the text is automatically adjusted (reduced) to fit into the box of the image
The available APIs from GraphicsMagick's node.js module are:
gm("img.png").drawText(x, y, text [, gravity])
But this does not provide any options to fit the text into the box per the two requirements above. If I use this API alone I will need to manually break the text, and change the text font size to fit the image, but even for that I will need to first be able to measure the text size. Is there a way to do that?
Any suggestions?
For illustration purpose, below image shows three different text strings that need to fit into the same sized box. Hope it clarifies.

I have lost half a day, but can reach solve for this question. I use gm with gm.subClass({ imageMagick: true }). I think you can use it with a few changing for fully resolve your problem.
public createPreview(text: string) {
this.imageGenerator(800, 600, '#4c4cfc')
.fill('#FFFFFF')
.font('../../../client/assets/fonts/GraphikLCG-Bold.woff')
.fontSize('46')
.out('-background', '#4c4cfc')
.out('-size', '400x', 'caption:' + text)
.out('-gravity', 'center')
.out('-composite')
.write('./brandNewImg.jpg', (err: any) => {
console.log(err);
});
}

Related

How to add text annotation to existing PDF using pdf-annotate Python

I'm trying to use this Python library called pdf-annotate to add text annotation to existing PDF files. Here's the documentation of the library: https://github.com/plangrid/pdf-annotate
I got this sample code that add a square on the bottom left corner of the pdf file but couldn't figure out how to add free text instead of square. For example, I want to add a comment like "This document has been reviewed." instead of the square. This should be annotation on the existing pdf page instead of adding a new page to the pdf.
from pdf_annotate import PdfAnnotator, Appearance, Location
annotator = PdfAnnotator('sample.pdf')
annotator.add_annotation(
'square',
Location(x1=50, y1=50, x2=100, y2=100, page=0),
Appearance(fill=(1, 0, 0))
)
annotator.write('annotated.pdf')
I've been trying to review the source code in the documentation to figure out how to add text instead of square but couldn't figure it out. Does anyone know how to use this library to add text?
In my case the following worked:
annotator.add_annotation(
'text',
Location(x1=x_start, y1=y_start, x2=x_end, y2=y_end, page=0),
Appearance(content='Review...', font_size=10, fill=(0,0,0)),
)
But especially the "fill" argument is required. It gives the color of the text and must be specefied.

Add SVG image to PdfSignatureAppearance with iText7

I want to add a SVG image to PdfSignatureAppearance. The method setSignatureGraphic has an ImageData parameter now in iText7. I couldn't find a way to create an imageData from SVG because ImageDataFactory is not supporting this format.
Can you please guide me on how to do that?
Note that with iText5 I was able to add svg after converting it to PDF and import it to a PDFTemplate then create an image after instantiate the PDFTemplate. setSignatureGraphic was accepting com.itextpdf.text.Image as parameter
Your question could be split into 2 more precise and simple ones:
How to process an SVG with iText?
How to create an ImageData instance out of the result of point 1?
As for question 1: one can use SvgConverter class (part of iTextCore's svg module). Unfortunately there are only PDF-related methods there: an SVG could be converted either to Image (class of layout module), or to PdfFormXObject (again PDF-related) or to a PDF file.
// to PDF
SvgConverter.convertToImage(new FileInputStream(sourceFolder + "your_svg.svg"), signer.getDocument()); // the mentioned `signer` is the instance of PdfSigner which you use to sign the document
// to Image
SvgConverter.convertToImage(new FileInputStream(sourceFolder + "your_svg.svg"), new File(destinationFolder + "svgAsPdf.pdf"));
As for question 2, there are several answers:
a) Suppose that you want to use this Image as the PdfSignatureAppearance's graphics data. For now the class doesn't provide a convenient setter, however, you could use some low level methods - either getLayer0 or getLayer2 to get the signature's background or foreground. They are represented by PDfFormXObject, hence you can use Canvas to add your image to them:
Image svg = SvgConverter.convertToImage(new FileInputStream(sourceFolder + "your_svg.svg"), signer.getDocument());
Canvas canvas = new Canvas(appearance.getLayer0(), signer.getDocument());
canvas.add(svg);
canvas.close();
b) Suppose that your goal is to use the rendered bitmap as the PdfSignatureAppearance's graphics data. Then there is a specific iText product - pdfRender - which converts PDF files to images. The following code could be applied:
PdfToImageRenderer.renderPdf(new File(destinationFolder + "svgAsPdf.pdf"), new File(folderForTheResultantImage));
Now you can create an ImageData instance out of the resultant image file (by default a PDF is converted to a series of images with the format "pdfnamePAGE_NUMBER.jpg", but one could customize either the name or the output image format). In your case the PDF consist of just one page (which represents the converted SVG) and its name is "image1.jpg". The rest is obvious:
appearance.setSignatureGraphic(ImageDataFactory.create(destinationFolder + "image1.jpg"));

watermark in image using nodejs

I have added watermark on image using node_module 'gm', i had make font color grey but i found an bug in that if image content grey color so my watermark merged with image so i can't read the watermark text, that's why i want to create an overlay between image and watermark text but i don't know how i achieve it my current code is
const image = gm(__dirname+'/download.jpeg').fill('#ffffff').font('Arial', 10, '#ffffff').drawText(10, 20, "some text");
image.write('result.png', err => {
if(err) return console.error(err);
});
and i don't know how to make overlay between them my actual image is
and my watermarked image is
any help should be appreciate thanks in advance
So there is no such module that can give you the expected result.
But I faced the same problem & came across this module "node-caption" but again this module is also not giving us the expected result. So I have tweaked this module a little bit & made it work as expected.
Here is the URL to the modified module of my git hub repo https://github.com/mohsincynexis/node-caption.
The changes are only in the generate.js file.
Here is the attached image after watermark.

Converting PDF/SVG to PNG with antialias off using Magick++

I've tried to convert a SVG file to PNG with antialiasing off in Magick++ but I wasn't successful. But I was able to convert the SVG file to PDF with another program and the use the ImageMagick convert command to convert the PDF file to PNG.
How can I use ImageMagick to do it? The command I use for converting PDF to PNG is this:
convert +antialias -interpolate Nearest -filter point -resize 1000x1000 "img.pdf" PNG24:"filter.png"
Is there any way to use Magick++ to do that or better, convert SVG to PNG directly with antialiasing off?
Thanks in advance.
Edit:
The answer given in this post doesn't work for me. Possible because I'm using a colored SVG instead of 1-bit alpha channel. Also I mentioned in my question that I'm also looking for a way to do this in Magick++.
Magick++ has the Magick::Image::textAntiAlias & Magick::Image::strokeAntiAlias methods available, but they would only be useful if your parsing the SVG and rebuilding the image (i.e. roll-your-own SVG engine) one SVG element at a time.
As #ccprog pointed out in the comments; once the decoder utility rasters the vectors, the damage is done & setting the flags would not have an effect on the resulting resize.
Without seeing the SVG, I can only speculate what the problem is. I would suggest setting the document size before reading the SVG content.
For example, read the image at a smaller size than resample up.
Magick::Image img;
img.size(Magick::Geometry(100, 100)); // Decode to a small context
img.read("input.svg");
img.interpolate(Magick::NearestInterpolatePixel);
img.filterType(Magick::PointFilter);
img.resize(Magick::Geometry(600, 600));
img.write("PNG24:output#100x100.png");
Or render at larger size then the finial image.
Magick::Image img;
img.size(Magick::Geometry(1000, 1000)); // Decode to a larger context
img.read("input.svg");
img.interpolate(Magick::NearestInterpolatePixel);
img.filterType(Magick::PointFilter);
img.resize(Magick::Geometry(600, 600));
img.write("PNG24:output#1000x1000.png");
Update from comments
For Postscript (PDF) & True-Type antialiasing, you would set Magick::Image::textAntiAlias (or Magick::Image::antiAlias if using IM6) to false. Just ensure that the density is set to allow any overhead.
Magick::Image img;
img.density(Magick::Point(300));
if (MagickLibVersion < 0x700) {
img.antiAlias(false);
} else {
img.textAntiAlias(false);
}
img.interpolate(Magick::NearestInterpolatePixel);
img.filterType(Magick::PointFilter);
img.read("input.pdf");
img.resize(Magick::Geometry(1000, 1000));
img.write("PNG24:output.png");

how to correct the orientation of the image with ITK-VTK

I am a beginner in VTK ITK, I am trying to read a DICOM series with ITK and display with VTK but I had pictures upside down, I tried to read a single image (JPG) with ITK and visualuser with VTK it is the same problem, so I had the idea of ​​treating the image on photoshop ie I applied to the original image rotation (vertical symmetry of the work area) and I did the reading with ITK and display with VTK, the image is displayed in the correct orientation, infact ITK keeps the orientation of the image, but the problem is at VTK, it is which displays the image upside down, I searched all over the internet I have not found a solution or a method or not even an idea, I encountered the same problem in many forums but there is no response, I count on your help, I can not apply any image processing to find a solution to this problem.
Please Help! thank you in advance
Ideally you should re-orient your camera in VTK so that it is suited for medical image visualization. (The default camera in VTK uses the computer graphics conventions).
If you want a quick hack, you can copy-paste the following code in ITK:
FlipFilterType::Pointer flipperImage = FlipFilterType::New();
bool flipAxes[3] = { false, true, false };
flipperImage = FlipFilterType::New();
flipperImage->SetFlipAxes(flipAxes);
flipperImage->SetInput( image );
flipperImage->Update();
I use a rapid way to set the orientation:
imageActor->SetOrientation(180,0,0);
No need to add filter.
Here's an example of how I would do it. I'm not sure what classes you are using, so I cannot be specific.
vtkSmartPointer<vtkImageData> result = vtkSmartPointer<vtkIMageData>::New();
result->DeepCopy(YourImage); //DeepCopy your image to result
rImage->Update();
double val;
int i = 0;
for(vtkIdType f = result->GetNumberOfPoints()-1; f > -1; f--)
{
val = YourImage->GetPointData()->GetScalars()->GetTuple1(f);
result->GetPointData()->GetScalars->SetTuple1(i,val);
i++;
}
result->Update();
//Now Visualize your image

Resources