1
$\begingroup$

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}];
$\endgroup$
  • $\begingroup$ you should use ColorSeparate to separate colorchannels $\endgroup$ – paw Feb 20 '15 at 9:33
  • 2
    $\begingroup$ Try using Image rather than ArrayPlot : mathematica.stackexchange.com/a/24124/2079 $\endgroup$ – george2079 Feb 20 '15 at 15:41
  • $\begingroup$ @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. $\endgroup$ – lynvie Feb 20 '15 at 17:22
1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.