I wanted to combine a py_function inside a map function, which took me a day, despite chatGPT's assistance.
Since resizing an image with tf.image has implementation differences in relate to openCVs, I wanted to keep using the optimized tf.Dataset with the .map API, but also combine the opencv.resize API.
Here's what worked for me:
In general, the resize_fn is called from the tensorflow map API
A short explanation: The cv2.resize drops the channels dimension for grayscale images so you can also neglect the np.squeeze command and just stay with the tf.expand_dims to return the image as a tensor with the channels dimension. In addition, the image.shape and the image.set_shape just make sure that the channels' dimension is kept, but they aren't mandatory here.
Hope it will help others.