# Writelines for camera

**URL:** <https://forums.openmv.io/t/writelines-for-camera/9418>\
**Category:** OpenMV Boards\
**Created:** [April 4, 2024, 11:58am UTC](https://forums.openmv.io/t/writelines-for-camera/9418 "2024-04-04T11:58:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nima](https://avatars.discourse-cdn.com/v4/letter/n/9dc877/32.png) [@nima](https://forums.openmv.io/u/nima)\
**Post date:** [April 4, 2024, 11:58am UTC](https://forums.openmv.io/t/writelines-for-camera/9418/1 "2024-04-04T11:58:05Z")

</div>

hi. I was wondering if there was a similar thing to the “writelines” code of python on the cam.  
I was trying to have the “main.py” code save a bunch of numbers even after the camera has been shut off. so I tried to create files on the cam and save the numbers on the files. the cam is not connected to the laptop , it is connected to a Arduino board.

---

<div class="post-metadata">

**Author:** ![jay.cross.bee](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/jay.cross.bee/32/2648_2.png) [@jay.cross.bee](https://forums.openmv.io/u/jay.cross.bee)\
**Post date:** [April 4, 2024, 1:33pm UTC](https://forums.openmv.io/t/writelines-for-camera/9418/2 "2024-04-04T13:33:40Z")

</div>

Could you post the code you’re using so we can get a better idea of what’s going on?

Thank you.

---

<div class="post-metadata">

**Author:** ![nima](https://avatars.discourse-cdn.com/v4/letter/n/9dc877/32.png) [@nima](https://forums.openmv.io/u/nima)\
**Post date:** [April 4, 2024, 2:25pm UTC](https://forums.openmv.io/t/writelines-for-camera/9418/3 "2024-04-04T14:25:33Z")

</div>

import sensor  
import time

sensor.reset()  
sensor.set\_pixformat(sensor.RGB565)  
sensor.set\_framesize(sensor.QVGA)  
sensor.skip\_frames(time=2000)  
sensor.set\_auto\_gain(False)  
sensor.set\_auto\_whitebal(False)  
clock = time.clock()

r = [(320 // 2) - (50 // 2), (240 // 2) - (50 // 2), 50, 50]

for i in range(60):  
img = sensor.snapshot()  
img.draw\_rectangle(r)

# {{

# the parts in the “{{” and “}}” are my own code

# the rest is the “Automatic RGB565 Color Tracking Example” but without the comments

# this part work’s

f = open(‘color.txt’,‘r+’).readlines()

for i in range(0,len(f)):  
f[i] = int(f[i])

threshold = [f[0], f[1], f[2], f[3], f[4], f[5]]

# }}

for i in range(60):  
img = sensor.snapshot()  
hist = img.get\_histogram(roi=r)  
lo = hist.get\_percentile(0.01)  
hi = hist.get\_percentile(0.99)  
threshold[0] = (threshold[0] + lo.l\_value()) // 2  
threshold[1] = (threshold[1] + hi.l\_value()) // 2  
threshold[2] = (threshold[2] + lo.a\_value()) // 2  
threshold[3] = (threshold[3] + hi.a\_value()) // 2  
threshold[4] = (threshold[4] + lo.b\_value()) // 2  
threshold[5] = (threshold[5] + hi.b\_value()) // 2  
for blob in img.find\_blobs(  
[threshold], pixels\_threshold=100, area\_threshold=100, merge=True, margin=10  
):  
img.draw\_rectangle(blob.rect())  
img.draw\_cross(blob.cx(), blob.cy())  
img.draw\_rectangle(r)

# {{

open(‘color.txt’,‘r+’).writeline([threshold[0]+‘\r\n’,threshold[1]+‘\r\n’,threshold[2]+‘\r\n’,threshold[3]+‘\r\n’,threshold[4]+‘\r\n’,threshold[5]])

# this is basically what i want it to do

# }}

while True:  
clock.tick()  
img = sensor.snapshot()  
for blob in img.find\_blobs([threshold], pixels\_threshold=100, area\_threshold=100, merge=True, margin=10):  
img.draw\_rectangle(blob.rect())  
img.draw\_cross(blob.cx(), blob.cy())  
print(clock.fps())

---

<div class="post-metadata">

**Author:** ![jay.cross.bee](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/jay.cross.bee/32/2648_2.png) [@jay.cross.bee](https://forums.openmv.io/u/jay.cross.bee)\
**Post date:** [April 4, 2024, 3:03pm UTC](https://forums.openmv.io/t/writelines-for-camera/9418/4 "2024-04-04T15:03:59Z")

</div>

I would try using a context manager to write to the file, like this:  
`with open('color.txt', 'w') as f:`  
`for thr in threshold:`  
`# print(f'thr') # for making sure that thr is actually what you think it is`  
`f.write(f'{thr}\r\n')`

Also, I’m not sure about the r+ in the open() function. For writing, just use ‘w’

Also, the ‘color.txt’ file was opened and never closed before opening again.

The context manager (`with ...`) will close it for you automatically.

---

<div class="post-metadata">

**Author:** ![nima](https://avatars.discourse-cdn.com/v4/letter/n/9dc877/32.png) [@nima](https://forums.openmv.io/u/nima)\
**Post date:** [April 4, 2024, 4:39pm UTC](https://forums.openmv.io/t/writelines-for-camera/9418/5 "2024-04-04T16:39:53Z")

</div>

from what I have learnt it seams that “r+” allows you to read and write in the file at the same time while “w+” does the same but if the file does not exist it creates it.  
sadly I don’t have access to the camera for a few days but I will try to update if it worked.

---

<div class="post-metadata">

**Author:** ![nima](https://avatars.discourse-cdn.com/v4/letter/n/9dc877/32.png) [@nima](https://forums.openmv.io/u/nima)\
**Post date:** [April 8, 2024, 11:37am UTC](https://forums.openmv.io/t/writelines-for-camera/9418/6 "2024-04-08T11:37:05Z")

</div>

fixed!  
open(‘greeen.txt’,‘r+’).write(‘%d\r\n%d\r\n%d\r\n%d\r\n%d\r\n%d’%(thresholdgreen[0],thresholdgreen[1],thresholdgreen[2],thresholdgreen[3],thresholdgreen[4],thresholdgreen[5]))
