how to make pycimg workable for tensorflow as PIL?


I am trying to preprocess images for tensorflow, however I just can't get the same result with PIL as is done by pycimg.

The idea is to remove backgroud shadows from the image by substracting the blured image of itself, as following.

With pycimg, substraction is successful, but can't save (???) with PIL, the result is not satisfying.

Quite confused and stuck here. Please help.

original image

original image

target image( done by pycimg)

target image

blured image

blured image

image done by PIL

image process by PIL

    from  pycimg  import  CImg  as  cimg
    from  tensorflow.keras.preprocessing.image  import  load_img,  img_to_array
    from  PIL  import  ImageFilter,  Image
    import  numpy  as  np

    #  %%  cimg  method
    img  =  cimg(r'bird.png')
    img_copy  =  cimg(r'bird.png')

    #  making  blured  image
    img_blur  =  cimg.blur(img_copy,  sigma=15)
    #  if  use  `img`  directly,  the  cimg.blur  changes  the  original  image  also,  that  makes  everything  black,  so  have  to  make  a  copy.

    img_diff  =  img.__sub__(img_blur)
    img_diff.display()'bird_diff.jpg')  #  this  works'bird_diff.png')  #  this  also  works'D:\test\bird_diff.png')  #  fails  and  gives  err  msg  of    `Process  finished  with  exit  code  -1073741819  (0xC0000005)`

Then I turn back to PIL, with GaussianBlur, I can do the same process, but the result is quite different and of no use to me.

    #  %%
    img_pil  =  load_img(r'bird.png')
    img_pil_blur  =  img_pil.filter(ImageFilter.GaussianBlur(radius=15))

    img_sub  =  np.asarray(img_pil)  -  np.asarray(img_pil_blur)
    img_sub_arr  =  Image.fromarray(img_sub)

Here comes few questoions.

  1. How can I make the same result by PIL as it is done by pycimg? for tensorflow seemedly take PIL as default, don't know if it works with pycimg.

  2. if PIL is not working, how can I change the pycimg format to PIL format? the format is quite different. have tried reshape & move.axis.

Many thanks.

asked on Stack Overflow May 4, 2020 by Grec001

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0