0

I want to remove the dark(black strips) and also the white curves in the image, and then align the remained parts connected in a new small-sized image, making the colored parts looks continuously. I hope can get some suggestions for solutions.

enter image description here

I have tried to use PIL to read the image. I don't know how to set the right threshold and resize the image

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
NoVel
  • 21
  • 7

2 Answers2

0

I'm not an expert at all in image processing, but let me know if this is enough for you.

Looking at the brightness (sum of the RGB values) distribution, maybe one option is to just filter pixel based on their value:

enter image description here

It looks like the dark parts have a brightness below 100 (or something like that). I filtered it this way:

from PIL import Image
import numpy as np

def filter_image(img,threshold=100):
    data = np.array(img.convert('RGB'))

    brightness = np.sum(data,axis=2)
    filtered_img = data.copy()*0

    for i in range(data.shape[0]):
        k = 0 # k index the columns that are bright enough
        for j in range(data.shape[1]):
            if brightness[i,j] > threshold:
                filtered_img[i,k,:] = data[i,j,:]
                k += 1 # we increment only if it's bright enough
        # End of column iterator. The right side of the image is black

    return Image.fromarray(filtered_img)

img = Image.open("test.png")
filtered = filter_image(img)
filtered.show()

I get the following result. I'm sure experts can do much better, but it's a start:

enter image description here

Nakor
  • 1,484
  • 2
  • 13
  • 23
-1

The following is only looking for black pixels, as can be seen by the first image, many of the pixels you want out are not black. You will need to find a way to scale up what you will take out.

Also, research will need to be done on collapsing an image, as can be seen by my collapse. Although, this image collapse may work if you are able to get rid of everything but the reddish colors. Or you can reduce by width, which is what the third picture shows.

from PIL import Image, ImageDraw


def main():
    picture = Image.open('/Volumes/Flashdrive/Stack_OverFlow/imageprocessing.png', 'r')
    # pix_val = list(im.getdata())
    # print(pix_val)
# https://code-maven.com/create-images-with-python-pil-pillowimg = Image.new('RGB', (100, 30), color = (73, 109, 137))
    blackcount = 0
    pix = picture.convert('RGB')  # https://stackoverflow.com/questions/11064786/get-pixels-rgb-using-pil
    width, height = picture.size
    img = Image.new('RGB', (width, height), color=(73, 109, 137))
    newpic = []
    for i in range(width):
        newpictemp = []
        for j in range(height):
            # https://stackoverflow.com/questions/13167269/changing-pixel-color-python
            r, g, b = pix.getpixel((i, j))
            if r == 0 and g == 0 and b == 0:
                blackcount += 1
            else:
                img.putpixel((i, j), (r, g, b))
                newpictemp.append((r, g, b))
        newpic.append(newpictemp)
    img.save('pil_text.png')
    newheight = int(((width * height) - blackcount) / width)
    print(newheight)
    img2 = Image.new('RGB', (width, newheight), color=(73, 109, 137))
    for i in range(width):
        for j in range(newheight):
            try:
                z = newpic[i][j]
                img2.putpixel((i, j), newpic[i][j])
            except:
                continue
    img2.save('pil_text2.png')


if __name__ == "__main__":
    main()

No black pixels on left, removed black pixels on right, remove and resize by width (height resize shown in code)

GeneralCode
  • 794
  • 1
  • 11
  • 26