<p>You can do this with <a href="https://github.com/madmaze/pytesseract" rel="nofollow noreferrer">pytesseract</a> and <a href="https://pillow.readthedocs.io/en/stable/" rel="nofollow noreferrer">Pillow</a>.</p>
<p>Assuming you have Python on your system, install <a href="https://github.com/tesseract-ocr/tesseract" rel="nofollow noreferrer">Tesseract</a>, then pip-install <a href="https://github.com/madmaze/pytesseract" rel="nofollow noreferrer">pytesseract</a> and <a href="https://pillow.readthedocs.io/en/stable/" rel="nofollow noreferrer">Pillow</a> and finaluse the following script:</p>
<pre class="lang-python prettyprint-override"><code>from pathlib import Path
import subprocess

from PIL import Image, ImageDraw, ImageFilter
import pytesseract

img_file = '/your/file/name.png'

# Open the img using PIL
img = Image.open(img_file)

results = pytesseract.image_to_data(img, lang=&quot;deu&quot;, output_type=pytesseract.Output.DICT)

img_copy = img.copy()

draw = ImageDraw.Draw(img) # Create a drawing object

confidence_threshold = 60

for i, text in enumerate(results['text']):
    if text.strip() and int(results['conf'][i]) &gt; 60:  # Filter out low-confidence results
        x = results['left'][i]-2 # bounding box is on the left too tight
        y = results['top'][i]
        w = results['width'][i]+2
        h = results['height'][i]

        # Blur image
        cropped_area = img_copy.crop((x, y, x + w, y + h))

        # Set blur size
        (g_blur, b_blur) = (6, 11)
        blurred_area = cropped_area \
                          .filter(ImageFilter.GaussianBlur(radius=g_blur)) \
                          .filter(ImageFilter.BoxBlur(radius=b_blur))
        img.paste(blurred_area, (x,y))

        # Draw rectangle
        #draw.rounded_rectangle([x, y, x + w, y + h], radius=10, outline=&quot;red&quot;, width=2)

        # Draw confidence text
        #conf_text = f&quot;{results['conf'][i]}% {text}&quot;
        #draw.text((x, (2*y + h) // 2), conf_text, fill=&quot;black&quot;, font_size=20)

# Save  the image
ifp = Path(img_file)
out_img = ifp.parent / f&quot;{ifp.stem}_blurred{ifp.suffix}&quot;
img.save(out_img)
</code></pre>
<p><strong>Explanation of the script</strong></p>
<p>First use pytesseract to identify text in the image and give back a data structure with the text boxes:</p>
<pre><code>results = pytesseract.image_to_data(img, lang=&quot;deu&quot;, output_type=pytesseract.Output.DICT)
</code></pre>
<p>then iterate over these boxes:</p>
<pre><code>for i, text in enumerate(results['text']):
</code></pre>
<p>and for each box blur the text with Pillow:</p>
<pre><code>cropped_area = img_copy.crop((x, y, x + w, y + h))
(g_blur, b_blur) = (6, 11)
blurred_area = cropped_area \
    .filter(ImageFilter.GaussianBlur(radius=g_blur)) \
    .filter(ImageFilter.BoxBlur(radius=b_blur))
img.paste(blurred_area, (x,y))
</code></pre>

CC BY-SA 4.0
2024-10-27T03:02:35.393Z
2024-10-27T03:02:35.393Z
halloleo
<p>I have a Mind Map image (PNG file) in which I want to blur <em>programmatically</em> all text, so that the letters are unreadable. All other elements of the Mind Map should remain crisp and clear.</p>
<p>A sample mind map looks like this:</p>
<p><a href="https://i.sstatic.net/82Qb342T.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/82Qb342T.png" alt="Mind Map" /></a></p>
<p>To achieve this task I need to find a <em>programmatic</em> way to</p>
<ul>
<li>identify the smallest areas (rectangles) containing continues text in the mind map image.</li>
</ul>
<p><strong><em>What is a good tool to do so?</em></strong></p>
<p>Side note: Once I have detected these areas (e.g. as a list of rectangles) the blurring itself is easily done (for example with an ImageMagick script).</p>

CC BY-SA 4.0
2024-10-06T13:15:47.987Z
This question is extremely broad. Find text in a pdf or png figure out its size and blur using some method without blurring anything around the text. Is how I read this. Please try to narrow it down.
pmw1234
CC BY-SA 4.0
2024-10-10T08:33:34.233Z
@pmw1234 This is _not_ extremely broad, but rather exactly what you say: Find text in a pdf or png, figure out its size and blur using some method without blurring anything around the text.
halloleo
CC BY-SA 4.0
2024-10-10T13:23:15.900Z
Here is an answer that it as the same level as this question: Modify the pdf to blur the text. Generate the output which will have the blurred text.
pmw1234
CC BY-SA 4.0
2024-10-11T03:34:18.303Z
@pmw1234 haha. I get your point: I need to be a bit more specific! I have updated the question accordingly.
halloleo
CC BY-SA 4.0
2024-10-11T13:41:33.883Z
Given the updated info, this probably isn't the place to ask this question. The computer graphics stack exchange is all about algorithms such as writing code and doing the math to actually detect the regions, then writing a shader to do the blur using languages like glsl or hlsl. I'm not sure which stack exchange would be a good fit or I would suggest one.
pmw1234
CC BY-SA 4.0
2024-10-11T15:20:02.867Z
I think to identify the text regions, the easiest way is to use neural networks... But @pmw1234 is right, cgse is not the right place to ask this
Thomas
<blur><edge-detection>
How can I detect all text in a Mind Map image in order top blur the text?
halloleo
# How can I detect all text in a Mind Map image in order top blur the text?

(Asked by: username_0 on 2024-10-03)

I have a Mind Map image (PNG file) in which I want to blur *programmatically* all text, so that the letters are unreadable. All other elements of the Mind Map should remain crisp and clear.

A sample mind map looks like this:

[![Mind Map](https://i.sstatic.net/82Qb342T.png)](https://i.sstatic.net/82Qb342T.png)

To achieve this task I need to find a *programmatic* way to

* identify the smallest areas (rectangles) containing continues text in the mind map image.

***What is a good tool to do so?***

Side note: Once I have detected these areas (e.g. as a list of rectangles) the blurring itself is easily done (for example with an ImageMagick script).

**Tags:** blur, edge-detection

> **Comments:**
> **username_1**: This question is extremely broad. Find text in a pdf or png figure out its size and blur using some method without blurring anything around the text. Is how I read this. Please try to narrow it down.  
> **username_0**: @username_1 This is _not_ extremely broad, but rather exactly what you say: Find text in a pdf or png, figure out its size and blur using some method without blurring anything around the text.  
> **username_1**: Here is an answer that it as the same level as this question: Modify the pdf to blur the text. Generate the output which will have the blurred text.  
> **username_0**: @username_1 haha. I get your point: I need to be a bit more specific! I have updated the question accordingly.  
> **username_1**: Given the updated info, this probably isn't the place to ask this question. The computer graphics stack exchange is all about algorithms such as writing code and doing the math to actually detect the regions, then writing a shader to do the blur using languages like glsl or hlsl. I'm not sure which stack exchange would be a good fit or I would suggest one.  
> **username_2**: I think to identify the text regions, the easiest way is to use neural networks... But @username_1 is right, cgse is not the right place to ask this  

---

## 1 answers

### Answer by username_0 on 2024-10-27. Score: 0

You can do this with [pytesseract](https://github.com/madmaze/pytesseract) and [Pillow](https://pillow.readthedocs.io/en/stable/).

Assuming you have Python on your system, install [Tesseract](https://github.com/tesseract-ocr/tesseract), then pip-install [pytesseract](https://github.com/madmaze/pytesseract) and [Pillow](https://pillow.readthedocs.io/en/stable/) and finaluse the following script:

```python
from pathlib import Path
import subprocess

from PIL import Image, ImageDraw, ImageFilter
import pytesseract

img_file = '/your/file/name.png'

# Open the img using PIL
img = Image.open(img_file)

results = pytesseract.image_to_data(img, lang="deu", output_type=pytesseract.Output.DICT)

img_copy = img.copy()

draw = ImageDraw.Draw(img) # Create a drawing object

confidence_threshold = 60

for i, text in enumerate(results['text']):
    if text.strip() and int(results['conf'][i]) > 60:  # Filter out low-confidence results
        x = results['left'][i]-2 # bounding box is on the left too tight
        y = results['top'][i]
        w = results['width'][i]+2
        h = results['height'][i]

        # Blur image
        cropped_area = img_copy.crop((x, y, x + w, y + h))

        # Set blur size
        (g_blur, b_blur) = (6, 11)
        blurred_area = cropped_area \
                          .filter(ImageFilter.GaussianBlur(radius=g_blur)) \
                          .filter(ImageFilter.BoxBlur(radius=b_blur))
        img.paste(blurred_area, (x,y))

        # Draw rectangle
        #draw.rounded_rectangle([x, y, x + w, y + h], radius=10, outline="red", width=2)

        # Draw confidence text
        #conf_text = f"{results['conf'][i]}% {text}"
        #draw.text((x, (2*y + h) // 2), conf_text, fill="black", font_size=20)

# Save  the image
ifp = Path(img_file)
out_img = ifp.parent / f"{ifp.stem}_blurred{ifp.suffix}"
img.save(out_img)

```

**Explanation of the script**

First use pytesseract to identify text in the image and give back a data structure with the text boxes:

```
results = pytesseract.image_to_data(img, lang="deu", output_type=pytesseract.Output.DICT)

```

then iterate over these boxes:

```
for i, text in enumerate(results['text']):

```

and for each box blur the text with Pillow:

```
cropped_area = img_copy.crop((x, y, x + w, y + h))
(g_blur, b_blur) = (6, 11)
blurred_area = cropped_area \
    .filter(ImageFilter.GaussianBlur(radius=g_blur)) \
    .filter(ImageFilter.BoxBlur(radius=b_blur))
img.paste(blurred_area, (x,y))

```


---

