[Python] How to render pixel data from array in Jupyter notebook sharp when scaling is enabled?

Discussão em 'Python' iniciado por Stack, Junho 3, 2025.

  1. Stack

    Stack Membro Participativo

    I want to display an image from an array in a Jupyter notebook (inline) in Visual Studio Code. I'm running Windows 11 on a high DPI monitor with scaling set to 150%. Pixels don't render sharp in the notebook. I want integer scaling for my displayed image. So pixels should be rendered 100% (1x1 pixels), 200% (2x2 pixels), etc. I tried matplotlib and pillow. I can't get it sharp. Here is my python code:

    from PIL import Image
    from IPython.display import display
    import numpy as np

    bw_data = np.array([
    [0, 1, 0, 1],
    [1, 0, 1, 0],
    [0, 1, 0, 1],
    [1, 0, 1, 0]
    ], dtype=np.uint8)

    # mode '1' = 1-bit black/white
    bw_image = Image.fromarray(bw_data * 255).convert(mode='1')

    display(bw_image)


    output: [​IMG]

    This is not sharp.

    Saving the image does produce a sharp image:

    bw_image.save('image.png')


    Result: [​IMG]

    I use iconico magnifier V2.4 (with scaling disabled on executable) to inspect the rendered images on pixel level. And using image editors such as Gimp or paint to inspect saved images (since image viewers use upscaling when zooming in).

    I prefer not to write the image to storage first, but keep it in RAM.

    Continue reading...

Compartilhe esta Página