Mathematica Saving an image in a for loop with an updated name

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Image Loop
AI Thread Summary
The discussion focuses on exporting images in a loop with dynamically generated names. The original code attempts to assign a graphics object to a string variable, which leads to an error. The solution involves directly exporting the rasterized image within the loop without storing it in a string. The corrected code successfully exports images named "image0.jpg" to "image3.jpg" by calling the rasterization function directly in the export command. This approach resolves the issue and allows for the desired image output.
member 428835
Hello all!

I am trying to export image0, image1, image2, etc in a for loop. What I have is
Code:
For[i = 0, i < 4, i++,
 "image" <> ToString[i] =
  Rasterize[plfunc3dANGside2[6, 2, 115, 0.1*i], Background -> None,
   ImageResolution -> 1000];
 Export["/home/josh/ISS_drops/vibrations/" <> "image" <> ToString[i] <>
    ".jpg", "image" <> ToString[i]]
 ]
I get an error when I try running this. Any idea of a better way? My logic:
line 1: for loop specs
line 2: string name of image
line3-4: definition of the image
line5-6: export image
 
Physics news on Phys.org
Line 2 evaluates to a String but line 3 evaluates to a Graphics. You cannot store a Graphics in a String. You probably want to use ToSymbol[ ] on your string
 
  • Like
Likes member 428835
Dale said:
Line 2 evaluates to a String but line 3 evaluates to a Graphics. You cannot store a Graphics in a String. You probably want to use ToSymbol[ ] on your string
Amazing, and thanks! For anyone in the future, I have now changed the script to this

Code:
For[i = 0, i < 4, i++,
 Export["/home/josh/ISS_drops/vibrations/" <> "image" <> ToString[i] <>
    ".jpg", 
  Rasterize[plfunc3dANGside2[6, 2, 115, 0.1*i], Background -> None, 
   ImageResolution -> 1000]]
 ]
[\code]
 

Similar threads

Replies
6
Views
4K
Replies
52
Views
12K
Replies
2
Views
4K
Replies
1
Views
3K
Replies
1
Views
3K
Replies
8
Views
4K
Replies
1
Views
3K
Replies
1
Views
2K
Replies
1
Views
3K
Back
Top