Why is the color changed after applying ImageData?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I have an image square.png as below:-



enter image description here



I imported the image as ImageSquare, and then applied ImageData to the image. When I applied Image to convert the image back, the color is changed:-



ImageSquare = Import@StringJoin[NotebookDirectory, "square.png"]
ImageSquare2 = Image@ImageData@ImageSquare


enter image description here



Why is that? In fact if I checked the value of the images, I still got True:



(ImageData@ImageSquare2) == (ImageData@ImageSquare)


Many thanks!!







share|improve this question
























    up vote
    2
    down vote

    favorite












    I have an image square.png as below:-



    enter image description here



    I imported the image as ImageSquare, and then applied ImageData to the image. When I applied Image to convert the image back, the color is changed:-



    ImageSquare = Import@StringJoin[NotebookDirectory, "square.png"]
    ImageSquare2 = Image@ImageData@ImageSquare


    enter image description here



    Why is that? In fact if I checked the value of the images, I still got True:



    (ImageData@ImageSquare2) == (ImageData@ImageSquare)


    Many thanks!!







    share|improve this question






















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have an image square.png as below:-



      enter image description here



      I imported the image as ImageSquare, and then applied ImageData to the image. When I applied Image to convert the image back, the color is changed:-



      ImageSquare = Import@StringJoin[NotebookDirectory, "square.png"]
      ImageSquare2 = Image@ImageData@ImageSquare


      enter image description here



      Why is that? In fact if I checked the value of the images, I still got True:



      (ImageData@ImageSquare2) == (ImageData@ImageSquare)


      Many thanks!!







      share|improve this question












      I have an image square.png as below:-



      enter image description here



      I imported the image as ImageSquare, and then applied ImageData to the image. When I applied Image to convert the image back, the color is changed:-



      ImageSquare = Import@StringJoin[NotebookDirectory, "square.png"]
      ImageSquare2 = Image@ImageData@ImageSquare


      enter image description here



      Why is that? In fact if I checked the value of the images, I still got True:



      (ImageData@ImageSquare2) == (ImageData@ImageSquare)


      Many thanks!!









      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 2 at 1:05









      H42

      1,404111




      1,404111




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You should specify the ColorSpace manually:



          ImageSquare2 = Image[ImageData@ImageSquare, ColorSpace -> "RGB"]


          Or take it from the original image:



          colorSpace = Options@ImageSquare
          ImageSquare2 = Image[ImageData@ImageSquare, colorSpace]


          Mathematica graphics



          The original approach yield:



          Options@(Image@ImageData@ImageSquare)
          (* ColorSpace -> Automatic, Interleaving -> True *)


          Which is wrong and uses the following rule from Image:




          c1,c2,c3,… channel values rendered by equally spaced hues






          share|improve this answer



























            up vote
            2
            down vote













            ImageSquare2 = Image[ImageData@ImageSquare, Options@ImageSquare]


            Mathematica graphics






            share|improve this answer



























              up vote
              2
              down vote













              This happens because this PNG file has three colour channels (RGB) and an additional alpha channel (transparency).



              When applying ImageData (or ColorSeparate) blindly, we simply get four channels as the result. But the information on how to interpret these (i.e. RGB + alpha) is lost. Re-combining them gives a generic 4-channel image, not an RGB one (see here one how it's displayed).



              I suggest removing the alpha channel before using ImageData: RemoveAlphaChannel.



              Otherwise, use ColorSpace -> "RGB" with Image or the second argument of ColorCombine when recombining the channels. The fourth channel will be interpreted as an alpha channel in this case.



              You can check if an image has an alpha channel like this: https://mathematica.stackexchange.com/a/157458/12






              share|improve this answer




















              • Thanks. I tried RemoveAlphaChannel as you suggested and it really works. But I don't understand why it matters. I noticed that MinMax@(ImageData@ImageSquare)[[All, All, 4]] is just 1,1. Besides, why is the color-at-the-left of ImageSquare and ImageSquare2 are still the same? I also tried to change the alpha-value of ImageSquare, but that doesn't allow me to reconstruct an image that looks like ImageSquare2.
                – H42
                Sep 2 at 15:51











              Your Answer




              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("mathjaxEditing", function ()
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
              );
              );
              , "mathjax-editing");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "387"
              ;
              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',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181082%2fwhy-is-the-color-changed-after-applying-imagedata%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote



              accepted










              You should specify the ColorSpace manually:



              ImageSquare2 = Image[ImageData@ImageSquare, ColorSpace -> "RGB"]


              Or take it from the original image:



              colorSpace = Options@ImageSquare
              ImageSquare2 = Image[ImageData@ImageSquare, colorSpace]


              Mathematica graphics



              The original approach yield:



              Options@(Image@ImageData@ImageSquare)
              (* ColorSpace -> Automatic, Interleaving -> True *)


              Which is wrong and uses the following rule from Image:




              c1,c2,c3,… channel values rendered by equally spaced hues






              share|improve this answer
























                up vote
                2
                down vote



                accepted










                You should specify the ColorSpace manually:



                ImageSquare2 = Image[ImageData@ImageSquare, ColorSpace -> "RGB"]


                Or take it from the original image:



                colorSpace = Options@ImageSquare
                ImageSquare2 = Image[ImageData@ImageSquare, colorSpace]


                Mathematica graphics



                The original approach yield:



                Options@(Image@ImageData@ImageSquare)
                (* ColorSpace -> Automatic, Interleaving -> True *)


                Which is wrong and uses the following rule from Image:




                c1,c2,c3,… channel values rendered by equally spaced hues






                share|improve this answer






















                  up vote
                  2
                  down vote



                  accepted







                  up vote
                  2
                  down vote



                  accepted






                  You should specify the ColorSpace manually:



                  ImageSquare2 = Image[ImageData@ImageSquare, ColorSpace -> "RGB"]


                  Or take it from the original image:



                  colorSpace = Options@ImageSquare
                  ImageSquare2 = Image[ImageData@ImageSquare, colorSpace]


                  Mathematica graphics



                  The original approach yield:



                  Options@(Image@ImageData@ImageSquare)
                  (* ColorSpace -> Automatic, Interleaving -> True *)


                  Which is wrong and uses the following rule from Image:




                  c1,c2,c3,… channel values rendered by equally spaced hues






                  share|improve this answer












                  You should specify the ColorSpace manually:



                  ImageSquare2 = Image[ImageData@ImageSquare, ColorSpace -> "RGB"]


                  Or take it from the original image:



                  colorSpace = Options@ImageSquare
                  ImageSquare2 = Image[ImageData@ImageSquare, colorSpace]


                  Mathematica graphics



                  The original approach yield:



                  Options@(Image@ImageData@ImageSquare)
                  (* ColorSpace -> Automatic, Interleaving -> True *)


                  Which is wrong and uses the following rule from Image:




                  c1,c2,c3,… channel values rendered by equally spaced hues







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 2 at 1:15









                  m0nhawk

                  2,47811431




                  2,47811431




















                      up vote
                      2
                      down vote













                      ImageSquare2 = Image[ImageData@ImageSquare, Options@ImageSquare]


                      Mathematica graphics






                      share|improve this answer
























                        up vote
                        2
                        down vote













                        ImageSquare2 = Image[ImageData@ImageSquare, Options@ImageSquare]


                        Mathematica graphics






                        share|improve this answer






















                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          ImageSquare2 = Image[ImageData@ImageSquare, Options@ImageSquare]


                          Mathematica graphics






                          share|improve this answer












                          ImageSquare2 = Image[ImageData@ImageSquare, Options@ImageSquare]


                          Mathematica graphics







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Sep 2 at 1:11









                          Michael E2

                          140k11191457




                          140k11191457




















                              up vote
                              2
                              down vote













                              This happens because this PNG file has three colour channels (RGB) and an additional alpha channel (transparency).



                              When applying ImageData (or ColorSeparate) blindly, we simply get four channels as the result. But the information on how to interpret these (i.e. RGB + alpha) is lost. Re-combining them gives a generic 4-channel image, not an RGB one (see here one how it's displayed).



                              I suggest removing the alpha channel before using ImageData: RemoveAlphaChannel.



                              Otherwise, use ColorSpace -> "RGB" with Image or the second argument of ColorCombine when recombining the channels. The fourth channel will be interpreted as an alpha channel in this case.



                              You can check if an image has an alpha channel like this: https://mathematica.stackexchange.com/a/157458/12






                              share|improve this answer




















                              • Thanks. I tried RemoveAlphaChannel as you suggested and it really works. But I don't understand why it matters. I noticed that MinMax@(ImageData@ImageSquare)[[All, All, 4]] is just 1,1. Besides, why is the color-at-the-left of ImageSquare and ImageSquare2 are still the same? I also tried to change the alpha-value of ImageSquare, but that doesn't allow me to reconstruct an image that looks like ImageSquare2.
                                – H42
                                Sep 2 at 15:51















                              up vote
                              2
                              down vote













                              This happens because this PNG file has three colour channels (RGB) and an additional alpha channel (transparency).



                              When applying ImageData (or ColorSeparate) blindly, we simply get four channels as the result. But the information on how to interpret these (i.e. RGB + alpha) is lost. Re-combining them gives a generic 4-channel image, not an RGB one (see here one how it's displayed).



                              I suggest removing the alpha channel before using ImageData: RemoveAlphaChannel.



                              Otherwise, use ColorSpace -> "RGB" with Image or the second argument of ColorCombine when recombining the channels. The fourth channel will be interpreted as an alpha channel in this case.



                              You can check if an image has an alpha channel like this: https://mathematica.stackexchange.com/a/157458/12






                              share|improve this answer




















                              • Thanks. I tried RemoveAlphaChannel as you suggested and it really works. But I don't understand why it matters. I noticed that MinMax@(ImageData@ImageSquare)[[All, All, 4]] is just 1,1. Besides, why is the color-at-the-left of ImageSquare and ImageSquare2 are still the same? I also tried to change the alpha-value of ImageSquare, but that doesn't allow me to reconstruct an image that looks like ImageSquare2.
                                – H42
                                Sep 2 at 15:51













                              up vote
                              2
                              down vote










                              up vote
                              2
                              down vote









                              This happens because this PNG file has three colour channels (RGB) and an additional alpha channel (transparency).



                              When applying ImageData (or ColorSeparate) blindly, we simply get four channels as the result. But the information on how to interpret these (i.e. RGB + alpha) is lost. Re-combining them gives a generic 4-channel image, not an RGB one (see here one how it's displayed).



                              I suggest removing the alpha channel before using ImageData: RemoveAlphaChannel.



                              Otherwise, use ColorSpace -> "RGB" with Image or the second argument of ColorCombine when recombining the channels. The fourth channel will be interpreted as an alpha channel in this case.



                              You can check if an image has an alpha channel like this: https://mathematica.stackexchange.com/a/157458/12






                              share|improve this answer












                              This happens because this PNG file has three colour channels (RGB) and an additional alpha channel (transparency).



                              When applying ImageData (or ColorSeparate) blindly, we simply get four channels as the result. But the information on how to interpret these (i.e. RGB + alpha) is lost. Re-combining them gives a generic 4-channel image, not an RGB one (see here one how it's displayed).



                              I suggest removing the alpha channel before using ImageData: RemoveAlphaChannel.



                              Otherwise, use ColorSpace -> "RGB" with Image or the second argument of ColorCombine when recombining the channels. The fourth channel will be interpreted as an alpha channel in this case.



                              You can check if an image has an alpha channel like this: https://mathematica.stackexchange.com/a/157458/12







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Sep 2 at 11:56









                              Szabolcs

                              152k13415896




                              152k13415896











                              • Thanks. I tried RemoveAlphaChannel as you suggested and it really works. But I don't understand why it matters. I noticed that MinMax@(ImageData@ImageSquare)[[All, All, 4]] is just 1,1. Besides, why is the color-at-the-left of ImageSquare and ImageSquare2 are still the same? I also tried to change the alpha-value of ImageSquare, but that doesn't allow me to reconstruct an image that looks like ImageSquare2.
                                – H42
                                Sep 2 at 15:51

















                              • Thanks. I tried RemoveAlphaChannel as you suggested and it really works. But I don't understand why it matters. I noticed that MinMax@(ImageData@ImageSquare)[[All, All, 4]] is just 1,1. Besides, why is the color-at-the-left of ImageSquare and ImageSquare2 are still the same? I also tried to change the alpha-value of ImageSquare, but that doesn't allow me to reconstruct an image that looks like ImageSquare2.
                                – H42
                                Sep 2 at 15:51
















                              Thanks. I tried RemoveAlphaChannel as you suggested and it really works. But I don't understand why it matters. I noticed that MinMax@(ImageData@ImageSquare)[[All, All, 4]] is just 1,1. Besides, why is the color-at-the-left of ImageSquare and ImageSquare2 are still the same? I also tried to change the alpha-value of ImageSquare, but that doesn't allow me to reconstruct an image that looks like ImageSquare2.
                              – H42
                              Sep 2 at 15:51





                              Thanks. I tried RemoveAlphaChannel as you suggested and it really works. But I don't understand why it matters. I noticed that MinMax@(ImageData@ImageSquare)[[All, All, 4]] is just 1,1. Besides, why is the color-at-the-left of ImageSquare and ImageSquare2 are still the same? I also tried to change the alpha-value of ImageSquare, but that doesn't allow me to reconstruct an image that looks like ImageSquare2.
                              – H42
                              Sep 2 at 15:51


















                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181082%2fwhy-is-the-color-changed-after-applying-imagedata%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              What does second last employer means? [closed]

                              List of Gilmore Girls characters

                              Confectionery