0

I am trying to convert a uint8 array of a 48x48 image to its corresponding 48x48 image file (jpg/jpeg/gif). I tried converting the array contents to first binary and then wrote ('wb' mode) it to a file, but that did not work out.

Is there a way I can accomplish this?

2
  • You could convert numpy array to PIL.image and then use "save" method to encode it into jpg Commented Mar 5, 2016 at 8:08
  • @Yaroslav Yup there is a way to do this using the PIL library kaggle.com/c/… Commented Mar 5, 2016 at 8:30

2 Answers 2

3

If you are producing the image in TensorFlow (as I'm inferring from your tag), you can use the tf.image.encode_jpeg() or tf.image.encode_png() ops to encode a uint8 tensor as an image:

uint8_data = ...
image_data = tf.image.encode_png(uint8_data)

The result of either op is a tf.string tensor that you can evaluate and write out to a file.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @mrry! Btw I found a way to do this using the PIL python library kaggle.com/c/…
1

I was able to the same very easily using Octave. Here i have tried to generate a random matrix of 48x48 and then i saved the image as jpg format.

img = rand(48,48);
imwrite(img, "test.jpg")

Image of 48x48

You can save any type of image with this approach.

If you can give some more details about what you want to achieve. Do u need to do it just once or u need it as part of program.

Hope that helped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.