Remove black borders on images with watermarks in Python

2024/7/2 14:57:11

I have a bunch of image I would like to uniformise by removing black borders. Usually I use the Trim function of Imagemagick with the fuzz parameters but in the case the image have some watermark the result is not here.

Actually I'm making some tests with opencv and morphological transform to try to identify watermark and image and then select the bigger element but I'm really new with opencv and I struggle.

Watermark can be everywhere, from bottom left to upper right.

black borders with watermak 1 black borders with watermak 2 black borders with watermak 3

I would prefer a Python code but using some app like Imagemagick or similar is welcome.

Actually using opencv only I get this result:

import copy
import cv2from matplotlib import pyplot as pltIMG_IN = '/data/black_borders/island.jpg'# keep a copy of original image
original = cv2.imread(IMG_IN)# Read the image, convert it into grayscale, and make in binary image for threshold value of 1.
img = cv2.imread(IMG_IN,0)# use binary threshold, all pixel that are beyond 3 are made white
_, thresh_original = cv2.threshold(img, 3, 255, cv2.THRESH_BINARY)# Now find contours in it.
thresh = copy.copy(thresh_original)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)# get contours with highest height
lst_contours = []
for cnt in contours:ctr = cv2.boundingRect(cnt)lst_contours.append(ctr)
x,y,w,h = sorted(lst_contours, key=lambda coef: coef[3])[-1]# draw contours
ctr = copy.copy(original)
cv2.rectangle(ctr, (x,y),(x+w,y+h),(0,255,0),2)# display results with matplotlib# original
original = original[:,:,::-1] # flip color for maptolib display
plt.subplot(221), plt.imshow(original)
plt.title('Original Image'), plt.xticks([]),plt.yticks([])# Threshold
plt.subplot(222), plt.imshow(thresh_original, cmap='gray')
plt.title('threshold binary'), plt.xticks([]),plt.yticks([])# selected area for future crop
ctr = ctr[:,:,::-1] # flip color for maptolib display
plt.subplot(223), plt.imshow(ctr)
plt.title('Selected area'), plt.xticks([]),plt.yticks([])plt.show()

results:

enter image description here

Answer

To remove black borders:-

Follow this link(Perfect Answer I think) :-
Crop black edges with OpenCV

To remove black border by specifying region, follow this link
How to crop an image in OpenCV using Python

Instead of cropping any part from image, you may take only ROI (Region of Interest). To do this, follow this link,
How to copy a image region using opencv in python?

To remove watermark:-

If watermark may appear anywhere in your image means, you cannot clear watermark fully. Just you may apply blurring effect on that image. It will blur your watermark.
Its link :
https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_filtering/py_filtering.html


If watermark will exist only on the black border means, the above mentioned methods will solve your problem.

https://en.xdnf.cn/q/73393.html

Related Q&A

scipy cdist with sparse matrices

I need to calculate the distances between two sets of vectors, source_matrix and target_matrix.I have the following line, when both source_matrix and target_matrix are of type scipy.sparse.csr.csr_matr…

NumPy arrays with SQLite

The most common SQLite interface Ive seen in Python is sqlite3, but is there anything that works well with NumPy arrays or recarrays? By that I mean one that recognizes data types and does not requir…

Binary Phase Shift Keying in Python

Im currently working on some code to transmit messages/files/and other data over lasers using audio transformation. My current code uses the hexlify function from the binascii module in python to conve…

Django. Listing files from a static folder

One seemingly basic thing that Im having trouble with is rendering a simple list of static files (say the contents of a single repository directory on my server) as a list of links. Whether this is sec…

inconsistent migration history when changing a django apps name

Im trying to rename one of the apps in my django website. There is another app which depends on it and its mysql tables. I went over all the files in both apps and changed the instances of the old name…

Tensorflow vs Numpy math functions

Is there any real difference between the math functions performed by numpy and tensorflow. For example, exponential function, or the max function? The only difference I noticed is that tensorflow take…

PyQt5: I cant understand QGraphicsScenes setSceneRect(x, y, w, h)

I see some people say if you want to put QGraphicsScenes origin of coordinates at the origin of QGraphicsView, i.e. top-left corner. You need to let both of them have the same size.So here is what I do…

Remove first character from string Django template

I know this has been asked multiple times but the solution that everyone reaches (and the documentation) doesnt seem to be working for me...Trying to remove first characterCode is {{ picture.picture_pa…

Prevent Python logger from printing to console

Im getting mad at the logging module from Python, because I really have no idea anymore why the logger is printing out the logging messages to the console (on the DEBUG level, even though I set my File…

How to remove python assertion when compiling in cython?

so, here is my problem: I code in python, but I need to improve performance in some part of my code that are too slow. A good(and easy) solution seems to be using cython; I tried it and got good result…