When calling the .toBuffer() method on a sharp object, I get the following error:
Input buffer contains unsupported image format
I'm fetching an image from S3, and I am certain its retrieved as a Buffer which is then passed to sharp.
Moreover, when I console.log the base64 of the buffer and check what's behind, I do get the original image and the filetype is jpeg, so I can't seem to figure out why it would say that the Input buffer contains unsupported image format.
const imageBuffer = fileFromS3.Body
console.log(imageBuffer)
console.log(imageBuffer.toString('base64'))
let result = await sharp(imageBuffer)
.toColourspace('b-w')
.sharpen()
.toBuffer()
When I check the sharp object before running toBuffer() I get this data in the object:
let result = await sharp(imageBuffer)
.toColourspace('b-w')
.sharpen()
console.log(result)
Prints:
...
input: {
failOnError: true,
limitInputPixels: 268402689,
sequentialRead: false,
buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff db 00 43 00 03 02 02 02 02 02 03 02 02 02 03 03 03 03 04 06 04 04 04 04 04 08 06 06 05 ... 96981 more bytes>
}
Related
I am able to retrieve a blob object of my profile picture from the microsoft graph api. However, I don't know how to save the blob to a file like I would with python.
When I console.log the blob itself it comes up like this:
console.log(profilepic)
Blob {
[Symbol(type)]: 'image/jpeg',
[Symbol(buffer)]: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 60 00 60 00 00 ff db 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d 0c 0b 0b 0c 19 12 13 0f ... 9870 more bytes>
so I thought I could try to save it to file like so:
console.log(profilepic)
test = await profilepic.arrayBuffer();
var imageName = 'test.jpg';
fs.createWriteStream(imageName).write(test);
but got an error like this:
(node:24176) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type object
This is my first time trying something like this in nodeJS.
In python I would be able to do the same thing like this:
the graph_user_picture.content is the same as the profilepic blob.
graph_user_picture = requests.get(app_config.USERPHOTO,
headers={"Authorization": "Bearer " + token['access_token']})
file = open("static/user_iamge"+session["user"]["name"]+".png", "wb")
file.write(graph_user_picture.content)
file.close()
How can I save a blob image to a folder?
lakhal#lakhal-ThinkPad-T430s:~$ hexdump -C /dev/bus/usb/003/014
00000000 12 01 00 02 e0 00 00 40 e8 04 63 68 ff ff 02 03 |.......#..ch....|
00000010 04 01 09 02 4b 00 02 01 00 c0 30 08 0b 00 02 e0 |....K.....0.....|
00000020 01 03 07 09 04 00 00 01 e0 01 03 05 05 24 00 10 |.............$..|
00000030 01 05 24 01 00 01 04 24 02 00 05 24 06 00 01 07 |..$....$...$....|
00000040 05 83 03 08 00 09 09 04 01 00 02 0a 00 00 06 07 |................|
00000050 05 81 02 00 02 00 07 05 02 02 00 02 00 |.............|
0000005d
The file allows you to read certain USB descriptors from the device. The USB device descriptor, which is defined by the USB specification, is located at the beginning of the file.
For more details, see the file usb/core/devio.c in the source code of your kernel. In particular, look at the function usbdev_read, which implements the kernel side of the read system call for these special files. I cannot find official documentation at the moment.
I believe some parts of the file are "holes", meaning that you won't be able to read any data from them, and you might get meaningless data depending on whether the program you uses initializes the buffers it uses for reading from the file.
I'm using Angular 2 to retrieve image from backend. My response looks like that
{ AcceptRanges: 'bytes',
LastModified: 'Sat, 09 Dec 2017 17:06:46 GMT',
ContentLength: '462882',
ETag: '"c0ff5b060c2da69deff97065d43e3645"',
ContentType: 'application/octet-stream',
Metadata: {},
Body: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 02 00 00 64 00 64 00 00 ff ec 00 11 4475 63 6b 79 00 01 00 04 00 00 00 3c 00 00 ff ee 00 0e 41 64 6f 62 65 00 64 ... > }
Is there any chance I can insert that thing directly inside html img tag, or should I start off by converting it to say base64? Thank you in advance.
I would suggest you to use responseType: ResponseContentType.Blob in your GET-Request settings and convert it later to base64 encoded source. Hope this helps
a solution is to use Base64 Image source, that should return encoded base64 from backend. forExample:
img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP//ywAAAAAAQABAAACAUwAOw==";
In Expressjs I upload an image. It comes as a Buffer encoding 7bit:
{ fieldname: 'file',
originalname: 'img.JPG',
encoding: '7bit',
mimetype: 'image/jpeg',
buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 64 00 64 00 00 ff db 00 43 00 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 0
1 01 01 01 01 01 ... >,
size: 873066 }
How to save it as an image to a file system? Something is saved but it is not an image:
Windows Photo Viewer can't open this picture because the file appears
to be damaged, corrupted of too large.
These do not work:
fs.writeFile('uploaded-img.jpg', req.file, 'ascii', function(err) {
fs.writeFile('uploaded-img.jpg', req.file.toString('ascii'), 'ascii', function(err) {
How about:
fs.writeFile('uploaded-img.jpg', req.file.buffer, 'ascii', function(err) {
i have a file with .raw extension (raw image format), but i cannot open it. i tried to analyze it with the commands file filename.raw, exiv2 print, exiftool, ufraw, dcraw. since it appears to be a binary file, i tried to extract information using strings filename.raw, hexdump and xxd. it is surprising that the file does not appear to have any metadata. i suspect the file extension to be wrong, and the file to be manipulated.
how else can i determine the type? how can i extract any information?
thank you very much for any suggestions.
EDIT: a sample file can be downloaded
here.
Hmm... well,
//Judging from the number of bytes in the file:
// 1447600
//This number is exactly.
// 3 x 499 x 967
//Which is very odd, but it means if this is an image, then it must be 3 bytes per pixel and either 499 x 967 or 967 x 499
EDIT: resolution is wrong. Probably bits per channel as well, this is what I get for using bc in the middle of the night instead of WA.
Actual factors are 2^4*5^2*7*11*47, which means it cannot be 3 Bytes per pixel. There might be some stream compression going on? But probably not, the number is too nice.
Looking at some of the data, most of it is quite noisy, which fits with a camera with a noisy digital sensor:
00000000 0e 08 06 02 04 06 00 02 00 04 01 04 0e 03 05 01 |................|
00000010 03 01 06 04 00 03 01 05 01 00 02 00 06 02 04 01 |................|
00000020 00 07 0c 0d 05 01 0a 0d 03 08 11 08 0c 03 06 06 |................|
00000030 08 02 07 04 01 01 07 02 05 06 10 04 07 01 04 02 |................|
00000040 02 01 0c 09 07 02 05 01 0d 04 00 05 0e 01 00 07 |................|
00000050 00 02 04 0a 11 04 07 04 01 00 0d 07 0f 06 03 02 |................|
00000060 06 04 0a 0b 03 08 02 0c 00 08 00 04 06 09 03 03 |................|
00000070 04 00 0a 04 01 06 00 00 02 08 0a 07 04 09 01 0b |................|
00000080 01 0c 0d 04 03 03 05 00 02 11 09 00 07 08 08 05 |................|
00000090 04 07 02 0c 00 13 01 06 07 02 08 0a 07 05 02 01 |................|
This area, at the beginning of the file, is likely to be close to all black.
So, of the common 3 bytes per pixel formats, you have BGR24, RGB24 or YUV24.
Because it's simpler to deal with RGB or BGR formats, let's whip up some short code to dump this to a png, using libpng.
First try:
#include <png.h>
#include <exception>
#include <memory>
int main() {
FILE * fpout;
FILE * fpin;
png_structp png_ptr = nullptr;
png_infop info_ptr = nullptr;
const unsigned width = 499;
const unsigned height = 967;
const unsigned channels = 3;
fpin = fopen("sample.raw", "rb");
if (!fpin)
throw std::exception();
std::unique_ptr<char[]> data(new char[width*height*channels]);
fread(data.get(), width*height*channels, 1, fpin);
fclose(fpin);
fpout = fopen("output.png", "wb");
if (!fpout)
throw std::exception();
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!png_ptr) {
fclose (fpout);
throw std::exception();
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
png_destroy_write_struct(&png_ptr, nullptr);
fclose (fpout);
throw std::exception();
}
if (setjmp(png_jmpbuf (png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose (fpout);
throw std::exception();
}
png_set_IHDR(png_ptr,
info_ptr,
width,
height,
8,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
/* Initialize rows of PNG. */
std::unique_ptr<png_bytep[]> row_ptrs(new png_bytep[height]);
size_t stride = width * 8 * channels / 8;
for (size_t i = 0; i < height; ++i) {
size_t q = i * stride;
row_ptrs[i] = (png_bytep)data.get() + q;
}
/* Write the image data to "fp". */
png_init_io(png_ptr, fpout);
png_set_rows(png_ptr, info_ptr, row_ptrs.get());
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, nullptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose (fpout);
}
Gives us:
http://i.imgur.com/Iwi3M.jpg
This is heartening, because it seems that we merely have the resolution wrong. So examining the image, it looks like the bar repeats itself every 467 pixels or there abouts. However, it does not look like there is anything interesting in the data. So it's likely not simple RBG or BGR or YUV encoding, otherwise, it would look nicer.
Raw Therapee doesn't recognize it. So, I'm asking you now. What camera did you use to make this?