# Saving image to a file using NvJPEGEncoder

**URL:** <https://forums.developer.nvidia.com/t/saving-image-to-a-file-using-nvjpegencoder/58919>\
**Category:** Jetson TX2\
**Created:** [March 8, 2018, 4:41am UTC](https://forums.developer.nvidia.com/t/saving-image-to-a-file-using-nvjpegencoder/58919 "2018-03-08T04:41:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhargavK](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@bhargavK](https://forums.developer.nvidia.com/u/bhargavK)\
**Post date:** [March 8, 2018, 4:41am UTC](https://forums.developer.nvidia.com/t/saving-image-to-a-file-using-nvjpegencoder/58919/1 "2018-03-08T04:41:00Z")

</div>

Hello,

I am trying to save the I420 data stored in a program buffer using MMAPI NvJPEGEncoder.

I follow the example in ~/tegra\_multimedi\_api/samples/05\_jpeg\_encode.

I wrote a class to be used with my detectnet and I want to save the cropped images based on the detections.

The problem with saving using this code is very much like the one described in [https://devtalk.nvidia.com/default/topic/1028968/jetson-tx2/how-to-save-image-to-file-/post/5235026/#5235026](https://devtalk.nvidia.com/default/topic/1028968/jetson-tx2/how-to-save-image-to-file-/post/5235026/#5235026)

BUT, only when I try to save the cropped image (I see some green images or some images with information from some previous frame, it is random), i.e., the cropInfo in line 47 has values obtained from detections. If I try to save the entire frame, then it works perfectly!

What could be going wrong when I set cropRect to some non zero values?

Can anybody help?

EDIT: Removed the class as the link in #2 does have the exact same issue and a generic sample code.

---

<div class="post-metadata">

**Author:** ![DaneLLL](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/danelll/32/14043_2.png) [@DaneLLL](https://forums.developer.nvidia.com/u/DaneLLL)\
**Post date:** [March 8, 2018, 9:01am UTC](https://forums.developer.nvidia.com/t/saving-image-to-a-file-using-nvjpegencoder/58919/2 "2018-03-08T09:01:27Z")

</div>

Please refer to  
[url][does the jpegEncoder not clear the buffer? - Jetson TX2 - NVIDIA Developer Forums](https://devtalk.nvidia.com/default/topic/1029239/jetson-tx2/does-the-jpegencoder-not-clear-the-buffer-/post/5235949/#5235949%5B/url%5D)

---

<div class="post-metadata">

**Author:** ![bhargavK](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@bhargavK](https://forums.developer.nvidia.com/u/bhargavK)\
**Post date:** [March 9, 2018, 12:37am UTC](https://forums.developer.nvidia.com/t/saving-image-to-a-file-using-nvjpegencoder/58919/3 "2018-03-09T00:37:10Z")

</div>

Thanks, DaneLLL! This resolved my issue.

I have a few questions and suggestions that might help all of us here.

1. If the crop width and height are required to have even values, why not mention it in the docs? Even when I run the 05\_jpeg\_encode sample from MMAPI with odd values, I get the invalid output. It will be really helpful.
2. What is the reason for having crop width and height to be even? Is it the underlying compression that requires it or the way MMAPI handles different buffers or something else?
3. This is purely a suggestion as co-incidentally, I did stumble upon the post by the same user (linked in #1) who had made another post (which has the solution, linked in #2 above). However, as my search had a keyword nvjpegencoder, the second post wouldn't appear. Maybe you guys can improve the forums by generating automatic tags that would help further narrowing down to search for issues within a specific channel. I am sure you already have a good amount of data from this forums and definitely all the resources. :)

Thanks!

---

<div class="post-metadata">

**Author:** ![DaneLLL](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/danelll/32/14043_2.png) [@DaneLLL](https://forums.developer.nvidia.com/u/DaneLLL)\
**Post date:** [March 9, 2018, 1:53am UTC](https://forums.developer.nvidia.com/t/saving-image-to-a-file-using-nvjpegencoder/58919/4 "2018-03-09T01:53:56Z")

</div>

Hi bhargavK,

> [@](#):
>
> If the crop width and height are required to have even values, why not mention it in the docs? Even when I run the 05\_jpeg\_encode sample from MMAPI with odd values, I get the invalid output. It will be really helpful.
> 
> What is the reason for having crop width and height to be even? Is it the underlying compression that requires it or the way MMAPI handles different buffers or something else?

We will check and evaluate, but I think it is pretty common to try even value in SW programming. Most HW engine is with limitation of data alignment.

> [@](#):
>
> This is purely a suggestion as co-incidentally, I did stumble upon the post by the same user (linked in #1) who had made another post (which has the solution, linked in #2 above). However, as my search had a keyword nvjpegencoder, the second post wouldn’t appear. Maybe you guys can improve the forums by generating automatic tags that would help further narrowing down to search for issues within a specific channel. I am sure you already have a good amount of data from this forums and definitely all the resources. :)

One day you would see AI do this and never get a comment from a real person like me.

---

<div class="post-metadata">

**Author:** ![bhargavK](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@bhargavK](https://forums.developer.nvidia.com/u/bhargavK)\
**Post date:** [March 9, 2018, 2:15am UTC](https://forums.developer.nvidia.com/t/saving-image-to-a-file-using-nvjpegencoder/58919/5 "2018-03-09T02:15:24Z")

</div>

I am looking forward to the day when no one asked such questions and everything was powered by AI… :)

But honestly, it feels good to thank a person like you who helps us all the time! I still don’t thank Alexa.

Thanks a lot, I will remember the following advice!

> [@DaneLLL](#):
>
> Hi bhargavK,  
> We will check and evaluate, but I think it is pretty common to try even value in SW programming. Most HW engine is with limitation of data alignment.

---

<div class="post-metadata">

**Author:** ![nadeemm](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/nadeemm/32/367020_2.png) [@nadeemm](https://forums.developer.nvidia.com/u/nadeemm)\
**Post date:** [October 18, 2021, 6:28pm UTC](https://forums.developer.nvidia.com/t/saving-image-to-a-file-using-nvjpegencoder/58919/6 "2021-10-18T18:28:23Z")

</div>

Closed on October 18, 2021, 6:28pm UTC


