<p>For this specific task and if you don't care about how the method can be extended to other cases, the following python code does the job:</p>
<pre class="lang-python prettyprint-override"><code>import numpy as np
import matplotlib.pyplot as plt
import sys

if __name__ == &quot;__main__&quot;:
    img = plt.imread(sys.argv[1])

    img = img.astype(int)       # type conversion from uint8 to int
    # given that the spiral structure mostly consists of pixel with 0 value
    img += 120
    img = np.clip(img, 0, 255).astype(np.uint8)      # clip to valid image range and convert the int type img back to uint8
    plt.imsave(&quot;grayscale-120.jpg&quot;, img)
</code></pre>
<p>Note that, the original image does not just contain pixel value of 255 and 0, there are intermediate values. The code above does not get rid of the intermediate values (though, it is negligible). But if you really want to get rid of them, you can use:</p>
<pre class="lang-python prettyprint-override"><code>img[img &gt; 120] = 255
</code></pre>
<p>as a thresholded pixel value substitution before saving the image. This will yield images will exactly two pixel values (120 and 255). But... your image is  of jpeg format, so you will notice some compression artifacts and the noise that hardly noticable before is enhanced by hard thresholding. Yet, you can use opening operation to get rid of the artifacts, right before you save the image:</p>
<pre class="lang-python prettyprint-override"><code>import cv2
kernel = np.ones((3, 3),np.uint8)
img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
</code></pre>
<p>well... this is not perfect, but I am sure more tweaking can be done here.</p>
<div class="s-table-container"><table class="s-table">
<thead>
<tr>
<th style="text-align: center;">no hard thresholding</th>
<th style="text-align: center;">hard thresholding</th>
<th style="text-align: center;">with opening op</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center;"><a href="https://i.sstatic.net/yRba5.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/yRba5.png" alt="enter image description here" /></a></td>
<td style="text-align: center;"><a href="https://i.sstatic.net/5lbBu.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/5lbBu.png" alt="enter image description here" /></a></td>
<td style="text-align: center;"><a href="https://i.sstatic.net/vXthF.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/vXthF.png" alt="enter image description here" /></a></td>
</tr>
</tbody>
</table></div>

CC BY-SA 4.0
2024-03-17T03:13:39.077Z
2024-03-17T03:13:39.077Z
Enigmatisms
<p>I am trying to get the following image to a certain grayscale value <a href="https://i.sstatic.net/50gkN.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/50gkN.jpg" alt="enter image description here" /></a></p>
<p>I want the spiral to have a grayscale of 120 and everything else to have a grayscale value of 255.
What can I do?</p>

<image-processing><image>
How Can I convert Grayscale values of an image?
Henry Evans
# How Can I convert Grayscale values of an image?

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

I am trying to get the following image to a certain grayscale value [![enter image description here](https://i.sstatic.net/50gkN.jpg)](https://i.sstatic.net/50gkN.jpg)

I want the spiral to have a grayscale of 120 and everything else to have a grayscale value of 255.
What can I do?

**Tags:** image-processing, image


---

## 1 answers

### Answer by username_1 on 2024-03-17. Score: 2

For this specific task and if you don't care about how the method can be extended to other cases, the following python code does the job:

```python
import numpy as np
import matplotlib.pyplot as plt
import sys

if __name__ == "__main__":
    img = plt.imread(sys.argv[1])

    img = img.astype(int)       # type conversion from uint8 to int
    # given that the spiral structure mostly consists of pixel with 0 value
    img += 120
    img = np.clip(img, 0, 255).astype(np.uint8)      # clip to valid image range and convert the int type img back to uint8
    plt.imsave("grayscale-120.jpg", img)

```

Note that, the original image does not just contain pixel value of 255 and 0, there are intermediate values. The code above does not get rid of the intermediate values (though, it is negligible). But if you really want to get rid of them, you can use:

```python
img[img > 120] = 255

```

as a thresholded pixel value substitution before saving the image. This will yield images will exactly two pixel values (120 and 255). But... your image is of jpeg format, so you will notice some compression artifacts and the noise that hardly noticable before is enhanced by hard thresholding. Yet, you can use opening operation to get rid of the artifacts, right before you save the image:

```python
import cv2
kernel = np.ones((3, 3),np.uint8)
img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

```

well... this is not perfect, but I am sure more tweaking can be done here.

| no hard thresholding | hard thresholding | with opening op |
| --- | --- | --- |
| [enter image description here](https://i.sstatic.net/yRba5.png) | [enter image description here](https://i.sstatic.net/5lbBu.png) | [enter image description here](https://i.sstatic.net/vXthF.png) |


---

