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
target image( done by pycimg
)
blured image
image done 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')
img.show()
# 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()
img_diff.save(r'bird_diff.jpg') # this works
img_blur.save(r'bird_diff.png') # this also works
img_diff.save(r'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)
img_sub_arr.show()
Here comes few questoions.
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.
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.
User contributions licensed under CC BY-SA 3.0