Removing white space around a saved image
I cannot claim I know exactly why or how my “solution” works, but this is what I had to do when I wanted to plot the outline of a couple of aerofoil sections — without white margins — to a PDF file.
(Note that I used matplotlib inside an IPython notebook, with the -pylab flag.)
plt.gca().set_axis_off()
plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0,
hspace = 0, wspace = 0)
plt.margins(0,0)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.savefig("filename.pdf", bbox_inches = 'tight',
pad_inches = 0)
I have tried to deactivate different parts of this, but this always lead to a white margin somewhere. You may even have modify this to keep fat lines near the limits of the figure from being shaved by the lack of margins.
Remove white space from an image using python
The code is almost perfect. It just can't crop on the right side because of the scrollbar. And it does not consider some padding (which you liked according the comments).
The thing I removed is the topology modification.
import numpy as np
import cv2
img = cv2.imread('cropwhitefromimage.png')
scrollbarright = 20
img = img[:, :-scrollbarright]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = 255*(gray < 128).astype(np.uint8)
coords = cv2.findNonZero(gray)
x, y, w, h = cv2.boundingRect(coords)
padding = 10
rect = img[y-padding:y+h+padding, x-padding:x+w+padding]
cv2.imshow("Cropped", rect)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("cropwhitefromimage_result.png", rect)
How to remove whitespace from an image in Python?
I think this is what you want - a kind of double-matted surround:
#!/usr/bin/env python3
from PIL import Image, ImageDraw, ImageOps
# Open input image
im = Image.open('zHZB9.png')
# Get rid of existing black border by flood-filling with white from top-left corner
ImageDraw.floodfill(im,xy=(0,0),value=(255,255,255),thresh=10)
# Get bounding box of text and trim to it
bbox = ImageOps.invert(im).getbbox()
trimmed = im.crop(bbox)
# Add new white border, then new black, then new white border
res = ImageOps.expand(trimmed, border=10, fill=(255,255,255))
res = ImageOps.expand(res, border=5, fill=(0,0,0))
res = ImageOps.expand(res, border=5, fill=(255,255,255))
res.save('result.png')
Removing white space around images with Tkinter
You can use the option borderwidth
to change the width of the border around the images.
img2 = tk.PhotoImage(file='blue.png')
ree2 = tk.Label(root, image=img2, borderwidth=0)
img4 = tk.PhotoImage(file='green.png')
ree4 = tk.Label(root, image=img4, borderwidth=0)
Setting it to 0 will remove the whitespace you are seeing.
matplotlib.pyplot: remove white space and keep same size while saving the image
Arguably, matplotlib might not be the best tool to achieve this (although it's certainly possible). I propose the Python Image Library as a substitute. Using this library, you can save your image like this:
from PIL import Image
im = Image.fromarray(np.uint8(img))
im.save('outfile.png', "PNG")
Removing white space around a saved image
I cannot claim I know exactly why or how my “solution” works, but this is what I had to do when I wanted to plot the outline of a couple of aerofoil sections — without white margins — to a PDF file.
(Note that I used matplotlib inside an IPython notebook, with the -pylab flag.)
plt.gca().set_axis_off()
plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0,
hspace = 0, wspace = 0)
plt.margins(0,0)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.savefig("filename.pdf", bbox_inches = 'tight',
pad_inches = 0)
I have tried to deactivate different parts of this, but this always lead to a white margin somewhere. You may even have modify this to keep fat lines near the limits of the figure from being shaved by the lack of margins.
Related Topics
Winerror 10049: the Requested Address Is Not Valid in Its Context
Python - Outputting Variables to Txt File
Tkinter Ttk Treeview How to Set Fixed Width Why It Change With Number of Column
Turn the Column Headers into the First Row and Row Headers into the First Column in Pandas Dataframe
Python: How to Split a List Based on a Specific Element
I Want to Multiply Two Columns in a Pandas Dataframe and Add the Result into a New Column
Subtract a Value from Every Number in a List in Python
How to Merge Json Objects Containing Arrays Using Python
Pandas.To_Sql Replace Old Data With New Data Based on 'Unique Id'
What Is the Correct Format to Write Float Value to File in Python
Replace Empty Strings With None/Null Values in Dataframe
How to Change the Foreground or Background Colour of a Tkinter Button on MAC Os X
Change a Colour of a Pixel in Python
Python Login Script; Usernames and Passwords in a Separate File
Find Matching Rows in 2 Dimensional Numpy Array