title: modify image to black text on white background
body: <p>I have an image that need to do OCR (Optical Character Recognition) to extract all data.</p>
  <p><a href="https://i.stack.imgur.com/rqDRe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rqDRe.png" alt="enter image description here" /></a></p>
  <p>First I want to convert color image to black text on white background in order to improve OCR  accuracy.</p>
  <p>I try below code</p>
  <pre><code>from PIL import Image
  img = Image.open(&quot;data7.png&quot;)
  img.convert(&quot;1&quot;).save(&quot;result.jpg&quot;)
  </code></pre>
  <p>it gave me below unclear image</p>
  <p><a href="https://i.stack.imgur.com/3AzHd.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3AzHd.jpg" alt="enter image description here" /></a></p>
  <p>I expect to have this image</p>
  <p><a href="https://i.stack.imgur.com/DsKaZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DsKaZ.png" alt="enter image description here" /></a></p>
  <p>Then, I will use pytesseract to get a dataframe</p>
  <pre><code>import pytesseract as tess
  file = Image.open(&quot;data7.png&quot;)
  text = tess.image_to_data(file,lang=&quot;eng&quot;,output_type='data.frame')
  text
  </code></pre>
  <p>Finally,the dataframe I want to get like below</p>
  <p><a href="https://i.stack.imgur.com/cCBYt.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cCBYt.jpg" alt="enter image description here" /></a></p>