In Python, the shape of an image with a single channel is a problem










2















I took a 256x256 RGB image and switched it to YCbCr,



I created code to import only the Y channel



Here is my code:



from PIL import Image
import numpy as np

im = Image.open ('test.bmp')
img = im.convert ('YCbCr')
arr_img = np.asarray (img)
arr_img = arr_img [:,:, 0]

img = Image.fromarray (arr_img)
img.show ()


After doing this, I created an image with the Y channel I wanted.



I was happy



But the problem is from here.



I ran this code



print (np.shape (arr_img))


Here, what I want is (256, 256, 1).



However, the above code output (256, 256).



So I changed "arr_img = arr_img [:,:, 0]" to "arr_img = arr_img [:, :, :1]".



However, it did not run because of an error.



I made the RGB image into an image with three channels of YCbCr,



I thought it would be the size (256, 256, 1) when I imported only the first channel, "Y"



How can I get the shape of (256, 256, 1)?










share|improve this question


























    2















    I took a 256x256 RGB image and switched it to YCbCr,



    I created code to import only the Y channel



    Here is my code:



    from PIL import Image
    import numpy as np

    im = Image.open ('test.bmp')
    img = im.convert ('YCbCr')
    arr_img = np.asarray (img)
    arr_img = arr_img [:,:, 0]

    img = Image.fromarray (arr_img)
    img.show ()


    After doing this, I created an image with the Y channel I wanted.



    I was happy



    But the problem is from here.



    I ran this code



    print (np.shape (arr_img))


    Here, what I want is (256, 256, 1).



    However, the above code output (256, 256).



    So I changed "arr_img = arr_img [:,:, 0]" to "arr_img = arr_img [:, :, :1]".



    However, it did not run because of an error.



    I made the RGB image into an image with three channels of YCbCr,



    I thought it would be the size (256, 256, 1) when I imported only the first channel, "Y"



    How can I get the shape of (256, 256, 1)?










    share|improve this question
























      2












      2








      2








      I took a 256x256 RGB image and switched it to YCbCr,



      I created code to import only the Y channel



      Here is my code:



      from PIL import Image
      import numpy as np

      im = Image.open ('test.bmp')
      img = im.convert ('YCbCr')
      arr_img = np.asarray (img)
      arr_img = arr_img [:,:, 0]

      img = Image.fromarray (arr_img)
      img.show ()


      After doing this, I created an image with the Y channel I wanted.



      I was happy



      But the problem is from here.



      I ran this code



      print (np.shape (arr_img))


      Here, what I want is (256, 256, 1).



      However, the above code output (256, 256).



      So I changed "arr_img = arr_img [:,:, 0]" to "arr_img = arr_img [:, :, :1]".



      However, it did not run because of an error.



      I made the RGB image into an image with three channels of YCbCr,



      I thought it would be the size (256, 256, 1) when I imported only the first channel, "Y"



      How can I get the shape of (256, 256, 1)?










      share|improve this question














      I took a 256x256 RGB image and switched it to YCbCr,



      I created code to import only the Y channel



      Here is my code:



      from PIL import Image
      import numpy as np

      im = Image.open ('test.bmp')
      img = im.convert ('YCbCr')
      arr_img = np.asarray (img)
      arr_img = arr_img [:,:, 0]

      img = Image.fromarray (arr_img)
      img.show ()


      After doing this, I created an image with the Y channel I wanted.



      I was happy



      But the problem is from here.



      I ran this code



      print (np.shape (arr_img))


      Here, what I want is (256, 256, 1).



      However, the above code output (256, 256).



      So I changed "arr_img = arr_img [:,:, 0]" to "arr_img = arr_img [:, :, :1]".



      However, it did not run because of an error.



      I made the RGB image into an image with three channels of YCbCr,



      I thought it would be the size (256, 256, 1) when I imported only the first channel, "Y"



      How can I get the shape of (256, 256, 1)?







      python image python-imaging-library






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 9:56









      ddjfjfj djfiejdnddjfjfj djfiejdn

      444




      444






















          1 Answer
          1






          active

          oldest

          votes


















          1














          A possibility is to add by hand the missing dimension:



          arr_img = np.zeros( [256,256] )



          arr_img = arr_img.reshape( * arr_img.shape, 1 )






          share|improve this answer























          • Thank you for the details.

            – ddjfjfj djfiejdn
            Nov 14 '18 at 9:09










          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%2f53278307%2fin-python-the-shape-of-an-image-with-a-single-channel-is-a-problem%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









          1














          A possibility is to add by hand the missing dimension:



          arr_img = np.zeros( [256,256] )



          arr_img = arr_img.reshape( * arr_img.shape, 1 )






          share|improve this answer























          • Thank you for the details.

            – ddjfjfj djfiejdn
            Nov 14 '18 at 9:09















          1














          A possibility is to add by hand the missing dimension:



          arr_img = np.zeros( [256,256] )



          arr_img = arr_img.reshape( * arr_img.shape, 1 )






          share|improve this answer























          • Thank you for the details.

            – ddjfjfj djfiejdn
            Nov 14 '18 at 9:09













          1












          1








          1







          A possibility is to add by hand the missing dimension:



          arr_img = np.zeros( [256,256] )



          arr_img = arr_img.reshape( * arr_img.shape, 1 )






          share|improve this answer













          A possibility is to add by hand the missing dimension:



          arr_img = np.zeros( [256,256] )



          arr_img = arr_img.reshape( * arr_img.shape, 1 )







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 10:36









          N.GN.G

          18217




          18217












          • Thank you for the details.

            – ddjfjfj djfiejdn
            Nov 14 '18 at 9:09

















          • Thank you for the details.

            – ddjfjfj djfiejdn
            Nov 14 '18 at 9:09
















          Thank you for the details.

          – ddjfjfj djfiejdn
          Nov 14 '18 at 9:09





          Thank you for the details.

          – ddjfjfj djfiejdn
          Nov 14 '18 at 9:09

















          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%2f53278307%2fin-python-the-shape-of-an-image-with-a-single-channel-is-a-problem%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

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Syphilis

          Darth Vader #20