How do I overlay successive images without resorting to using buffers?
See original GitHub issueHi,
I’ve just discovered sharp. I’m eternally grateful!
I’m trying to overlay many images onto a single image in order to create an image map.
I’ve gotten this far…
sharp({
create: {
width: 512,
height: 512,
channels: 4,
background: {
r: 255,
g: 255,
b: 255,
alpha: 255
}
}
}).png()
.overlayWith(grassImagePath, {
top: 0,
left: 0
})
.pipe(response)
…but naïvely trying to overlay successive images…
sharp({
create: {
width: 512,
height: 512,
channels: 4,
background: {
r: 255,
g: 255,
b: 255,
alpha: 255
}
}
}).png()
.overlayWith(grassImagePath, {
top: 0,
left: 0
})
.overlayWith(concreteImagePath, {
top: 0,
left: 0
})
.pipe(response)
…does not seem to work.
I’ve found a way around this by using buffers:
sharp({
create: {
width: 512,
height: 512,
channels: 4,
background: {
r: 255,
g: 255,
b: 255,
alpha: 255
}
}
}).png()
.toBuffer()
.then(function(buffer) {
sharp(buffer)
.overlayWith(grassImagePath, {
top: 0,
left: 0
})
.toBuffer()
.then(function(buffer) {
sharp(buffer)
.overlayWith(concreteImagePath, {
top: 256,
left: 0
})
.pipe(response)
})
});
I can remove the pyramid with some asynchronous utility function, after all this will be working on an array of images, but I still feel that using buffers like this is wasteful.
So my question is: is there a way to apply successive overlays without resorting to this kind of trickery?
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Overlay Layers (Standard Feature Analysis)—ArcGIS Pro
Overlays the geometries from multiple layers into one single layer. Overlay can be used to combine, erase, modify, or update spatial features.
Read more >Overlay Layers—Portal for ArcGIS
The Overlay Layers tool combines two layers into a single layer using one of five methods: Intersect, Erase, Union, Identity, or Symmetric Difference....
Read more >Lecture 56: Buffering and Overlay Analysis in GIS - YouTube
This lecture is on buffering and overlay analysis in GIS, which is a highly significant element of GIS that is used to analyse...
Read more >Overlaying greyscale color channels - python - Stack Overflow
... easy to do some real color blending of the two images without resorting to some hacky canvas buffer tricks that I've seen...
Read more >Multi Criteria Overlay Analysis (QGIS3) - QGIS Tutorials and Tips
You can do the overlay analysis on vector layers using geoprocessing tools such as buffer, dissolve, difference and intersection.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Your comment on https://github.com/lovell/sharp/issues/405 seems to do things the same way I have done. Namely returning a buffer via a promise then constructing a fresh
sharpinstance with it.I’m happy with this. I have (effectively):
Once I’ve created the loop it’s pretty tidy. I have to update the context with the fresh buffer every time (near the bottom) but apart from that it’s fine.
Thanks for the help. Feel free to close!
Until #405 is complete it makes for cleaner code to do the following and use
awaitinstead of a nasty promise chain. Then when the feature is completed you can just kill off those two lines.