I'm trying to use Pillow to paste an image with a transparent background onto a background image.
The background is a simple color background, and the transparent background image is as follows:
The transparent image
The code I'm using is below (the path is deliberately incorrect):
backgnd = Image.open("images/background.png")
backgnd.convert("RGBA")
body = Image.open("images/Untitled.png")
body.convert("RGBA")
backgnd.paste(body, (0,0), body)
backgnd.show()
The resulting image is as follows:
The resulting image, being all black
I've tried using the alpha_composite method, but it returns ValueError: image has wrong mode.
Try: backgnd.paste(body, (0,0), mask=body)
Related
I have to convert some PNGs with transparent bg to simple JPEGs where the transparent background turns to white (which I assumed will happen by default). I have tried all of the solutions I could find here, but after saving the PNG as JPEG the image will look like this: (the noisy area was transparent on the PNG and the black was drop shadow)
Converted image
Original image
This is the code I use:
# save the PNG
response = requests.get(image_url, headers = header)
file = open("%s-%s.png" % (slug, item_id,), "wb")
file.write(response.content)
file.close()
# open the PNG and save as a JPEG
im1 = Image.open(filepath_to_png)
rgb_im = im1.convert('RGB')
rgb_im.mode
rgb_im.save(filepath_normal)
My question is that how could I prevent the JPEG to have that corrupted background? My goal is just simply have the same image in JPEG what I had in PNG.
The method you are using to convert to RGB would work on some images that just require straight-forward conversion like the ones with hard-edged transparency masks, but for those with soft-edged masking (like the transparency shadows in your image) it is not be effective as the conversion does not know how to deal with that semi-transparency.
A better approach to handle this would be to create a new Image with the same dimensions and fill it with a white background, then you just need to paste your original image:
new_im = Image.new( "RGB", im1.size, ( 255,255,255 ) )
new_im.paste( im1, im1 )
new_im.save( filepath_normal )
I have tested this approach using an image with soft-edged masking and obtained the following result:
You could use pillow library in python.
from PIL import Image
im = Image.open("1.png")
bg = Image.new("RGB", im.size, (255,255,255))
bg.paste(im,im)
bg.save("2.jpg")
Result I got had transparent background turned to white.
So I am trying to paste a transparent image on top of a normal image. But for some reason, the transparency is gone.
Is there a way to keep the transparency of the second image (the transparent one) on top of the first image (the normal one) without losing the transparency of the second image?
(I am new to PIL by the way)
BG = Image.open("BGImage.png") # normal image, size is 1920x1080
BG = BG.convert("RGBA")
overlay = Image.open("OverlayImage.png") # transparent image, size is 1920x1080
overlay = overlay.convert("RGBA")
BG = Image.alpha_composite(BG, overlay) # I tried doing this but it didn't work
BG.save("NewImage.png")
Background Image:
Overlay Image:
What I got:
What I wanted (slight change in color):
Use paste like this - after ensuring your image is not palettised:
from PIL import Image
# Open background and foreground ensuring they are not palette images
bg = Image.open('bg.png').convert('RGB')
fg = Image.open('fg.png')
# Paste foreground onto background and save
bg.paste(fg,mask=fg)
bg.save('result.png')
At the moment my code takes an image in color and converts it to grayscale. The problem is that it makes the transparent pixels white or black.
This is what I have so far:
import cv2
img = cv2.imread("watch.png",cv2.IMREAD_GRAYSCALE)
cv2.imwrite("gray_watch.png",img)
Here is the pic for reference:
Watch.png
I found that using only PIL can accomplish this:
from PIL import Image
image_file = Image.open("convert_image.png") # opens image
image_file = image_file.convert('LA') # converts to grayscale w/ alpha
image_file.save('output_image.png') # saves image result into new file
This preserves the transparency of the image as well.
"LA" is a color mode. You can find other modes here: https://pillow.readthedocs.io/en/3.1.x/handbook/concepts.html#concept-modes
Creating a program to paste another image on top of another image. And when I paste a campaign logo that I made in Photoshop, And when I run the program, I get the error:
ValueError: bad transparency mask
I tried converting the image from RGBA to RGB, and that did not work, here is the code:
def test():
background = Image.open("photo.png")
logo = Image.open("66.png")
background_small = logo.resize(bg_size)
logo_small = logo.resize(logo_size)
background.paste(logo, (0, 600), logo)
background.show()
background.save('out.png')
Edit: I fixed the error using this stackoverflow post: Convert RGBA PNG to RGB with PIL
Try converting both to "RGBA" first.
I have a bunch of images I need to put a text-overlay on top of. I created the overlay with GIMP (PNG with transparency) and tried pasting it on top of the other image:
from PIL import Image
background = Image.open("hahn_echo_1.png")
foreground = Image.open("overlay_step_3.png")
background.paste(foreground, (0, 0), foreground)
background.save("abc.png")
However, instead of displaying a nice black text on top, I get this:
overlay.png looks like this in Gimp:
So I would expect some nice and black text instead of this colorful mess.
Any ideas? Some PIL option I am missing?
As vrs pointed out above, using alpha_composite like this answer: How to merge a transparent png image with another image using PIL
does the trick. Make sure to have the images in the correct mode (RGBA).
Complete solution:
from PIL import Image
background = Image.open("hahn_echo_1.png").convert("RGBA")
foreground = Image.open("overlay_step_3.png").convert("RGBA")
print(background.mode)
print(foreground.mode)
Image.alpha_composite(background, foreground).save("abc.png")
Result: