question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Save image in original resolution

See original GitHub issue

Hi! I want to save my plot with the ScaleBar, here is my code:

fig, ax = plt.subplots()
ax.axis("off")
ax.imshow(my_slice3, cmap="gray")
scalebar = ScaleBar(650, "nm", length_fraction=0.25, location= "lower right")
ax.add_artist(scalebar)
plt.savefig('foo.png')

When I save the image, the resolution is not anymore the resolution of the original image. At Stack Overflow I found the hint, that one can use imsave , but that is not working with the Scalebar. https://stackoverflow.com/questions/31544130/saving-an-imshow-like-image-while-preserving-resolution I know that I can increase the resolution e.g. with plt.rcParams['figure.dpi'] = 300 . But that is not leading to the exact pixel-by-pixel image. Any idea how to solve this?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
kolibril13commented, Aug 10, 2021

Here is a solution I found:

  1. Find out display dpi here: https://dpi.lv/
  2. Run this script:
import matplotlib.pyplot as plt
import numpy as np

def export_figure_matplotlib(arr, f_name, dpi=200, resize_fact=1, plt_show=False):
    """
    Export array as figure in original resolution
    :param arr: array of image to save in original resolution
    :param f_name: name of file where to save figure
    :param resize_fact: resize facter wrt shape of arr, in (0, np.infty)
    :param dpi: dpi of your screen
    :param plt_show: show plot or not
    """
    fig = plt.figure(frameon=False)
    fig.set_size_inches(arr.shape[1]/dpi, arr.shape[0]/dpi)
    ax = plt.Axes(fig, [0., 0., 1., 1.])
    ax.set_axis_off()
    fig.add_axes(ax)
    ax.imshow(arr)
    scalebar = ScaleBar(650, "nm", length_fraction=0.25, location=  "lower right")
    ax.add_artist(scalebar)
    plt.savefig(f_name, dpi=(dpi * resize_fact))
    if plt_show:
        plt.show()
    else:
        plt.close()
        
export_figure_matplotlib(my_slice3, "hello2.pdf", dpi=298, resize_fact=1, plt_show=True)

Inspired by https://stackoverflow.com/a/51438809/6933377

1reaction
hakonanescommented, Aug 10, 2021

As these images have a resolution about 2000x2000 pixel, I don’t want to lose image quality when adding the scalebar.

Aha, the scalebar would not be pixelated, then, wrong assumption on my end…

As I am making the poster with a vector program, I can simply save the image as a pdf. The image is then a raster image, and the Scalebar is still in vector format.

I would increase the dots per inch (DPI), as you do, to ensure that you don’t loose much pixel resolution. Apart from that, I don’t know what else can help, unfortunately. It might be that just adding a custom scalebar in your program yourself is the simplest solution when you want to keep the exact image resolution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Save as Picture" saves images in low resolution
When using the right-click option Save as Picture to save an image from a Word document, the image is saved in very low...
Read more >
How do I save the image in the original resolution? - MathWorks
1. Download the "export_fig" function from the File Exchange. Here is the link: · 2. Extract the compressed folder and add it to...
Read more >
How to Save Internet Pictures in High Resolution - Techwalla
Open the picture in photo-editing software, and look at the image size. You will see the pixel dimensions listed (such as 800 by...
Read more >
How to download original image resolution - Super User
Right-click on the image, and select Save Image As... (or similar menu option). Share.
Read more >
How to Save High-Quality Images in Photoshop - MakeUseOf
Changing the resolution also increases the image's dimensions above. If you change your width and height back to the original image size, the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found