Reputation: 1524
I am trying to read bmp images (2048 x2048), resize them to 256x 256 and write the image to disk using tensorflow. I have succeeded in reading it but unable to find a way write it to disk. Any idea how to do it ?
Here is the code below:
import tensorflow as tf
img_path = "D:/image01.bmp"
img = tf.read_file(img_path)
img_decode = tf.image.decode_bmp(img, channels=1) # unit8 tensor
IMG_WIDTH = 256
IMG_HEIGHT = 256
img_cast = tf.cast(img_decode,dtype=tf.uint8)
img_4d = tf.expand_dims(img_cast, axis=0)
img_res = tf.image.resize_bilinear(img_4d, (IMG_HEIGHT, IMG_WIDTH), align_corners=True)
session = tf.InteractiveSession()
file_name = "D:/out.bmp"
file = tf.write_file(file_name, img_res)
print('Image Saved')
session.close()
Error:
ValueError Traceback (most recent call last)
D:\Users\ge3f-P2\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
509 as_ref=input_arg.is_ref,
--> 510 preferred_dtype=default_dtype)
511 except TypeError as err:
D:\Users\ge3f-P2\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
1145 if ret is None:
-> 1146 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1147
D:\Users\ge3f-P2\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in _TensorTensorConversionFunction(t, dtype, name, as_ref)
982 "Tensor conversion requested dtype %s for Tensor with dtype %s: %r" %
--> 983 (dtype.name, t.dtype.name, str(t)))
984 return t
ValueError: Tensor conversion requested dtype string for Tensor with dtype uint8: 'Tensor("DecodeBmp:0", shape=(?, ?, 1), dtype=uint8)'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-18-9b7aeb9e42de> in <module>
----> 1 file = tf.write_file(file_name,final)
D:\Users\ge3f-P2\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_io_ops.py in write_file(filename, contents, name)
2256 if _ctx is None or not _ctx._eager_context.is_eager:
2257 _, _, _op = _op_def_lib._apply_op_helper(
-> 2258 "WriteFile", filename=filename, contents=contents, name=name)
2259 return _op
2260 _result = None
D:\Users\ge3f-P2\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
531 if input_arg.type != types_pb2.DT_INVALID:
532 raise TypeError("%s expected type of %s." %
--> 533 (prefix, dtypes.as_dtype(input_arg.type).name))
534 else:
535 # Update the maps with the default, if needed.
TypeError: Input 'contents' of 'WriteFile' Op has type uint8 that does not match expected type of string.
The problem is I can't find a "encode_bmp" or any bmp related function that can be used to encode the image and save the resized image to disk.
I went through this thread but this doesn't help solve the question. Link here
Upvotes: 0
Views: 669
Reputation: 2864
Since Tensorflow currently does not have a native way of saving/encoding images to the BMP format, one way to solve this would be to save the image as a PNG in a temporary location and then use the Python Imaging Library to convert it to a BMP.
See: PILs Image.Save method and the list of supported file formats.
From my understanding, the reason for the exception you are receiving is that you are trying to save a unit8
tensor, when the write_file
method expects an - encoded - string.
Try this:
from PIL import Image
.
.
.
file_name = "D:/tmp.png"
enc = tf.image.encode_png(img_res)
file = tf.write_file(file_name, enc)
print('PNG Image Saved')
session.close()
Image.open(file_name).save("D:/out.bmp")
os.remove(file_name)
Upvotes: 1