Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ArthurDelannoyazerty/83b59aba51deb70e4679b025443d5767 to your computer and use it in GitHub Desktop.
Save ArthurDelannoyazerty/83b59aba51deb70e4679b025443d5767 to your computer and use it in GitHub Desktop.
Python functions that return the dpi of the main screen, and the x/y size of the image in inch in order to resize the plot automatically.
import ctypes
import tkinter
def get_dpi():
ctypes.windll.shcore.SetProcessDpiAwareness(1)
root = tkinter.Tk()
dpi = ctypes.windll.user32.GetDpiForWindow(root.winfo_id())
root.destroy()
return dpi
def get_inches_plot(array):
dpi = get_dpi()
return array.shape[1]/dpi, array.shape[0]/dpi
@ArthurDelannoyazerty
Copy link
Author

Example

quality:float = 2
fig = plt.figure()
fig.set_size_inches(get_inches_plot(img))             <---
plt.axis('off')
plt.imshow(img)
plt.savefig(filepath, dpi=get_dpi()*quality)          <---
plt.show()

quality is the detail of the image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment