0

I am trying to train a model with some noisy images having dithering.

What I have :

  1. clean pdfs with white background
  2. coloured pdfs(RGB) and grayscale pdfs (with 3 channels, RGB)

What I want:

  1. convert only white background (not text) into gray background, if possible only half page should be converted

  2. Add dithering to the gray background without loosing the text

what I tried:

import os

from PIL import Image
from numpy import asarray


ORIGIN_PATH = "/home/dithering/temp/"
DESTIN_PATH = "/home/dithering`enter code here`/temp_try/"
"""for filename in os.listdir(ORIGIN_PATH):
    img = Image.open(ORIGIN_PATH + filename).convert("L")
    rbg_grayscale_img = img.convert("RGB")
    rbg_grayscale_img.save(DESTIN_PATH + filename)"""


for filename in os.listdir(ORIGIN_PATH):

    img = Image.open(ORIGIN_PATH + filename).convert("L", dither=Image.Dither.FLOYDSTEINBERG)

    # convert image to nparray
    numpydata = asarray(img)
    numpydata[numpydata > 250] = 128

    # data
    print(numpydata)

    # convert array to image
    final_image = Image.fromarray(numpydata)

    # img show
    final_image.show()

    # img save
    final_image.save(DESTIN_PATH + filename)

I expect something like this, enter image description here

Any help would be appreciated, thanks in advance!

aarya
  • 83
  • 1
  • 8
  • generate an entirely "dithered"/noisy image (no content, just noise/dithering), the size of your subject image. get mask for background. use mask to copy dithering into subject image. – Christoph Rackwitz Jul 06 '22 at 08:49
  • thank you @ChristophRackwitz, could you please also write some lines of code wrt the above idea? – aarya Jul 06 '22 at 08:52
  • start by generating a flat gray PIL image ([`image.paste(color, box)`](https://stackoverflow.com/questions/20548590/python-imaging-library-can-i-simply-fill-my-image-with-one-color)), then converting it to `mode="1"`, which will dither it. – Christoph Rackwitz Jul 06 '22 at 09:11

0 Answers0