Resizing images using imagemagick in node.js - node.js

In my application i tried to resize the image using imagemagick but i got following error error while resizing: imagesexecvp(): No such file or directory.this is my code
im.resize({
srcPath: '/tmp/Images/' + req.files.image.name,
dstPath: 'resized_'+req.files.image.name ,
width:42,
height:42
}, function(err, stdout, stderr){
if (err) {
console.log('error while resizing images' + stderr);
};
});

The imagemagick module uses the convert and identify commands and the error you describe could happen because the commands can't be found. Make sure the commands are in a folder referenced by the path environment variable or, alternatively you can reference the commands in you node application:
var im = require('imagemagick');
im.identify.path = '/opt/ImageMagick/bin/identify'
im.convert.path = '/opt/ImageMagick/bin/convert';

It looks like you are trying to resize the file without changing the destination. Try this:
im.resize({
srcPath: process.cwd() + '/tmp/Images/' + req.files.image.name,
dstPath: process.cwd() + '/tmp/Images/resized_'+req.files.image.name ,
width:42,
height:42
}, function(err, stdout, stderr){
if (err) {
console.log('error while resizing images' + stderr);
};
console.log( process.cwd() + '/tmp/Images/' + req.files.image.name + 'has been resized and saved as ' + process.cwd() + '/tmp/Images/resized_'+req.files.image.name)
});
You might also check your permissions (ls -l) in /tmp/Images/ to make sure they are set properly

You need to install image magic command line tools. Try brew install imagemagick

If you are on ubuntu, install package like this:
apt-get install imagemagick
After that, try again :D

Related

cannot find ffmpeg in videoshow in nodejs

I want to create video from image files. So I have installed videoshow module. And configured the same as per the documentaion.
var videoOptions = {
fps: 25,
loop: 5, // seconds
transition: true,
transitionDuration: 1, // seconds
videoBitrate: 1024,
videoCodec: 'libx264',
size: '640x?',
audioBitrate: '128k',
audioChannels: 2,
format: 'mp4',
pixelFormat: 'yuv420p'
}
var images = [
"D:/PROJECTS/Video/storage/1.jpg",
"D:/PROJECTS/Video/storage/2.jpg"
];
app.get("/video", function () {
videoshow(images, videoOptions)
// .audio('song.mp3')
.save('video.mp4')
.on('start', function (command) {
console.log('ffmpeg process started:', command)
})
.on('error', function (err, stdout, stderr) {
console.error('Error:', err)
console.error('ffmpeg stderr:', stderr)
})
.on('end', function (output) {
console.error('Video created in:', output)
})
});
But When I run it shows the error on server
Error: Error: Cannot find ffmpeg
at D:\PROJECTS\Video\node_modules\videoshow\node_modules\fluent-ffmpeg\lib\processor.js:136:22
at D:\PROJECTS\Video\node_modules\videoshow\node_modules\fluent-ffmpeg\lib\capabilities.js:123:9
at D:\PROJECTS\Video\node_modules\videoshow\node_modules\async\dist\async.js:473:16
at next (D:\PROJECTS\Video\node_modules\videoshow\node_modules\async\dist\async.js:5315:29)
at D:\PROJECTS\Video\node_modules\videoshow\node_modules\async\dist\async.js:958:16
at D:\PROJECTS\Video\node_modules\videoshow\node_modules\fluent-ffmpeg\lib\capabilities.js:116:11
at D:\PROJECTS\Video\node_modules\videoshow\node_modules\fluent-ffmpeg\lib\utils.js:223:16
at F (D:\PROJECTS\Video\node_modules\videoshow\node_modules\which\which.js:68:16)
at E (D:\PROJECTS\Video\node_modules\videoshow\node_modules\which\which.js:80:29)
at D:\PROJECTS\Video\node_modules\videoshow\node_modules\which\which.js:89:16
Then I installed ffmpeg using
npm install ffmpeg --save
but not worked. So I tried installing at the global level using
npm install ffmpeg -g
Even installing on my window machine and setting the path of its bin folder in environment variables did not work?
What could be the issue?
Actually, I need to instal the ffmpeg in my system and set it to the environment path variable.
And you don't need to install in nodejs.
npm i videoshow
Just use this command

Tiff convert to png Node js

i have to convert multiple tiff to png. For example tiff which include 3 pages i should convert to 3 png's.So i am using tiff-to-png module and i have encountered with this problem.
Error: Command failed: convert /tiffs/one.tiff -scene 1 ./png/one/page%d.png
Invalid Parameter - /tiffs.Bellow is my code
'use strict'
const tiff_to_png=require('tiff-to-png');
const options={
logLevel:1
};
const converter=new tiff_to_png(options);
const tiffsLocation=['./tiffs/one.tiff'];
const location='./png';
converter.convertArray(tiffsLocation,location);
In the error context we see -/tiffs inavliiad parameter.
tiffsLocation is the variable which conatin my tiff file.
location is variable which contain path to folder where will be converted png file.
I cant understand why i have goten this error, tiffs in this case is the directory which contain my tiff file why i have got this error.Any ideas?
1st You have to install "Imagemagick"
For windows, you will find .exe file. Keep it in your mind that on installation time, check "Install legacy utilities (e.g: convert)"
For Ubuntu:
sudo apt install imagemagick
For Cent OS:
sudo yum install ImageMagick
var fs=require('fs');
var spawn = require('child_process').spawn;
//ifile: Tiff Absolute File Path
//ofile: PNG Absolute File Path (e.g: var ofile = APP_ROOT_PATH+'/data/files/png/sample.png';)
var tiff2png = spawn('convert', [ifile, ofile]);
tiff2png.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
tiff2png.stderr.on('data', function (data) {
return res.json(Utility.output('Unable to convert tiff to png','ERROR'));
console.log('stderr: ' + data);
});
tiff2png.on('close', function (code) {
/**Check Your Converted file exist or not. If exist then Converted**/
console.log('Close: ' + data);
});
tiff2png.on('error', function (code) {
return res.json(Utility.output('ERROR: Unable to convert tiff to png','ERROR'));
});
I found 2 libraries that do this:
https://github.com/oliver-moran/jimp
https://github.com/lovell/sharp
In the end I went with sharp as jimp could not handle 16bit images

How to convert image type to 'jpg' using imagemagick in node.js

I am using imagemagick in my node.js app. I have resized the image as follows:
im.resize({
format: 'jpg',
srcPath: imagePath,
dstPath: thumbPath,
width: 220,
}, function(err, stdout, stderr){
//some code
}
});
I want my code to convert the incoming images to jpg. How can I achieve this?
For this solution you need use easyimage package . This package run under and need imagemagick (very important).
$npm install easyimage --save // saved in your package.json
Including in your code:
var easyimg = require('easyimage');
First you need get the path of original image:
tmp_path = 'path/to/image.svg';
Now you change the extension of the "tmp_path":
tmp_extless = tmp_path.replace('.svg','.png'); //be sure of include "."
So, tmp_path is the original path, and tmp_extless is the path of our future image.
Now, the magic of easyimage:
easyimg.convert({src: tmp_path, dst: tmp_extless, quality: 80},
function(err,stdout){
if(stdout){ console.log('stdout', stdout);
//here you can run a script to delete tmp_path
}
}
);
Now, your new image is in the path: tmp_extless.
With this method you don't need writeFile or readFile.
You need to writeFileSync and pipe your data in through the anonymous function.
im.resize({
format: 'jpg',
srcPath: imagePath,
dstPath: thumbPath,
width: 220,
}, function(err, stdout, stderr){
fs.writeFileSync('[filename].jpg', stdout,'binary'); //write the file here
}
});
I believe you also need to call the convert method.
im.convert(['originalfilename.originalextension', 'jpg:-'],

No file produced when resizing image with imagemagick on node

So my issue is basically what the title says. When I run resize on a set of images i get no error but I also get no images in the final directory. I made the permissions on the directory 777 just in case that was the issue. I can use imagemagick to read the metadata from the images but it won't resize them. Can you find a simple, or huge, mistake I've made?
var im = require('imagemagick');
im.resize({
srcPath: srcPath,
destPath: destPath,
width: 256,
height: 256
}, function(err, stdout, stderr) {
if (err) {
console.log(err);
} else {
console.log("Image Resized");
}
});
All I ever get out is "Image Resized" out on the console.
Thanks in advance for any help and sorry if this is vague or I missed something.
I feel like an idiot. The problem is the parameter is 'dstPath' not 'destPath'

trying to access and manipulate images with node.js and GD or imagemagick

i'm trying to create a thumbnail of an jpg that resides on the server. i tried using node-gd and/or node-imagemagick but neither could access the file:
var gd = require('node-gd');
gd.openJpeg("./test.jpeg", function (img, path) {
if (img) {
console.log("file opened ... " + img);
}
else {
console.log("failed to open file ...");
}
});
logs: failed to open file ...
imagemagick:
var im = require('imagemagick');
im.identify('./test.jpeg', function (err, features) {
if (err) throw err;
console.log(features);
});
logs: Error: Command failed: execvp(): No such file or directory
but the test.jpeg file is definitely there.
var fs = require('fs');
fs.open(filePath, 'r', function (err, fd) {
console.log("open file ... " + err + " " + fd);
});
works fine!? no error is logged.
i tried chmod 0777 on the jpeg. nothing.
From what I understand of the documentation of the imagemagick module for node is, that the module provides access to the comandline binaries of imagemagick. Do you have imagemagick (the commandline binaries) installed? Are they in the PATH of you shell?
You are looking for a binary named "identify". You can show the path to it by running "which identify". It should give you a full path - if the prompt just returns, you don't have it installed or it's not in your path.
If you are on win32 the which command won't help, you have to check for a binary called identify.exe.
(never worked with gd - so I am unsure there)
here is the imagemagick example with your code - please note, the path to identify may be different in your environment:
snowflake:Desktop rhaen$ node check_im.js
{ format: 'JPEG', width: 320, height: 250, depth: 8 }
snowflake:Desktop rhaen$ which identify
/usr/local/bin/identify
So - the node module and your code works for me.

Resources