Replies: 8 comments 5 replies
-
When you say 'remove', do you mean 'trim so the image is smaller', And you're interested in affecting the top, left and bottom lines? Not just all red pixels? |
Beta Was this translation helpful? Give feedback.
-
Replacing with white is fine. |
Beta Was this translation helpful? Give feedback.
-
from PIL import Image, ImageDraw
with Image.open("input.png") as im:
processed = []
rects = []
x, y = 0, 0
while y < im.height:
while x < im.width:
def matched(x3, y3):
r, g, b, a = im.getpixel((x3, y3))
if (r > 40 or g > 40 or b > 40) and (r < 240 or g < 240 or b < 240) and a == 255:
# Opaque and not black or white
return True
if matched(x, y):
x1, y1, x2, y2 = x, y, x, y
# Search right
while matched(x2, y2):
x2 += 1
x2 -= 1
# Search up
while True:
lineMatches = True
for x3 in range(x1, x2+1):
if not matched(x3, y1):
lineMatches = False
break
if not lineMatches:
y1 += 1
break
y1 -= 1
# Search down
while True:
lineMatches = True
for x3 in range(x1, x2+1):
if not matched(x3, y2):
lineMatches = False
break
if not lineMatches:
y2 -= 1
break
y2 += 1
if x2 - x1 > 5 and y2 - y1 > 5 and [x1, y1, x2, y2] not in rects:
rects.append([x1, y1, x2, y2])
x = x2
x += 1
x = 0
y += 1
d = ImageDraw.Draw(im)
for rect in rects:
d.rectangle(rect, "#fff")
im.save("out.png") |
Beta Was this translation helpful? Give feedback.
-
Thank you. When I try another image, it throws an error below.
|
Beta Was this translation helpful? Give feedback.
-
Thank you. |
Beta Was this translation helpful? Give feedback.
-
It is difficult to adjust.
|
Beta Was this translation helpful? Give feedback.
-
I want to remove all long lines which are not a part of text |
Beta Was this translation helpful? Give feedback.
-
Ok. Thank you. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
All reactions