PIL: How to make area transparent in PNG?

I've been using PIL to crop Images, now I also want to make certain rectangular areas transparent, say

from PIL import Image
im = Image.open("sample.png")
transparent_area = (50,80,100,200)
...


Asked by: John485 | Posted: 30-11-2021






Answer 1

from PIL import Image
from PIL import ImageDraw
im = Image.open("image.png")
transparent_area = (50,80,100,200)

mask=Image.new('L', im.size, color=255)
draw=ImageDraw.Draw(mask) 
draw.rectangle(transparent_area, fill=0)
im.putalpha(mask)
im.save('/tmp/output.png')

I learned how to do this here.

Answered by: Catherine949 | Posted: 01-01-2022



Answer 2

No source code, but this is the general approach that should work: Create an alpha channel for the image in "L" (grayscale) mode as a separate image object. Fill the alpha channel with white (full opacity) and draw the rectangle on the alpha channel image in black (full transparency). Convert the image to which you want to apply the transparency to RGBA and use the image object putalpha() method to copy the alpha channel you created into the image's alpha channel. Save as PNG.

Answered by: Alina270 | Posted: 01-01-2022



Similar questions

python - How to use PIL to make all white pixels transparent?

I'm trying to make all white pixels transparent using the Python Image Library. (I'm a C hacker trying to learn python so be gentle) I've got the conversion working (at least the pixel values look correct) but I can't figure out how to convert the list into a buffer to re-create the image. Here's the code img = Image.open('img.png') imga = img.convert("RGBA") datas = imga.getdata() newData = list() for i...


python - Transparent 3D bar graphs

I would like to generate 3D bar graphs with transparent surfaces so that I can see what is going on behind tall bars. The mplot3d API docs say that keywords are allowed for the bar3d function. I pass all the required parameters but can only output graphs with solid surfaces.


python - How to merge a transparent png image with another image using PIL

I have a transparent png image "foo.png" and I've opened another image with im = Image.open("foo2.png"); now what i need is to merge foo.png with foo2.png. ( foo.png contains some text and I want to print that text on foo2.png )


python - In Pygame, why am I unable to make part of an image transparent?

I have an image of a red ball on a blue background saved as a BMP file. There are only two colors used in the image: red (255, 0, 0) and blue (0, 0, 255). I wrote the following Pygame test program to isolate the problem. It simply loads an image, sets the upper-left pixel's color as the transparent color, and blits it to the screen. It's about as simple as it gets, but I can't get it to work. import pygame ...


python - How to draw a transparent Line in pygame

I made a little game in which I need a background pattern with lines. Because of a better performance, I would like to draw the pattern in Python instead of taking an image. The problem is that I can't find a way to draw the lines with transparency. There are solutions for surfaces, but not for lines. Here is the pattern code: import pygame from math import pi pygame.init() size = [600, 600] ...


python - Fully transparent mask -> fully opaque widget

I ran into a strange corner case when attempting to render a fully transparent widget in PySide. If I call setMask with a QBitmap that is filled with Qt.color0, it makes the widget completely opaque (rather than completely transparent, as I would expect). Putting in a single pixel of Qt.color leads to the expected behavior: the widget becomes ...


Python . Select transparent colour or no colour for lines / points etc

How do I plot a line of transparent colour or no colour ? For example : plot(x,y,color='transparent') or plot(x,y,color='None') BTW , color='white' is not what I'm looking for . Thanks in advance invisible computer people .


Python PIL: Blend transparent image onto another

I need to blend an image over another image using Pythons PIL Library. As you can see in the image below, my two source images are A and B. When I do: imageA.paste(imageB, (0, 0), imageB) I get C as a result, but the part at the top of the gray background is now transparent. Image D is what I get when I put B over A in Photoshop and is what I need to achieve with PIL. What ...


python - How do you make an image transparent over a period of time?

I am currently trying to display a death screen when the player dies in my game, but rather than it just popping up I want it to gradually becoming less and less transparent until the opacity is 256. I currently have this line of code below to display the image, but I was wondering how I would go about gradually increasing the opacity over a period of few seconds. screen.blit(pygame.image.load("Images/dead....


python - How to draw a transparent image in pygame?

Consider a chess board, i have a transparent image of queen(queen.png) of size 70x70 and i want to display it over a black rectangle. Code: BLACK=(0,0,0) queen = pygame.image.load('queen.png') pygame.draw.rect(DISPLAYSURF, BLACK, (10, 10, 70, 70)) DISPLAYSURF.blit(queen, (10, 10)) Error: i am not getting transparent image ie black rectangle is not visible at all, only queen with white bac...


python - How to make a surface with a transparent background in pygame

Can someone give me some example code that creates a surface with a transparent background in pygame?


python - How to use PIL to make all white pixels transparent?

I'm trying to make all white pixels transparent using the Python Image Library. (I'm a C hacker trying to learn python so be gentle) I've got the conversion working (at least the pixel values look correct) but I can't figure out how to convert the list into a buffer to re-create the image. Here's the code img = Image.open('img.png') imga = img.convert("RGBA") datas = imga.getdata() newData = list() for i...


pygame - Updating part of a surface in python, or transparent surfaces

I have an application written in python that's basically an etch-a-sketch, you move pixels around with WASD and arrow keys and it leaves a trail. However, I want to add a counter for the amount of pixels on the screen. How do I have the counter update without updating the entire surface and pwning the pixel drawings? Alternatively, can I make a surface that's completely transparent except for the text so you can se...


django - python PIL - background displayed opaque instead of transparent

I want to generate 32x32 sized thumbnails from uploaded images (actually avatars). To prevent a thumbnail from being smaller than that size, I want to create a transparent 32x32 background and paste the thumbnail on it. The code below tries to do so. However, the avatar is displayed on a black and opaque background; I lose transparency information somewhere through the process. Where am I doing wrong?...


python - Transparent FrameBuffer background in OpenGL

I want to use glClear and glClearColor to fill a frame buffer with a colour including alpha transparency. However the framebuffer always renders as opaque when binded to a texture which is rendered to the screen. I want everything which is rendered to the framebuffer to kept their transparency. I just want to change the background. See the following code: def create_texture(surface): surface...


How do I create a Status Icon / System Tray Icon with custom text and transparent background using Python and GTK?

Here is the code that I have so far to define the icon: icon_bg = gtk.gdk.pixbuf_new_from_file('gmail.png') w, h = icon_bg.get_width(), icon_bg.get_height() cmap = gtk.gdk.Colormap(gtk.gdk.visual_get_system(), False) drawable = gtk.gdk.Pixmap(None, w, h, 24) drawable.set_colormap = cmap gc = drawable.new_gc() drawable.draw_pixbuf(gc, icon_bg, 0, 0, 0, 0, w, h) drawn_icon = gtk.gdk.Pixbuf(gtk.gdk.COLORSPAC...


python - How to make cStringIO transparent to another function that expects a real local file

I came up with the following problem: CODE A works right now.. I am saving a png file called chart.png locally, and then I am loading it into the proprietary function (which I do not have access). However, in CODE B, am trying to use cStringIO.StringIO() so that I do not have to write the file "chart.png" to the disk. But I cannot find a way to pass it to the pproprietaryfunction because it is expecting a real fil...


python - Matplotlib transparent line plots

I am plotting two similar trajectories in matplotlib and I'd like to plot each of the lines with partial transparency so that the red (plotted second) doesn't obscure the blue. EDIT: Here's the image with transparent lines.


python - Transparent 3D bar graphs

I would like to generate 3D bar graphs with transparent surfaces so that I can see what is going on behind tall bars. The mplot3d API docs say that keywords are allowed for the bar3d function. I pass all the required parameters but can only output graphs with solid surfaces.


python - Partially transparent scatter plot, but with a solid color bar

In Python, with Matplotlib, how to simply do a scatter plot with transparency (alpha < 1), but with a color bar that represents their color value, but has alpha = 1? Here is what one gets, with from pylab import *; scatter(range(10), arange(0, 100, 10), c=range(10), alpha=0.2); color_bar = colorbar(): How can the color ba...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top