krasserm / super-resolution

Tensorflow 2.x based implementation of EDSR, WDSR and SRGAN for single image super-resolution
Apache License 2.0
1.49k stars 352 forks source link

How to save result as an image file? #65

Open akkara opened 3 years ago

krasserm commented 3 years ago

@akkara what did you try and what did not work?

CaptainStabs commented 3 years ago

I've tried the demo code, after fixing missing imports, but don't see any code that actually outputs the images.

krasserm commented 3 years ago

Which demo code and which imports are missing. All demo notebooks should run without any modifications required.

Udanita commented 3 years ago

first of all import the tensorflow as tf: import tensorflow as tf

after "sr = resolve_single(model, lr)" you can get an image from the result and save to a path like this: pil_img = tf.keras.preprocessing.image.array_to_img(sr) pil_img.save('desired path')

rfmiotto commented 3 years ago

I will leave a code here as an example, from which it is possible to execute the method and save the output image. Make sure to run this code inside the root dir of this project.

import os
import matplotlib.pyplot as plt

from data import DIV2K
from model.edsr import edsr

from utils import load_image
from model import resolve_single

lr_image_path = 'demo/0869x4-crop.png'

# Number of residual blocks
depth = 16

# Super-resolution factor
scale = 4

# Downgrade operator
downgrade = 'bicubic'

weights_dir = f'weights/edsr-{depth}-x{scale}'
weights_file = os.path.join(weights_dir, 'weights.h5')

model = edsr(scale=scale, num_res_blocks=depth)
model.load_weights(weights_file)

lr = load_image(lr_image_path)
sr = resolve_single(model, lr)

fig = plt.figure()
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
plt.imshow(sr)
plt.savefig('sr_output.png', dpi=300, bbox_inches='tight', pad_inches=0)

You can set a different dpi value in order to change the size of the output image