Crappify resizing Black and White image

Hi Team,

I am trying to crappify black and white images, however, with the current set of crappification code, it gets converted to RGB format and then gives issue while calculating SSIM (color channel mismatch). I tried changing the conversion to black and white by giving “L” and “1” and changing BILINEAR to NEAREST as a parameter in the following piece of code, but it gave me an error which you may find the later screenshot attached.

img = img.resize(targ_sz, resample=PIL.Image.BILINEAR).convert(‘RGB’)

Crappifying code: https://imgur.com/CFKDHO3 (gets executed successfully)

Modified Crappifying code for B&W image with the error message: https://imgur.com/79CZHKL

Kindly suggest how to fix it.

Code:

from PIL import Image, ImageDraw, ImageFont

class crappifier(object):

def __init__(self, path_lr, path_hr):
    self.path_lr = path_lr
    self.path_hr = path_hr              

def __call__(self, fn, i):       
    dest = self.path_lr/fn.relative_to(self.path_hr)    
    dest.parent.mkdir(parents=True, exist_ok=True)
    img = PIL.Image.open(fn)
    targ_sz = resize_to(img, 96, use_min=True)
    img = img.resize(targ_sz, resample=PIL.Image.NEAREST).convert('L')
    w,h = img.size
    q = random.randint(10,70)
    ImageDraw.Draw(img).text((random.randint(0,w//2),random.randint(0,h//2)), str(q), fill=(255,255,255))
    img.save(dest, quality=q)

il = ImageList.from_folder(path_hr)
parallel(crappifier(path_lr, path_hr), il.items)

Error:

Traceback (most recent call last):
File “/usr/lib/python3.6/concurrent/futures/process.py”, line 175, in _process_worker
r = call_item.fn(*call_item.args, **call_item.kwargs)
File “”, line 16, in call
ImageDraw.Draw(img).text((random.randint(0,w//2),random.randint(0,h//2)), str(q), fill=(255,255,255))
File “/usr/local/lib/python3.6/dist-packages/PIL/ImageDraw.py”, line 337, in text
ink = getink(fill)
File “/usr/local/lib/python3.6/dist-packages/PIL/ImageDraw.py”, line 300, in getink
ink, fill = self._getink(fill)
File “/usr/local/lib/python3.6/dist-packages/PIL/ImageDraw.py”, line 113, in _getink
ink = self.draw.draw_ink(ink)
TypeError: function takes exactly 1 argument (3 given)
“”"

The above exception was the direct cause of the following exception:

TypeError Traceback (most recent call last)
in ()
1 il = ImageList.from_folder(path_hr)
----> 2 parallel(crappifier(path_lr, path_hr), il.items)

2 frames
/usr/local/lib/python3.6/dist-packages/fastai/core.py in parallel(func, arr, max_workers, leave)
361 results = []
362 for f in progress_bar(concurrent.futures.as_completed(futures), total=len(arr), leave=leave):
–> 363 results.append(f.result())
364 if any([o is not None for o in results]): return results
365

/usr/lib/python3.6/concurrent/futures/_base.py in result(self, timeout)
423 raise CancelledError()
424 elif self._state == FINISHED:
–> 425 return self.__get_result()
426
427 self._condition.wait(timeout)

/usr/lib/python3.6/concurrent/futures/_base.py in __get_result(self)
382 def __get_result(self):
383 if self._exception:
–> 384 raise self._exception
385 else:
386 return self._result

TypeError: function takes exactly 1 argument (3 given)