After finishing a few tutorials, I thought I was ready to calculate the pixel values of a large (smaller numbers 20x20 used here) image and export to a standard format (.jpg) but my code doesn't work. I am clearly missing something important. I thought it might be best to see if someone would look at a small segment of the code and comment.

(* image calc *)
numrow = 20;
numcol = 20;

(* midpoint of image *)
midrow = N[numrow/2];
midcol = N[numcol/2];

(* azimuth angle of pixel from center point *) 
(* mixed symbolic and numeric results *)
patfun[i_,j_] := N[ArcTan[i-midrow,j-midcol]];

(* spoke-like image *)
pat = Array[patfun[i,j], 20, 20];
  • 1
    $\begingroup$ This is just the simple stuff... you haven't yet shown where you're going wrong and how you failed at exporting. $\endgroup$
    – rm -rf
    Commented Oct 30, 2013 at 1:41
  • 1
    $\begingroup$ There is another tutorial you should read: common pitfalls $\endgroup$
    – m_goldberg
    Commented Oct 30, 2013 at 2:14

4 Answers 4


Here is another way to do it, which keeps the auxiliary variables localized.

pixelData =
  With[{numrow = 200, numcol = 200}, 
    With[{midrow = numrow/2, midcol = numcol/2},
        patfun[midrow, midcol] = 0.; 
        patfun[i_, j_] := N[ArcTan[i - midrow, j - midcol]];
        Table[patfun[i, j], {i, 0, numrow}, {j, 0, numcol}]]]];
img = Image @ Rescale @ pixelData



Here's another way: Especially for listable built in functions like ArcTan using (packed) arrays for coordinates might be faster than calling ArcTan for every single pixel.


enter image description here

Export["filename here",img];

(And to prevent the division by zero at x/y=0, if the dimensions aren't even, I usually just cheat and add a small number like 10^-10 or $MachineEpsilon to x or y.)


To supplement the other answers: by adding a semicolon at the end of every line, you sometimes don't spot the errors soon enough. For example, after:

pat = Array[patfun[i, j], {20, 20}]

you would have seen a list of

{{ArcTan[-10. + i, -10. + j][1, 1], ...

where the i and j are hanging around like uninvited guests.

After 'correcting' this line to:

pat = Array[patfun, {20, 20}]

you see an error message:

ArcTan::indet: Indeterminate expression ArcTan[0.,0.] encountered. >>

and, halfway down (if you've left the semicolon off):

{-1.5708, -1.5708, -1.5708, -1.5708, -1.5708, -1.5708, -1.5708, -1.5708,
 -1.5708, Indeterminate, 1.5708, 1.5708, 1.5708, 1.5708, 1.5708, 1.5708, 1.5708,
 1.5708, 1.5708, 1.5708},

and that Indeterminate will frustrate a subsequent attempt to apply the Image function to your data. You could, I suppose, do this:

Image[pat /. Indeterminate -> 0]

but I think the other solutions here - avoiding errors rather than cleaning up after them - are better.

  • 1
    $\begingroup$ +1, but don't do this for images from a typical megapixel camera. I regularly loose hours of unsaved work by doing just that... $\endgroup$ Commented Oct 30, 2013 at 9:24

Several coding problems addressed. Take a look:

hNumrow = hNumcol = 10;
patfun[i_, j_] := If[i - hNumrow == 0, Pi/2 Sign[j - hNumcol], ArcTan[i - hNumrow, j - hNumcol]];
pat = Rescale@Array[patfun[#1, #2] &, 2 {hNumrow, hNumcol}];
Export["\\yourFile.jpg", Image@pat]

Mathematica graphics


Your Answer

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

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