0

I have issue where I have transparent part of image, but I am not sure if it really is transparent because I can not see what is behind transparent part and it is look like there is nothing but under it should be another image, problem looks like this or like this. If I do not use masks for deleting black edges it looks like this

I want to make edges completely transparent or entirely delete them.

Thank you for your help. Have a nice day

this is how I load image

    trainImg = cv2.imread(fnames[x], cv2.IMREAD_UNCHANGED)
    trainImg_gray = cv2.cvtColor(trainImg, cv2.COLOR_RGB2GRAY)

    queryImg = cv2.imread(fnames[y], cv2.IMREAD_UNCHANGED)
    queryImg_gray = cv2.cvtColor(queryImg, cv2.COLOR_RGB2GRAY)

this is how I work with pictures

        height = trainImg.shape[0] + queryImg.shape[0]
        width = trainImg.shape[1] + queryImg.shape[1]
        dim = (trainImg.shape[1], trainImg.shape[0])
        print(fnames_cropped[x])

        trainImg = cv2.imread(fnames_cropped[x])
        (b, g, r) = cv2.split(trainImg)
        trainImg = cv2.merge([r, g, b])
        trainImg = cv2.resize(trainImg, dim)

        trainImg = trim(trainImg)

        dim = (queryImg.shape[1], queryImg.shape[0])
        print(fnames_cropped[y])
        queryImg = cv2.imread(fnames_cropped[y])
        (b, g, r) = cv2.split(queryImg)
        queryImg = cv2.merge([r, g, b])

        queryImg = cv2.resize(queryImg, dim)

        queryImg = trim(queryImg)

        # Make mask of black pixels - mask is True where image is black
        RGB = np.array(trainImg)
        h, w = RGB.shape[:2]

        # Add an alpha channel, fully opaque (255)
        RGBA = np.dstack((RGB, np.zeros((h, w), dtype=np.uint8) + 255))

        # Make mask of black pixels - mask is True where image is black
        mBlack = (RGBA[:, :, 0:3] == [0, 0, 0]).all(2)

        # Make all pixels matched by mask into transparent ones
        RGBA[mBlack] = (0, 0, 0, 0)

        trainImg = RGBA

        # Make mask of black pixels - mask is True where image is black
        RGB = np.array(queryImg)
        h, w = RGB.shape[:2]

        # Add an alpha channel, fully opaque (255)
        RGBA = np.dstack((RGB, np.zeros((h, w), dtype=np.uint8) + 255))

        # Make mask of black pixels - mask is True where image is black
        mBlack = (RGBA[:, :, 0:3] == [0, 0, 0]).all(2)

        # Make all pixels matched by mask into transparent ones
        RGBA[mBlack] = (0, 0, 0, 0)

        queryImg = RGBA

        result = cv2.warpPerspective(trainImg, H, (width, height), flags=cv2.INTER_NEAREST, borderMode=cv2.BORDER_CONSTANT)

        im = Image.fromarray(trim(result))


        result[0:queryImg.shape[0], 0:queryImg.shape[1]] = queryImg

        im = Image.fromarray(trim(result))
        im.save(feature_extractor+feature_matching+fnames_cropped[x]+fnames_cropped[y])
Community
  • 1
  • 1
RocK_st4r
  • 5
  • 1
  • 4
  • the image will be a rectangle, right. How do you want to represent nonimportant part of the image – spaceman Feb 21 '20 at 22:36
  • It should be again transparent, because I will be adding another images to those stitched images. – RocK_st4r Feb 21 '20 at 22:40
  • try to save image as png. – spaceman Feb 21 '20 at 22:45
  • I am saving it in PNG :/ – RocK_st4r Feb 21 '20 at 22:49
  • go through im.save function https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.save then different format an image can be saved https://pillow.readthedocs.io/en/3.1.x/handbook/image-file-formats.html – spaceman Feb 21 '20 at 22:56
  • im.save(feature_extractor+feature_matching+fnames_cropped[x]+fnames_cropped[y], format="PNG") still same result :/ – RocK_st4r Feb 21 '20 at 23:04
  • Please check the below links.... https://stackoverflow.com/questions/4379978/python-pil-how-to-make-area-transparent-in-png https://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio – spaceman Feb 21 '20 at 23:10

0 Answers0