Skip to content

Instantly share code, notes, and snippets.

@selflein
Last active February 1, 2020 13:00
Show Gist options
  • Save selflein/07e6cf7f745595ffba1808c33700950d to your computer and use it in GitHub Desktop.
Save selflein/07e6cf7f745595ffba1808c33700950d to your computer and use it in GitHub Desktop.
Save image with same resolution without border etc. using matplotlib #python #matplotlib
dpi = 80
# What size does the figure need to be in inches to fit the image?
figsize = width / float(dpi), height / float(dpi)
# Create a figure of the right size with one axes that takes up the full figure
fig = plt.figure(figsize=figsize)
ax = fig.add_axes([0, 0, 1, 1])
ax.set_aspect('equal', adjustable='box')
# Hide spines, ticks, etc.
ax.axis('off')
# Display the image.
ax.imshow(img)
# Annotate something etc.
# ...
fig.savefig('out.png', dpi=dpi)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment