How can I set the background of a transparent image to white using PIL?


Banuta Alin Alexandru

I have a PNG image with a transparent background and I want to resize it to another image, but with a white background instead of a transparent background. How to do this using PIL?

Here is my code:

basewidth = 200
img = Image.open("old.png")
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)hsize = int((float(img.size[1]) * float(wpercent)))
img.save("new.png")
Khaki
import Image
from resizeimage import resizeimage

f = Image.open('old.png') 
alpha1 = 0 # Original value
r2, g2, b2, alpha2 = 255, 255, 255,255 # Value that we want to replace it with

red, green, blue,alpha = data[:,:,0], data[:,:,1], data[:,:,2], data[:,:,3]
mask = (alpha==alpha1)
data[:,:,:3][mask] = [r2, g2, b2, alpha2]

data = np.array(f)
f = Image.fromarray(data)
f = f.resize((basewidth,hsize), PIL.Image.ANTIALIAS)

f.save('modified.png', image.format)

Related


How can I change the background of an image using PIL?

I work I'm trying to find out how to remove/change the background of a grayscale image in Python using the PIL package, but I'm failing. What I have is an RGB image with a white background and I want to load it, resize it, change the background from white to b

How can I set a transparent border around an image using CSS?

microphone I'm trying to create an oval shape on hover when someone hovers over a link. By doing this, I lost the image that was part of the link. I know what I'm doing wrong but what? .pagelinks { float: right; margin-right: 48%; display: inline-block;

How can I set a transparent border around an image using CSS?

microphone I'm trying to create an oval shape on hover when someone hovers over a link. By doing this, I lost the image that was part of the link. I know what I'm doing wrong but what? .pagelinks { float: right; margin-right: 48%; display: inline-block;

How can I remove the white background of a transparent icon?

Delightful I'm using a transparent forum icon, but I have a problem with the white background of the image: How can I remove it using CSS or software? thanks. Jamin Quimby Do you want the image to look like this with the white transparent? Photoshop took about

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

How can I set the opacity of a background image using CSS?

Nices I need to set opacity for the background image and I need to use plain text in the image. For this, I went through several ways but didn't find a solution that satisfies my requirement. Can you suggest me the best way for this purpose? In the end, what I

How can I set the opacity of a background image using CSS?

Nices I need to set opacity for the background image and I need to use plain text in the image. For this, I went through several ways but didn't find a solution that satisfies my requirement. Can you suggest me the best way for this purpose? In the end, what I