0

So I basically just want to have a list of all the pixel colour values that overlap written in a text file so I can then access them later.

The only problem is that the text file is having (set([ or whatever written with it.

Heres my code

import cv2
import numpy as np
import time

om=cv2.imread('spectrum1.png')
om=om.reshape(1,-1,3)
om_list=om.tolist()
om_tuple={tuple(item) for item in om_list[0]}
om_set=set(om_tuple)


im=cv2.imread('RGB.png')        
im=cv2.resize(im,(100,100))         
im= im.reshape(1,-1,3)
im_list=im.tolist()
im_tuple={tuple(item) for item in im_list[0]}
ColourCount= om_set & set(im_tuple)
File= open('Weedlist', 'w')
File.write(str(ColourCount))

Also, if I run this program again but with a different picture for comparison, will it append the data or overwrite it? It's kinda hard to tell when just looking at numbers.

Davey Boy
  • 79
  • 1
  • 10
  • So what is the *expected* output? Do your sets contain tuples of integers or floats? – Martijn Pieters Jun 13 '16 at 07:01
  • Note that `om_tuple` is *already a set*; you used a set comprehension to build it. This set *contains* tuples. The line `om_set=set(om_tuple)` is essentially redundant. – Martijn Pieters Jun 13 '16 at 07:07
  • The same goes for the `set()` call when you create the `ColourCount` intersection. You could just use `ColourCount = om_tuple & {tuple(item) for item in im_list[0]}` – Martijn Pieters Jun 13 '16 at 07:09

3 Answers3

0

You opened the file with the 'w' mode, write mode, which will truncate (empty) the file when you open it. Use 'a' append mode if you want data to be added to the end each time

You are writing the str() conversion of a set object to your file:

ColourCount= om_set & set(im_tuple)
File= open('Weedlist', 'w')
File.write(str(ColourCount))

Don't use str to convert the whole object; format your data to a string you find easy to read back again. You probably want to add a newline too if you want each new entry to be added on a new line. Perhaps you want to sort the data too, since a set lists items in an ordered determined by implementation details.

If comma-separated works for you, use str.join(); your set contains tuples of integer numbers, and it sounds as if you are fine with the repr() output per tuple, so we can re-use that:

with open('Weedlist', 'a') as outputfile:
    output = ', '.join([str(tup) for tup in sorted(ColourCount)])
    outputfile.write(output + '\n')

I used with there to ensure that the file object is automatically closed again after you are done writing; see Understanding Python's with statement for further information on what this means.

Note that if you plan to read this data again, the above is not going to be all that efficient to parse again. You should pick a machine-readable format. If you need to communicate with an existing program, you'll need to find out what formats that program accepts.

If you are programming that other program as well, pick a format that other programming language supports. JSON is widely supported for example (use the json module and convert your set to a list first; json.dump(sorted(ColourCount), fileobj), then `fileobj.write('\n') to produce newline-separated JSON objects could do).

If that other program is coded in Python, consider using the pickle module, which writes Python objects to a file efficiently in a format the same module can load again:

with open('Weedlist', 'ab') as picklefile:
    pickle.dump(ColourCount, picklefile)

and reading is as easy as:

sets = []
with open('Weedlist', 'rb') as picklefile:
    while True:
        try:
            sets.append(pickle.load(output))
        except EOFError:
            break

See Saving and loading multiple objects in pickle file? as to why I use a while True loop there to load multiple entries.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Will I be able to use this data as one big set in another program? It looks like I will be able to but I thought I would ask ahead first. – Davey Boy Jun 13 '16 at 07:23
  • @DaveyBoy: how is that other program going to load the data? And what do you mean by *one big set*; the data per line or the data of all appended writes taken together? – Martijn Pieters Jun 13 '16 at 07:24
  • @DaveyBoy: in other words, how are you planning to read the file again? Are you writing that other program in Python too? You need to think about data interchange here. You may need to use a different format instead. If the other program is in Python as well, consider using `pickle` (standard library) or the numpy file reading and writing support. If another language, find out what formats that supports. It may be JSON would be a better choice (writing out the set as a JSON array). – Martijn Pieters Jun 13 '16 at 07:26
  • I did text_file = open("Weedlist", "r") lines = text_file.readlines() om_set=set(lines) text_file.close() but it does not seem to be doing the job... I don't think it's reading the values right... The other program is currently written in python. – Davey Boy Jun 13 '16 at 07:47
  • @DaveyBoy: Each line contains `(number, number, number)` strings separated by commas; you'd need to parse those strings out. That's not all that efficient; study up on how to use `pickle` instead, which writes binary data that the same module can efficiently convert back to Python objects again. – Martijn Pieters Jun 13 '16 at 08:18
0

If you replace these lines:

im=cv2.imread('RGB.png')

File= open('Weedlist', 'w')
File.write(str(ColourCount))

with:

import sys

im=cv2.imread(sys.argv[1])

open(sys.argv[1]+'Weedlist', 'w').write(str(list(ColourCount)))

you will get a new file for each input file and also you don't have to overwrite the RGB.png every time you want to try something new.

Files opened with mode 'w' will be overwritten. You can use 'a' to append.

Jonas Byström
  • 25,316
  • 23
  • 100
  • 147
-1

How would you like the data to be written? Replace the final line by

File.write(str(list(ColourCount)))

Maybe you like that more.

If you run that program, it will overwrite the previous content of the file. If you prefer to apprend the data open the file with:

File= open('Weedlist', 'a')
LGenzelis
  • 764
  • 6
  • 14