How to get pixels data of image? - node.js

I want to manipulate an image with pure NodeJS
I can't find a way how to get the pixel data of an image. I can get the buffer and base64 of the image. I found many libraries that provide this functionality, but I can't reproduce it myself. I thought this was easy to do because in JavaScript we have a Canvas which gives any information about an image and is easy to manipulate
Here is my code:
var fs = require('fs');
fs.readFile('cat.png', (e,image) => {
console.log(image.toString('base64'));
})
May be here is another way how to read the image?
If this is not possible with pure Node JS, could you please explain to me:
Why?
How do other libraries work with images?

Related

Image resolution low after converting pdf to image using GraphicsMagick for NodeJS

I'm trying to convert pdf into image using gm package and i just can't understand how should i adjust the output resolution.
If i'm doing like this:
gm(`${sourcePath}[0]`)
.setFormat('jpg')
.stream()
.pipe(writeStream);
The output quality here is very low, so i'm trying to use density(594, 842)
like this:
gm(`${sourcePath}[0]`)
.setFormat('jpg')
.quality(100)
.density(595, 842)
.stream()
.pipe(writeStream);
but doing this the image looks very starchy.
Can anyone please point me what should i do in order to receive the
output image close as possible to the pdf source using the npm package?
Or maybe recommend me about a different library that i can use for converting pdf to image (before ocr)

PDFTron imported base64 signature is low quality

I am exporting typed and image signatures using exportSignatures(), wherein I get a String value similar to the following for each one:
[
"data:image/png;base64,qwerty...",
"data:image/png;base64,asdfgh..."
]
Whenever I try to load typed and image upload signatures back using importSignatures(signatureArray), the imported signatures are of much lower quality, and have a slight transparency on the entire image.
Is there a setting in the importSignatures to make it use the original quality?
If I try to parse and load only the actual base64 content (qwerty or asdfgh), it doesn't work.
But if I also try to decode just the base64 using an external decoder I am able to get the original quality of the image.
Here is a sample image of the original (left) and exported-imported (right) signatures:
The code I used to export/import the signatures are similar to the samples in the official documentation:
const signatureTool = docViewer.getTool('AnnotationCreateSignature');
const signatures = signatureTool.exportSignatures();
// save signatures to database as string array
// get signatures from database as string array
const signatureTool = docViewer.getTool('AnnotationCreateSignature');
signatureTool.importSignatures(signatures);
Would you be able to share the code snippet on how to reproduce this? Or would you be able to share a screenshot of the resulted signature? We haven't had such an issue reported before.

express-fileupload:get check size in express js

I am using this package
to upload the image into server ,i want to check the file size before upload but express-fileupload doesn't give any information about it
console.log(req.files.image); it returns only the name,data ,and image type
Assuming the name of your file in your HTML file is image, req.files.image.data.length will give you the buffer length in bytes. See this Node.js. API Documentation for more details. You can then do the math to convert the number of bytes into any thing you want.
This will allow you to get the actual number of bytes independent of filetype and doesn't require using the mv function. So you can use this on any file type not just images.
Hopefully this helps!
I think only way to get image info using express-fileupload is using mv function to move image on server then using image-size to get width and height

Reading the text value or number from an image using node.js

I want to read a number from an image using Node.js.
I am parsing the image using canvas and then reading the image but it gives me the binary data for image but I need the number value that image contains.
Try:
npm install tesseract.js
Then
include file in node.js
var Tesseract = require('tesseract.js');
Then
Tesseract.recognize(
'https://tesseract.projectnaptha.com/img/eng_bw.png',
'eng',
{ logger: m => console.log(m) }
).then(({ data: { text } }) => {
console.log(text);
})
Well, obviously you can't just read data off the image and get the text you need.
You need to interpret the image with some OCR (Optical character recognition) software.
What I could suggest if you are keen on using NodeJS is the node-tesseract module. Make sure to do as the Installation guide says because you also need to install the tesseract-ocr software as well as the module.
First, do install tesseract in your machine by doing the given steps from this tessdocs(tesseract documentation).
Once you are done with the installation please try the steps from the
above comment.

nodejs gm module script to split a big multipage tiff file in multiple single images

When executed from command line, invoking directly GraphicsMagick, the following instructions split correctly a big tiff multipage image in several single image files. Now I need to find the right translation to make it work with the Node.js gm module (
http://aheckmann.github.com/gm/):
gm convert +adjoin -trim input.pdf PNG8:output%03d.png
Any help would be really appreciated.
You can use custom arguments directly from nodejs.
Example:
var gm = require('gm');
gm().command('convert').in('+adjoin').in(filePath).write('image%02d.jpeg', function(err) {
// something
});
Info: https://github.com/aheckmann/gm

Resources