Python Forum
Deleting White from Bitmap
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Deleting White from Bitmap
#1
I have found a lot of code that makes the white in a bitmap transparent. However, for my program I need the white to be removed completely. How do I specify this? I know I can delete a background completely, but sometimes my background color is black so that doesn't work. Any tips?
Reply
#2
Don't use bitmap. Use png. They have a transparency.

so convert your image to png. In gimp I can select background and hit delete. For a transparent background for png. Sometimes you have to add alpha to image. Bitmap don't support transparency.
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Okay, will I be able to convert it back to a bitmap after? I need it to be a bitmap when it is saved in its final state.
Reply
#4
using pillow for python 3.
import tkinter as tk
from PIL import ImageTk, Image

def filter_image(image, color):
    alpha_mask = Image.new('1', image.size)
    for y in range(image.size[1]):
        for x in range(image.size[0]):
            if image.getpixel((x, y)) == color:
                alpha_mask.putpixel((x,y), 0)
            else:
                alpha_mask.putpixel((x,y), 1)

    image.putalpha(alpha_mask)

class App:
    def __init__(self):
        self.root = tk.Tk()
        filename = "../Images/test.bmp"
        p_image = Image.open(filename)
        # grab topleft corner or input rgb color
        filter_image(p_image, p_image.getpixel((0,0)))
        self.image = ImageTk.PhotoImage(p_image)
        self.label = tk.Label(self.root, image = self.image)
        self.label.pack()

app = App()
app.root.mainloop()

Remember bitmaps do not hold transparent. You have to tell program what not to draw.
99 percent of computer problems exists between chair and keyboard.
Reply
#5
Okay, I tried doing this. I am not sure how to tell if it worked. I can't seem to save the bitmap. I had to change my code to look like this:

def filter_image(image, color):
    alpha_mask = Image.new('1', image.size)
    for y in range(image.size[1]):
        for x in range(image.size[0]):
            if image.getpixel((x, y)) == color:
                alpha_mask.putpixel((x, y), 0)
            else:
                alpha_mask.putpixel((x, y), 1)

    image.putalpha(alpha_mask)

p_image = Image.open('C:\\Users\\Erik\\Desktop\\EditedResize.jpg')
# grab topleft corner or input rgb color
filter_image(p_image, color="white")
render = ImageTk.PhotoImage(p_image)
img = Label(self, image=render)
img.image = render
Does this get rid of all the white from my image?

My file is actually 'C:\\Users\\Erik\\Desktop\\EditedResize.bmp', sorry.
Reply
#6
This method also requires you to install pillow to be install on python 3.
python 2 use PIL.

Yes. It get rid of all white that equal (255,255,255).
String color names. Will not work.

filter_image(p_image, color="white")

Either grab top left corner pixel for transparent color.
Or use rgb format (255,255,255) equals white.

Bitmaps don't hold transparency. Using pillow save works fine.
Just replace ../Images/test with your file.
and change filter_image(p_image, (0,0,0)) to filter_image(p_image, (255,255,255))
import tkinter as tk
from PIL import ImageTk, Image

def filter_image(image, color):
    alpha_mask = Image.new('1', image.size)
    for y in range(image.size[1]):
        for x in range(image.size[0]):
            if image.getpixel((x, y)) == color:
                alpha_mask.putpixel((x,y), 0)
            else:
                alpha_mask.putpixel((x,y), 1)

    image.putalpha(alpha_mask)

class App:
    def __init__(self):
        self.root = tk.Tk()
        filename = "../Images/test.bmp"
        p_image = Image.open(filename)
        # grab topleft corner or input rgb color
        # filter_image(p_image, p_image.getpixel((0,0)))
        filter_image(p_image, (0,0,0))
        p_image.save("../Images/test2.bmp")
        p_image.save("../Images/test2.png")
        self.image = ImageTk.PhotoImage(p_image)
        self.label = tk.Label(self.root, image = self.image)
        self.label.pack()

app = App()
app.root.mainloop()
99 percent of computer problems exists between chair and keyboard.
Reply
#7
What does using the putpixel class do? Am I setting that specific pixel to black/white, or is my program deleting them/drawing them?
Reply
#8
My filter_image function build an alpha image. The putpixel put pixel at location in the image.
0 = transparent, 1 = visible. Then it adds this alpha image to the image. When save as a bitmap.
The alpa is remove. This is because bitmaps cannot hold alphas. Program draw them with alpha intensity.
99 percent of computer problems exists between chair and keyboard.
Reply
#9
Okay, that makes sense. Thank you so much for your help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] White edge sdgdfgg 4 722 Dec-14-2023, 04:48 AM
Last Post: reneejenkins
  [Tkinter] _tkinter.TclError: bitmap "Icon.gif" not defined djwilson0495 2 12,860 Aug-05-2020, 02:27 PM
Last Post: wuf
  What should I do the character comes to the door the background changes in to white. Killdoz 0 1,306 May-22-2020, 02:41 PM
Last Post: Killdoz
  [WxPython] Bitmap wont find file loulou 1 2,689 Jun-20-2019, 01:34 PM
Last Post: Larz60+
  [WxPython] How to create a static white box text giu88 2 2,465 Aug-14-2018, 10:50 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020