opencvimage-processingfiltercomputer-visionxerox

Filter to create Xerox/Photocopy image from Color images of documents


I have color images of Documents. Sample Input

PAN card (taken from https://www.indiamart.com/proddetail/pan-card-21687536812.html)

I wish to create realistic xerox/photocopy image from this. Most xerox/photocopy are set on low tone. Here is a sample output (sample output is not xerox of sample input - I couldnt find the same image for both on net) enter image description here (taken from http://shrikantmail7862.blogspot.com/2016/06/)

Note output is not a simple Black and White of the color image.

I am looking for a tools/code/algos that will do this for me. I need to do this for over 0.1M images I am sure there must be such a filter in image processing. Looking for suitable pointers


Solution

  • To create a xerox like effect, an approach would be to convert the image to greyscale and then reduce the color space.

    def xeroxFilter(imgPath, colorSpaceReduction=8, rotate=False, fillColor=(255,255,255)):
      '''Takes image as input and returns a xerox like image of the input image.
      impPath: Name of Image(keep the images in the same folder as this script)
      colorSpaceReduction:  Reducing the color space. Higher the value, Higher the reduction. 
      rotate: Rotate to the image
      fillColor: Specify the RGB value of the background color after roatation
      '''
      print("Original Image")
      cv2_imshow(cv2.imread(imgPath))
    
      print('GreyScale')
      cv2_imshow(cv2.imread(imgPath, cv2.IMREAD_GRAYSCALE))
      
      print('Color Space Reduced')
      color_reduced_img = cv2.imread(imgPath, cv2.IMREAD_GRAYSCALE) // colorSpaceReduction * colorSpaceReduction + colorSpaceReduction // 2
      cv2_imshow(color_reduced_img)
      file_name = 'ColorSpaceReduced-'+imgPath.split('.')[0]+'.png'
      cv2.imwrite(file_name, color_reduced_img)
      
      if rotate:
        print("Rotated")
        img = Image.open(file_name)
        rgb_img = Image.new("RGBA", img.size)
        rgb_img.paste(img)
        rotated_img = rgb_img.rotate(random.randint(-25,25), expand = 1, fillcolor = fillColor)
        display(rotated_img)
        rotated_img.save('Rotated-'+file_name)
      print("Original Image")
      cv2_imshow(cv2.imread(imgPath))
    
    xeroxFilter('MCRV7.jpg', rotate=True)
    

    Color Space Reduction code (credits @elizer)
    The output when the first image is passed into the script can be found below:
    [Xerox Filter Output Imge1