# Decode JPEG from byte\[\]?

**URL:** <https://discuss.ai.google.dev/t/decode-jpeg-from-byte/31508>\
**Category:** JVM\
**Tags:** sig\_jvm, java, help\_request\
**Created:** [January 17, 2023, 7:55am UTC](https://discuss.ai.google.dev/t/decode-jpeg-from-byte/31508 "2023-01-17T07:55:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebastian-sz](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/sebastian-sz/32/9617_2.png) [@sebastian-sz](https://discuss.ai.google.dev/u/sebastian-sz)\
**Post date:** [January 17, 2023, 7:55am UTC](https://discuss.ai.google.dev/t/decode-jpeg-from-byte/31508/1 "2023-01-17T07:55:11Z")

</div>

Hello,  
I am having raw JPEG bytes (as `byte[]`), which I am trying to decode for inference.

I was thinking of loading them into a `TString tensor` and using `tf.image.DecodeImage`. Or is there a better way?

I’m trying to read bytes content to a Tensor. Looking at [legacy samples](https://github.com/tensorflow/java-models/blob/2ef1672a7d0cb489941541cfba14a9f9197a09b4/tensorflow-examples-legacy/label_image/src/main/java/LabelImage.java#L48) it used to be possible to simply do:

```java
byte[] bytes = Files.readAllBytes(Paths.get(filename));
try (Tensor<String> input = Tensors.create(bytes);

```

However on newer versions there doesn’t seem to be `Tensor.create`. Is there a newer equivalent?

---

<div class="post-metadata">

**Author:** ![karllessard](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/karllessard/32/9185_2.png) [@karllessard](https://discuss.ai.google.dev/u/karllessard)\
**Post date:** [January 18, 2023, 1:42pm UTC](https://discuss.ai.google.dev/t/decode-jpeg-from-byte/31508/3 "2023-01-18T13:42:58Z")

</div>

Hi Sebastian,

There are different ways to achieve this in new TF Java but the first that comes to my mind is that, since the `DecodeImage` expects a String scalar value in input, you can simply convert your `byte[]` to a String and create a scalar:

```auto
try (var tensor = TString.scalarOf(new String(bytes))) { 
    ... 
}

```

I’m not sure I’ve tried it myself before. If conversion to a `String` must be avoided, then pass the tensor data as an scalar of `byte[]` like this:

```auto
try (var tensor = TString.tensorOfBytes(NdArrays.scalarOfObject(bytes))) {
   ...
}

```

It is also possible to read the source image using AWT and convert it to a tensor within your app, in case you don’t want to use `DecodeImage` anymore (though it should work), check this [example](http://discuss.ai.google.dev/t/creating-float-tensors-from-bufferedimage-in-java-kotlin/1865).

---

<div class="post-metadata">

**Author:** ![sebastian-sz](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/sebastian-sz/32/9617_2.png) [@sebastian-sz](https://discuss.ai.google.dev/u/sebastian-sz)\
**Post date:** [January 19, 2023, 9:54am UTC](https://discuss.ai.google.dev/t/decode-jpeg-from-byte/31508/5 "2023-01-19T09:54:08Z")

</div>

Hi Karl,  
thank you for the response and the links. I will give it a try.
