I receive an image in base64 string and I wanted to save the image in 3 different sizes. My code for saving the image in my app is as following and it works, how can I set a sepcific size for the image ?
fs.writeFile(pathImage, new Buffer(base64String, "base64"), function (err) {}
You can't just save an image in different sizes by writing part of the file to disk. In order to resize your image you need to first know what image format you are working with and then use an appropriate library to resize the image, usually by reducing image quality or cropping the image.
For example if you are working with a JPEG, PNG, WebP, or TIFF images, you could use https://github.com/lovell/sharp
From its example page
const sharp = require('sharp');
sharp(inputBuffer)
.resize(320, 240)
.toFile('output.webp', (err, info) => ... );
Related
We are on a project with node js and flutter. We use an S3 bucket for storing images. We need to optimize images to a minimum, for easy loading. We use compression from the backend which results in random image sizes for different images. When those images are resized on photoshop with a preferred size of 500 * 500 px,(also used 200 * 200px, 300 * 300px) it results in the same multiple-sized images. What can we do to minimize image size and optimize the image loading performance?
There are many ways to approach this problem.
Try this way.
Create two folders in your S3 bucket: original & resized.
Create a lambda function and add this lambda function to the S3 trigger, so that whenever any image will be uploaded to S3, this trigger will be executed and the lambda function will run.
Upload images from your client-side to an original folder of S3 Bucket.
Once the image is uploaded lambda will run to do the processing on the image (Re-size/optimize ) and will save this processed(re-sized) image to the re-sized folder of the bucket.
Keep polling the resized folder from the client-side to get resized image. (Remember we upload the original image to the “original” folder & get the resized image from the “resized” folder)
Source : https://medium.com/#pushpendrachauhan/re-sizing-optimizing-images-on-the-go-using-amazon-lambda-amazon-s3-bucket-node-js-4fc933e6ddae
https://aws.amazon.com/blogs/compute/resize-images-on-the-fly-with-amazon-s3-aws-lambda-and-amazon-api-gateway/
I want to compare the base64 image from png file, so i am using jimp to decode base64 and write to disk but it writes half of the image; is it possible to compare base64 image to png file directly??
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)
I'm currently looking for a way to generate the thumbnail image for a given pdf file, which shows several pages in the same image. The output should like what shows in the arxiv sanity website. I want to know if there is any npm package which supports this functionality. Thanks.
In ImageMagick command line, you can do that as follows. Suppose you want 8 pages from the PDF.
Input PDF from http://www.arxiv-sanity.com:
convert image.pdf[0-7] -thumbnail 140x140 -background white +smush 20 -bordercolor white -border 10 result.jpg
This takes the first 8 pages, makes thumbnails of size 140x140 and appends them side-by-side with a 20 pixels white spacing between them and adds a 10 pixel white border around it all.
Sorry, I do not know Node.js. But apparently there is a module that integrates ImageMagick. See https://github.com/yourdeveloper/node-imagemagick
var PDFImage = require("pdf-image").PDFImage; //pdf to image convert
var pdfImage = new PDFImage("1120.pdf");
pdfImage.convertPage(0).then(function (imagePath) {
},(err)=>{
console.log("err",err)
})
//##jimp Npm use thumbnail image generate
//if auth error Follow this step :
-> In /etc/ImageMagick-6/policy.xml (or /etc/ImageMagick/policy.xml) find the following line
->
and change it to allow reading and writing by the PDF coder in ImageMagick:
I am converting different images and pdf files with "gm" module for nodejs. Image types go successfully but when I want to convert PDF to image have problems. I need to covert only one selected page from pdf file to jpg/png. If I pass whole pdf file to "gm" it saves to image only first page, but I cannot find the way to save another page.
gm(file).toBuffer(format.toUpperCase(),
function (err, buffer) {
// so in buffer now we have converted image
}
Thank you.
You can use gm.selectFrame like this
gm(file).selectFrame(0).toBuffer() // To get first page
gm(file).selectFrame(1).toBuffer() // To get second page
// for only first pdf page use:
gm(file, 'pdf.pdf[0]').toBuffer(...)
// for only second pdf page use:
gm(file, 'pdf.pdf[1]').toBuffer(...)
There is spindrift for manipulating pdf (includes image conversion).
You can define your pdf using (You don't have you use all of the commands):
var pdf = spindrift('in.pdf')
.pages(7, 24)
.page(1)
.even()
.odd()
.rotate(90)
.compress()
.uncompress()
.crop(100, 100, 300, 200) // left, bottom, right, top
Later on convert to image:
// Use the 'index' property of an image element to extract an image:
pdf.extractImageStream(0)
If you have to use gm, you can do what #Ben Fortune suggested in his comment and split the pdf first.