5
$\begingroup$

relatively new to Wolfram/Mathematica. I'm using wolframscript on ubuntu, and trying to export various Two-Color Automata as PNG files. It works fine for 50 evolutions, but when I do 500 or so, it is saving as gray-scale - even tho the array has only 0s and 1s. In Python, I was able to make CAs as big as I wanted, and I simply saved them as PNG files where each tuple defined a single pixel. It works if I create the CA in WolframCloud and simply right-click the image and save, but I need to do this for 1000s of images - so that's not practical. Here is an example, produced by this:

Export["zebra.png", ArrayPlot[CellularAutomaton[30, {{1}, 0}, 500]]]

enter image description here

Is there no way to "directly" create a PNG image from array-data, where each element in the array defines one pixel of the image, as in Python?

$\endgroup$
3
  • 1
    $\begingroup$ You should replace ArrayPlot with Image. $\endgroup$
    – ihojnicki
    Mar 26, 2021 at 15:02
  • $\begingroup$ thanks, that actually did work, now it's a binary image, but the colors are reversed...? $\endgroup$
    – user78092
    Mar 26, 2021 at 15:29
  • 1
    $\begingroup$ @panawe, there was PixelConstrained option for ArrayPlot, which is obsolete as of v12.1, but it still works: ArrayPlot[RandomInteger[{0, 1}, {64, 64}], PixelConstrained -> 1] // Options // First $\endgroup$
    – I.M.
    Mar 27, 2021 at 3:11

3 Answers 3

2
$\begingroup$

Generate a 501x1001 array:

A = CellularAutomaton[30, {{1}, 0}, 500];
Dimensions[A]
(*    {501, 1001}    *)

Export one pixel per matrix entry:

Export["~/Desktop/zebra.png", Image[Raster[Reverse[A]]]]

The resulting image has exactly 1001x501 pixels, each one either black (=0) or white (=1) (opposite color scheme from ArrayPlot):

enter image description here

If you want the inverted color scheme, just export 1-A instead of A.

$\endgroup$
0
$\begingroup$

You must set ImageSize. Here us an example and an enlarged piece of the resulting picture:

n = 500;
ar = Table[RandomInteger[{0, 1}], {n}, {n}];
Export["d:/tmp/test.png", ArrayPlot[ar, ImageSize -> 2000]]

enter image description here

The same with "GameOfLive":

n = 500;
ar = Table[RandomInteger[{0, 1}], {n}, {n}];
gol = CellularAutomaton["GameOfLife", ar]
Export["d:/tmp/test.png", ArrayPlot[gol, ImageSize -> 2000]]

enter image description here

$\endgroup$
2
  • $\begingroup$ thanks for the reply; I can get it to work for an array of RandomIntegers that are either 0 or 1, but not for the CellularAutomaton function, even with trying ImageSize->2000....is there something specific to the CA function that is causing it? even if I look at the actual data within the array created by the CA function, it's just 0s and 1s, but it always produces a grayscale image. $\endgroup$
    – user78092
    Mar 26, 2021 at 15:14
  • $\begingroup$ I added an example using: CellularAutomaton $\endgroup$ Mar 26, 2021 at 15:33
0
$\begingroup$

Thanks for everyone's input, but I ended up just iterating over the array and replacing each integer with a tuple, which gave me the control I wanted. This way I can do an arbitrary number of colors for the CAs and control each integer's output-color.

Do[
  If[A[[z]][[y]] == 0, 
    B[[z]][[y]] = RGBColor["White"],
    B[[z]][[y]] = RGBColor["Green"]
  ], 
  {z, Length[A]}, {y, Length[A[[z]]]}
] 
$\endgroup$
1
  • 1
    $\begingroup$ I think you could achieve the same goal with a replacement ca = CellularAutomaton[30, {{1}, 0}, 500]; ca /. {0 -> RGBColor["White"], _?NumberQ -> RGBColor["Green"]}. Iterating over an array is rarely the most direct way to achieve your goal in Mathematica. $\endgroup$
    – MarcoB
    Mar 27, 2021 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.