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?
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.
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.
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.
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.
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()
What does using the putpixel class do? Am I setting that specific pixel to black/white, or is my program deleting them/drawing them?
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.
Okay, that makes sense. Thank you so much for your help!