How to paste a PNG image with transparency to another image in PIL without white pixels?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have two images, a background and a PNG image with transparent pixels. I am trying to paste the PNG onto the background using Python-PIL but when I paste the two images I get white pixels around the PNG image where there were transparent pixels.
My code:
import os
from PIL import Image, ImageDraw, ImageFont
filename='pikachu.png'
ironman = Image.open(filename, 'r')
filename1='bg.png'
bg = Image.open(filename1, 'r')
text_img = Image.new('RGBA', (600,320), (0, 0, 0, 0))
text_img.paste(bg, (0,0))
text_img.paste(ironman, (0,0))
text_img.save("ball.png", format="png")
My images:
my output image:
How can I have transparent pixels instead of white?
python python-imaging-library
add a comment |
I have two images, a background and a PNG image with transparent pixels. I am trying to paste the PNG onto the background using Python-PIL but when I paste the two images I get white pixels around the PNG image where there were transparent pixels.
My code:
import os
from PIL import Image, ImageDraw, ImageFont
filename='pikachu.png'
ironman = Image.open(filename, 'r')
filename1='bg.png'
bg = Image.open(filename1, 'r')
text_img = Image.new('RGBA', (600,320), (0, 0, 0, 0))
text_img.paste(bg, (0,0))
text_img.paste(ironman, (0,0))
text_img.save("ball.png", format="png")
My images:
my output image:
How can I have transparent pixels instead of white?
python python-imaging-library
add a comment |
I have two images, a background and a PNG image with transparent pixels. I am trying to paste the PNG onto the background using Python-PIL but when I paste the two images I get white pixels around the PNG image where there were transparent pixels.
My code:
import os
from PIL import Image, ImageDraw, ImageFont
filename='pikachu.png'
ironman = Image.open(filename, 'r')
filename1='bg.png'
bg = Image.open(filename1, 'r')
text_img = Image.new('RGBA', (600,320), (0, 0, 0, 0))
text_img.paste(bg, (0,0))
text_img.paste(ironman, (0,0))
text_img.save("ball.png", format="png")
My images:
my output image:
How can I have transparent pixels instead of white?
python python-imaging-library
I have two images, a background and a PNG image with transparent pixels. I am trying to paste the PNG onto the background using Python-PIL but when I paste the two images I get white pixels around the PNG image where there were transparent pixels.
My code:
import os
from PIL import Image, ImageDraw, ImageFont
filename='pikachu.png'
ironman = Image.open(filename, 'r')
filename1='bg.png'
bg = Image.open(filename1, 'r')
text_img = Image.new('RGBA', (600,320), (0, 0, 0, 0))
text_img.paste(bg, (0,0))
text_img.paste(ironman, (0,0))
text_img.save("ball.png", format="png")
My images:
my output image:
How can I have transparent pixels instead of white?
python python-imaging-library
python python-imaging-library
edited Jul 28 '16 at 7:08
Martin Evans
28.6k133256
28.6k133256
asked Jul 28 '16 at 5:38
pavitranpavitran
411619
411619
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You need to specify the image as the mask as follows in the paste function:
import os
from PIL import Image
filename = 'pikachu.png'
ironman = Image.open(filename, 'r')
filename1 = 'bg.png'
bg = Image.open(filename1, 'r')
text_img = Image.new('RGBA', (600,320), (0, 0, 0, 0))
text_img.paste(bg, (0,0))
text_img.paste(ironman, (0,0), mask=ironman)
text_img.save("ball.png", format="png")
Giving you:
To centre both the background image and the transparent image on the new text_img
, you need to calculate the correct offsets based on the images dimensions:
text_img.paste(bg, ((text_img.width - bg.width) // 2, (text_img.height - bg.height) // 2))
text_img.paste(ironman, ((text_img.width - ironman.width) // 2, (text_img.height - ironman.height) // 2), mask=ironman)
thank you very much it works,but when i tried to do the same with image from internet URL i got the errorbad transparency mask
can you tell me reason for that?
– pavitran
Jul 28 '16 at 9:18
1
It probably depends on the image, can you give me a link to the image? You could also trymask=ironman.split()[3]
– Martin Evans
Jul 28 '16 at 9:23
what does the function.split()[3]
do here?
– pavitran
Jul 28 '16 at 9:39
It is passing just the mask layer rather than the whole image.
– Martin Evans
Jul 28 '16 at 9:54
2
Use//
instead of/
– Martin Evans
Jan 24 '18 at 14:17
|
show 5 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38627870%2fhow-to-paste-a-png-image-with-transparency-to-another-image-in-pil-without-white%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to specify the image as the mask as follows in the paste function:
import os
from PIL import Image
filename = 'pikachu.png'
ironman = Image.open(filename, 'r')
filename1 = 'bg.png'
bg = Image.open(filename1, 'r')
text_img = Image.new('RGBA', (600,320), (0, 0, 0, 0))
text_img.paste(bg, (0,0))
text_img.paste(ironman, (0,0), mask=ironman)
text_img.save("ball.png", format="png")
Giving you:
To centre both the background image and the transparent image on the new text_img
, you need to calculate the correct offsets based on the images dimensions:
text_img.paste(bg, ((text_img.width - bg.width) // 2, (text_img.height - bg.height) // 2))
text_img.paste(ironman, ((text_img.width - ironman.width) // 2, (text_img.height - ironman.height) // 2), mask=ironman)
thank you very much it works,but when i tried to do the same with image from internet URL i got the errorbad transparency mask
can you tell me reason for that?
– pavitran
Jul 28 '16 at 9:18
1
It probably depends on the image, can you give me a link to the image? You could also trymask=ironman.split()[3]
– Martin Evans
Jul 28 '16 at 9:23
what does the function.split()[3]
do here?
– pavitran
Jul 28 '16 at 9:39
It is passing just the mask layer rather than the whole image.
– Martin Evans
Jul 28 '16 at 9:54
2
Use//
instead of/
– Martin Evans
Jan 24 '18 at 14:17
|
show 5 more comments
You need to specify the image as the mask as follows in the paste function:
import os
from PIL import Image
filename = 'pikachu.png'
ironman = Image.open(filename, 'r')
filename1 = 'bg.png'
bg = Image.open(filename1, 'r')
text_img = Image.new('RGBA', (600,320), (0, 0, 0, 0))
text_img.paste(bg, (0,0))
text_img.paste(ironman, (0,0), mask=ironman)
text_img.save("ball.png", format="png")
Giving you:
To centre both the background image and the transparent image on the new text_img
, you need to calculate the correct offsets based on the images dimensions:
text_img.paste(bg, ((text_img.width - bg.width) // 2, (text_img.height - bg.height) // 2))
text_img.paste(ironman, ((text_img.width - ironman.width) // 2, (text_img.height - ironman.height) // 2), mask=ironman)
thank you very much it works,but when i tried to do the same with image from internet URL i got the errorbad transparency mask
can you tell me reason for that?
– pavitran
Jul 28 '16 at 9:18
1
It probably depends on the image, can you give me a link to the image? You could also trymask=ironman.split()[3]
– Martin Evans
Jul 28 '16 at 9:23
what does the function.split()[3]
do here?
– pavitran
Jul 28 '16 at 9:39
It is passing just the mask layer rather than the whole image.
– Martin Evans
Jul 28 '16 at 9:54
2
Use//
instead of/
– Martin Evans
Jan 24 '18 at 14:17
|
show 5 more comments
You need to specify the image as the mask as follows in the paste function:
import os
from PIL import Image
filename = 'pikachu.png'
ironman = Image.open(filename, 'r')
filename1 = 'bg.png'
bg = Image.open(filename1, 'r')
text_img = Image.new('RGBA', (600,320), (0, 0, 0, 0))
text_img.paste(bg, (0,0))
text_img.paste(ironman, (0,0), mask=ironman)
text_img.save("ball.png", format="png")
Giving you:
To centre both the background image and the transparent image on the new text_img
, you need to calculate the correct offsets based on the images dimensions:
text_img.paste(bg, ((text_img.width - bg.width) // 2, (text_img.height - bg.height) // 2))
text_img.paste(ironman, ((text_img.width - ironman.width) // 2, (text_img.height - ironman.height) // 2), mask=ironman)
You need to specify the image as the mask as follows in the paste function:
import os
from PIL import Image
filename = 'pikachu.png'
ironman = Image.open(filename, 'r')
filename1 = 'bg.png'
bg = Image.open(filename1, 'r')
text_img = Image.new('RGBA', (600,320), (0, 0, 0, 0))
text_img.paste(bg, (0,0))
text_img.paste(ironman, (0,0), mask=ironman)
text_img.save("ball.png", format="png")
Giving you:
To centre both the background image and the transparent image on the new text_img
, you need to calculate the correct offsets based on the images dimensions:
text_img.paste(bg, ((text_img.width - bg.width) // 2, (text_img.height - bg.height) // 2))
text_img.paste(ironman, ((text_img.width - ironman.width) // 2, (text_img.height - ironman.height) // 2), mask=ironman)
edited Nov 16 '18 at 10:32
answered Jul 28 '16 at 7:05
Martin EvansMartin Evans
28.6k133256
28.6k133256
thank you very much it works,but when i tried to do the same with image from internet URL i got the errorbad transparency mask
can you tell me reason for that?
– pavitran
Jul 28 '16 at 9:18
1
It probably depends on the image, can you give me a link to the image? You could also trymask=ironman.split()[3]
– Martin Evans
Jul 28 '16 at 9:23
what does the function.split()[3]
do here?
– pavitran
Jul 28 '16 at 9:39
It is passing just the mask layer rather than the whole image.
– Martin Evans
Jul 28 '16 at 9:54
2
Use//
instead of/
– Martin Evans
Jan 24 '18 at 14:17
|
show 5 more comments
thank you very much it works,but when i tried to do the same with image from internet URL i got the errorbad transparency mask
can you tell me reason for that?
– pavitran
Jul 28 '16 at 9:18
1
It probably depends on the image, can you give me a link to the image? You could also trymask=ironman.split()[3]
– Martin Evans
Jul 28 '16 at 9:23
what does the function.split()[3]
do here?
– pavitran
Jul 28 '16 at 9:39
It is passing just the mask layer rather than the whole image.
– Martin Evans
Jul 28 '16 at 9:54
2
Use//
instead of/
– Martin Evans
Jan 24 '18 at 14:17
thank you very much it works,but when i tried to do the same with image from internet URL i got the error
bad transparency mask
can you tell me reason for that?– pavitran
Jul 28 '16 at 9:18
thank you very much it works,but when i tried to do the same with image from internet URL i got the error
bad transparency mask
can you tell me reason for that?– pavitran
Jul 28 '16 at 9:18
1
1
It probably depends on the image, can you give me a link to the image? You could also try
mask=ironman.split()[3]
– Martin Evans
Jul 28 '16 at 9:23
It probably depends on the image, can you give me a link to the image? You could also try
mask=ironman.split()[3]
– Martin Evans
Jul 28 '16 at 9:23
what does the function
.split()[3]
do here?– pavitran
Jul 28 '16 at 9:39
what does the function
.split()[3]
do here?– pavitran
Jul 28 '16 at 9:39
It is passing just the mask layer rather than the whole image.
– Martin Evans
Jul 28 '16 at 9:54
It is passing just the mask layer rather than the whole image.
– Martin Evans
Jul 28 '16 at 9:54
2
2
Use
//
instead of /
– Martin Evans
Jan 24 '18 at 14:17
Use
//
instead of /
– Martin Evans
Jan 24 '18 at 14:17
|
show 5 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38627870%2fhow-to-paste-a-png-image-with-transparency-to-another-image-in-pil-without-white%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown