Created
March 22, 2021 17:52
-
-
Save ArturJS/3dc6d611bfd76560487f804a1102283f to your computer and use it in GitHub Desktop.
getImageType.js from @tensorflow/tfjs-node/dist/image.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Helper function to get image type based on starting bytes of the image file. | |
| */ | |
| function getImageType(content) { | |
| // Classify the contents of a file based on starting bytes (aka magic number: | |
| // https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) | |
| // This aligns with TensorFlow Core code: | |
| // https://github.com/tensorflow/tensorflow/blob/4213d5c1bd921f8d5b7b2dc4bbf1eea78d0b5258/tensorflow/core/kernels/decode_image_op.cc#L44 | |
| if (content.length > 3 && content[0] === 255 && content[1] === 216 && | |
| content[2] === 255) { | |
| // JPEG byte chunk starts with `ff d8 ff` | |
| return ImageType.JPEG; | |
| } | |
| else if (content.length > 4 && content[0] === 71 && content[1] === 73 && | |
| content[2] === 70 && content[3] === 56) { | |
| // GIF byte chunk starts with `47 49 46 38` | |
| return ImageType.GIF; | |
| } | |
| else if (content.length > 8 && content[0] === 137 && content[1] === 80 && | |
| content[2] === 78 && content[3] === 71 && content[4] === 13 && | |
| content[5] === 10 && content[6] === 26 && content[7] === 10) { | |
| // PNG byte chunk starts with `\211 P N G \r \n \032 \n (89 50 4E 47 0D 0A | |
| // 1A 0A)` | |
| return ImageType.PNG; | |
| } | |
| else if (content.length > 3 && content[0] === 66 && content[1] === 77) { | |
| // BMP byte chunk starts with `42 4d` | |
| return ImageType.BMP; | |
| } | |
| else { | |
| throw new Error('Expected image (BMP, JPEG, PNG, or GIF), but got unsupported ' + | |
| 'image type'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment