MCPcopy
hub / github.com/tensorflow/tfjs / decodeImage

Function decodeImage

tfjs-node/src/image.ts:155–186  ·  view source on GitHub ↗
(
    content: Uint8Array, channels = 0, dtype = 'int32',
    expandAnimations = true)

Source from the content-addressed store, hash-verified

153 * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'}
154 */
155export function decodeImage(
156 content: Uint8Array, channels = 0, dtype = 'int32',
157 expandAnimations = true): Tensor3D|Tensor4D {
158 util.assert(
159 dtype === 'int32',
160 () => 'decodeImage could only return Tensor of type `int32` for now.');
161
162 const imageType = getImageType(content);
163
164 // The return tensor has dtype uint8, which is not supported in
165 // TensorFlow.js, casting it to int32 which is the default dtype for image
166 // tensor. If the image is BMP, JPEG or PNG type, expanding the tensors
167 // shape so it becomes Tensor4D, which is the default tensor shape for image
168 // ([batch,imageHeight,imageWidth, depth]).
169 switch (imageType) {
170 case ImageType.JPEG:
171 return decodeJpeg(content, channels);
172 case ImageType.PNG:
173 return decodePng(content, channels);
174 case ImageType.GIF:
175 // If not to expand animations, take first frame of the gif and return
176 // as a 3D tensor.
177 return tidy(() => {
178 const img = decodeGif(content);
179 return expandAnimations ? img : img.slice(0, 1).squeeze([0]);
180 });
181 case ImageType.BMP:
182 return decodeBmp(content, channels);
183 default:
184 return null;
185 }
186}
187
188/**
189 * Encodes an image tensor to JPEG.

Callers

nothing calls this directly

Calls 8

tidyFunction · 0.90
decodePngFunction · 0.85
decodeGifFunction · 0.85
decodeBmpFunction · 0.85
squeezeMethod · 0.80
getImageTypeFunction · 0.70
decodeJpegFunction · 0.70
sliceMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…