0

I have code:

def get_image_array(data):
    fig = plt.figure(figsize=(1.5, 1.5), dpi=100)
    plt.plot(data, '-o', c='r')
    plt.axis('off')
    fig.canvas.draw()

    img_arr = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
    img_arr = img_arr.reshape(fig.canvas.get_width_height()[::-1] + (3,)).astype(np.float32)
    img_arr = img_arr[None, :]
    plt.close()

    return img_arr

where I create figure with figure size 150x150 (RGB), convert it to numpy array for using in CNN model. I launch it on FullHD monitor and it works, but it doesn't work on 2k, 4k monitors. The erros is:

ValueError: cannot reshape array of size 270000 into shape (150,150,3)

How can I create figure with figure size 150x150 for different types of resolution?

1 Answer 1

1

Reshape just reconfigures the existing information so if you have 67500 data points (150*150*3) you can have that as one long array or you can nest it as (150,150,3) or (22500,3) and so on. However if you want to go to bigger resolutions you'd need more information than you've got available and so you need to make something up.

Like you could double the size of a pixel to cover 4 pixels instead of 1 or you can copy the array multiple times or you can pad it with 0s or other values. There are functions like numpy.resize but you should check beforehand what you actually want it to be like.

Also usually the other way around is easier, that is you start with a high definition image and then resize it to become smaller. In that case you lose information in rescaling and so you don't have to make something up.

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

2 Comments

thank you for your answer! good idea with creating HD image and resize it. do you mean changing DPI value in figure and then just resize it?
Yes taking an image in 3840x2160 (or 4K) and then downscale it to 150,150 with resize. Also don't upscale it to downscale it as that kinda defeats the purpose :D Downscaling a wallpaper to an icon looks good, upscaling an icon to a wallpaper not so much. Also DPI or dots per inch is more of a printer parameter as your digital images rather work with pixels or pixels per inch for real life monitors.

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.