1

I upload a PNG file and then split it into its red, green and blue components. I then save these components.

The original image is 1440 x 764 pixels, 2.21MB. But the 3 saved files are each 360x198 pixels, 96.9 KB.

Why is the output image so much smaller? Here's my code:

    file = "C:\\myFile.png";
    stem = "C:\\";
    img = Import[file];
    imgData = ImageData[img];
    rgbData = 
      Table[ArrayPlot[imgData[[;; , ;; , n]], 
        ColorFunction -> (GrayLevel[#] & 1)], {n, 1, 3}];
    Table[Export [FileNameJoin[{stem, ToString[n] <> ".png"}], 
       rgbData[[n]], "PNG"], {n, 1, 3}];
lynvie
  • 476
  • 3
  • 10
  • you should use ColorSeparate to separate colorchannels – paw Feb 20 '15 at 09:33
  • 2
    Try using Image rather than ArrayPlot : http://mathematica.stackexchange.com/a/24124/2079 – george2079 Feb 20 '15 at 15:41
  • @george2079: Yes that worked! Basically, I used Image instead of ArrayPlot and changed ColorFunction->(blah) to ColorSpace->"Grayscale" and I got the result I was looking for. But your comment is too short to accept as an answer. Could you expand a bit? Also -- why does ArrayPlot reduce the image size? That doesn't make any sense to me. – lynvie Feb 20 '15 at 17:22

1 Answers1

1

ArrayPlot is not designed for 1:1 mapping of array values to pixels, in fact in typical usage (small array) ArrayPlot generates a raster that is much larger than the input array dimensions.

You may get what you want using the PixelConstrained option:

 ArrayPlot[ data , PixelConstrained -> 1,  Frame -> False]
 ImageDimensions@%  ==  Reverse@Dimensions[data]

True

Using Image ( https://mathematica.stackexchange.com/a/24124/2079 ) Is a more direct/reliable approach however.

george2079
  • 38,913
  • 1
  • 43
  • 110