# Best way to repeatedly evaluate code lines?

**URL:** <https://community.wolfram.com/t/best-way-to-repeatedly-evaluate-code-lines/18717>\
**Category:** Questions\
**Tags:** image-processing, mathematica, graphics-and-visualization, import-and-export, packages-and-paclets\
**Created:** [August 21, 2021, 5:11pm UTC](https://community.wolfram.com/t/best-way-to-repeatedly-evaluate-code-lines/18717 "2021-08-21T17:11:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![udibehar](https://avatars.discourse-cdn.com/v4/letter/u/eb8c5e/32.png) [@udibehar](https://community.wolfram.com/u/udibehar)\
**Post date:** [August 21, 2021, 5:11pm UTC](https://community.wolfram.com/t/best-way-to-repeatedly-evaluate-code-lines/18717/1 "2021-08-21T17:11:19Z")

</div>

I have a couple of images (.tif file) on which I want to perform the following commands:

1. `Import` the image: `img = Import["path/to/file/image01.tif"]`

2. Remove the alpha channel: `img=RemoveAlphaChannel[ColorConvert[img, "Grayscale"]]`

3. Convert the imported image to data: `rawImg=ImageData[img]`

4. Finally, plot the mean of the image values in the vertical direction: `ListPlot[Mean[grayRaw]]`

As you can see, I am not asking for help about how to those manipulations on my image file. Let’s say I have a bunch of 10 such images, and I am looking for the most promising `ListPlot` of those.

One way I can do this is to get these 4 commands into one cell, and just change the image file name (`image01.tif`, `image02.tif` etc.).

But I would like to ask whether any one here in the community can give me a more systematic way of doing the manipulations above with a Wolfram Package file (`.wl file`). I read that this is the “more” correct way for developing Mathematica code (other than `.nb` files).

What about writing a function for doing these manipulations? Is that a good idea?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [August 22, 2021, 12:16am UTC](https://community.wolfram.com/t/best-way-to-repeatedly-evaluate-code-lines/18717/2 "2021-08-22T00:16:49Z")

</div>

Hi Ehud,

Writing a function is the best way to do that. Something like

```
meanGrayPlot[imageFileName_String] := 
  Import[imageFileName] // ColorConvert[#, "Grayscale"] & // RemoveAlphaChannel // 
  ImageData // Mean // 
  ListPlot[#, PlotLabel -> FileNameTake[imageFileName], ImageSize -> 300] &

```

Then map it over a list of filenames. I ran it on a directory with some PNG images I had. Change `*.png` to `*.tif` and `dir` to the name of the directory with your images.

```
FileNames["*.png", dir] // Map[meanGrayPlot] // 
  Partition[#, UpTo[3]] & // Grid[#, Frame -> All] & // 
  Labeled[#, "Mean Graylevel Plots", Top] &

```

 ![5141plot.png](https://global.discourse-cdn.com/wolfram/original/2X/8/8339a9af6fdb27ab92852424ecc5417d13231ca7.png)

---

<div class="post-metadata">

**Author:** ![udibehar](https://avatars.discourse-cdn.com/v4/letter/u/eb8c5e/32.png) [@udibehar](https://community.wolfram.com/u/udibehar)\
**Post date:** [August 27, 2021, 7:59pm UTC](https://community.wolfram.com/t/best-way-to-repeatedly-evaluate-code-lines/18717/3 "2021-08-27T19:59:29Z")

</div>

That’s a great answer. Thanks a lot!
