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;
}







10















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:
enter image description hereenter image description here



my output image:
enter image description here



How can I have transparent pixels instead of white?










share|improve this question































    10















    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:
    enter image description hereenter image description here



    my output image:
    enter image description here



    How can I have transparent pixels instead of white?










    share|improve this question



























      10












      10








      10


      3






      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:
      enter image description hereenter image description here



      my output image:
      enter image description here



      How can I have transparent pixels instead of white?










      share|improve this question
















      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:
      enter image description hereenter image description here



      my output image:
      enter image description here



      How can I have transparent pixels instead of white?







      python python-imaging-library






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 28 '16 at 7:08









      Martin Evans

      28.6k133256




      28.6k133256










      asked Jul 28 '16 at 5:38









      pavitranpavitran

      411619




      411619
























          1 Answer
          1






          active

          oldest

          votes


















          20














          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:



          paste with transparency





          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)





          share|improve this answer


























          • 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





            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











          • 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












          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          20














          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:



          paste with transparency





          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)





          share|improve this answer


























          • 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





            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











          • 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
















          20














          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:



          paste with transparency





          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)





          share|improve this answer


























          • 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





            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











          • 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














          20












          20








          20







          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:



          paste with transparency





          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)





          share|improve this answer















          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:



          paste with transparency





          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)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 error bad 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 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











          • 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






          • 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











          • 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




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values