Comments (10)
For what its worth this solution worked for me
lwip.open(filename, function (err, originalImage) {
if (err) {
throw err;
}
originalImage
.batch()
.resize(200, 200)
.toBuffer('jpg', function (err, buffer) {
var bufferStream = new stream.PassThrough();
bufferStream.end( buffer );
// bufferStream.pipe( process.stdout );
});
});
from lwip.
Hi, was this available already somehow? I've seen the examples of reading from a stream, but can't figure out how to write back to a stream.
To give some context. I've seen people use imagemagick in examples, and afterwards using "pipe" to get it back in their main process' stream. Hoped this would be a good alternative (without needing IM).
from lwip.
I haven't added streams support to the API yet (pull requests are welcome!).
I'm not sure exactly what you need, can you clarify?
Basically, if you want to stream image data out, you can get the image as a buffer and use it as a basis for a stream (see here for example).
If you want to get image data into your app from a stream, you can store it in a buffer and then use lwip.open
to decode the buffer.
from lwip.
Thanks Eyal, I'm aware that my questions must be strange. I feel like I only know half of what I need to ask the right questions :)
I'll try to clarify a bit. I'm twiggling my thumbs with MeteorJS. Using a package for that called CollectionFS which helps with managing uploaded images. All of their examples are using ImageMagick, where I would much rather have something without dependencies like lwip.
Now. The CollectionFS has support for plugging in "transformations". Somewhere in this whole setup, something is not working like it should. I've played with Async libraries, wrappers, etc. all over the place. But still no go.
Their transformation functions have this signature:
function myTransformFunction(fileObj, readStream, writeStream);
So my thought was, heck just read in the readStream, do magic lwip stuff and write back to the writeStream.... not so easy though :)
Here is one of their examples using ImageMagick (just a snippet for the syntax):
transformWrite: function(fileObj, readStream, writeStream) {
// Transform the image into a 10x10px thumbnail
gm(readStream, fileObj.name()).resize('10', '10').stream().pipe(writeStream);
}
is something like this possible in lwip through this new stream support you mentioned? Or is this something for the future.
Cheers.
from lwip.
@bensmeets
Based on the docs I'd try something like this:
- Define your own readable stream.
- Pipe it to the writable stream provided by CollectionFS.
- Read data from the readable stream provided by CollectionFS into a buffer, until the stream is exhausted.
- Once you have all the data in the buffer, decode it with lwip.
- Use lwip to manipulate the image.
- Encode the image to a buffer and emit the data your readable stream.
Pseudo code (not tested):
transformWrite: function(fileObj, readStream, writeStream) {
// Create a new readable stream
var middleStream = /* new stream.Transform() */;
middleStream.pipe(writeStream);
// temporary cache for original image data
var oImBuf = new Buffer();
// original image format (for decoder)
var ext = fileObj.extension;
// read original image from the stream
readStream.on('data', function(chunk) {
oImBuf = Buffer.concat(oImBuf, chunk);
});
// ready to decode original image
readStream.on('end', function() {
lwip.open(oImBuf, ext, function(err, img) {
// do some manipulations...
img.batch()
.scale(0.5)
.sharpen(10)
// get buffer of new manipulated image as a JPEG
.toBuffer('jpg', function(err, nImBuf){
// emit the new image to the middle stream
/* middleStream.push(nImBuf); */
// close the stream
/* middleStream.end(); */
});
});
});
}
Note that:
- We pipe our own read stream to the framework's write stream.
- We publish data to our stream only when we finish manipulating the image.
- I have commented out the parts related to our custom read stream because I'm not sure this is exactly how it works. It might be needed to create a class that implements a readable stream. But that shouldn't be very complicated.
Let me know if it helps.
from lwip.
@EyalAr Thanks, very grateful that you took the time for such a complete comment.
I've been struggling with it a lot and finally decided that the CollectionFS is too much black box material for me. It kept throwing errors at me when this was implemented. I'm switching to something more manual that gives some more insights.
I'm keep the above code put though! The manual alternative will need it as well. When I get it working there I'll shout out.
Ohhhh the good old days of synchronous code :)......
from lwip.
Just to keep things together, the issue was with the syntax for streaming, after switching to something more manual (regarding CollectionFS), everything went without a problem.
from lwip.
@bensmeets
Awesome, thanks for the update
from lwip.
Any update on streams? I'm trying to get an image in through busboy, process it with lwip, then send it out to MongoDB GridFS. Busboy puts out a stream and GridFS prefers a stream, but lwip is in the middle with only buffers.
from lwip.
@wmassingham I'll try to target it to v0.0.7. In the meantime, perhaps you can try to adapt the pseudo code I posted above.
Also, it would be good if you can post a sample of how you see streams implemented. If it was available, how would you use it?
Thanks.
from lwip.
Related Issues (20)
- Two images as one image
- No need to install other dependencies on the machine.. Not to install a lot like py, so there is a soft trouble
- I cannot install css-sprite because lwip has a bug
- TIFF support
- Failed to install on Redhat7 HOT 1
- Installation Failed - Build issue HOT 18
- CPU Usage
- npm install --save @dadi/cdn is failing with error on windows 7 HOT 2
- Project dead? HOT 4
- browser version broken in this release
- Problem with build on node 8 HOT 3
- node-gyp rebuild HOT 7
- LWIP installation error HOT 6
- Installation failing on Cloud 9 Web IDE
- Installation error HOT 7
- Installation Error HOT 3
- can it classify image
- Virtual routing and Forwarding(VRF) integration in lwIP HOT 1
- Failed install HOT 3
- Cmake vs2019 build error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lwip.