Python Forum
Formatting outputs created with .join command
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formatting outputs created with .join command
#1
I am doing a project on a Raspberry Pi Pico running Micro Python to which I am new at in terms of programming having never touch Python or any variant before I started a about a month ago.

I came here as I needed something to turn a small JPG image into a text file of RGB values my project and the only way I could find to do this was some code I found here. But this mostly dos what I want but not fully

The code I found here which was:

import cv2
img = cv2.imread('01.jpg')
with open('JpgtoRGBoutput.txt', 'w') as f_out: 
    for i in img: 
        f_out.write('|'.join(['{},{},{}'.format(*j) for j in i]) + '\n')
I've managed to adapt the code so it separates each pixel's RGB value onto a separate line by swapping the '|' part for '\n' so I get an output which reads something like:

162,53,2 231,127,80 211,116,72 157,56,11 190,67,11 191,57,0 198,61,0 195,68,0 166,58,4

but I now have 2 issues.

I need to add a number to identify the pixel before it's RGB value
I need each value to be 3 digits so instead of saying "255,25,5" I need it to say "001,255,025,005" (001 being the pixel number, the rest being the RGB value) because of how my micro python code reads the data into the Pico for use.
I tried this:

import cv2
img = cv2.imread('01.jpg')
cnt = 0
with open('JpgtoRGBoutput.txt', 'w') as f_out: 
    for i in img: 
        f_out.write('\n'+str(c)+",".join(['{},{},{}'.format(*j) for j in i]) + '\n')
        cnt += 1
But that only adds a number to the start of each line of pixels so I can work out that "i" is a line of pixels from the image and "j" presumably is a value within the line of pixels.

I've looked up the .join command but it confuses me because I am new to all of this. Can someone point me in the direction on how to achieve what I need to do?
Reply
#2
It helps to break things up into smaller pieces.
import cv2


img = cv2.imread('test.jpg')
with open('JpgtoRGBoutput.txt', 'w') as f_out:
    offset = 0
    for row in img:
        pixels = [f'{index+offset:03},{r:03},{g:03},{b:03}' for index, (r, g, b) in enumerate(row)]
        print(','.join(pixels), file=f_out)
        offset += len(pixels)
In this code the join() and the pixel creation are split into separate statements. This might make it easer to see that the only thing join does is stitch together a sequence of strings.

To pad numbers to 3 digits with a leading zero, you add ":03" to the format. I used f'string formatting because I think it reads better than using format(). If you prefer using format.
pixels = ["{:03},{:03},{:03},{:03}".format(col+offset, *pixel) for col, pixel in enumerate(row)]
There is a problem with this code in that for any image larger than 1000 pixels, the index is larger than 3 digits. This is the last line for a 250x72 image.
Output:
17750,250,218,039,17751,245,209,0311...17998,152,118,065,17999,233,207,170
Reply
#3
Just another way to pad with zeros - string method str.zfill(). It's actually more keystrokes than using f-string formatting but one can argue that more readable.


>>> for value in ("5", "25", "255", "2555"):
...     print(value.zfill(3))
...
005
025
255
2555
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  format json outputs ! evilcode1 3 1,758 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  I have written a program that outputs data based on GPS signal kalle 1 1,184 Jul-22-2022, 12:10 AM
Last Post: mcmxl22
  Why does absence of print command outputs quotes in function? Mark17 2 1,392 Jan-04-2022, 07:08 PM
Last Post: ndc85430
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,487 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  Combining outputs into a dataframe rybina 0 1,688 Mar-15-2021, 02:43 PM
Last Post: rybina
  ONE input => THREE outputs Tricia279 6 2,651 Jan-14-2021, 08:52 AM
Last Post: perfringo
  The difference between os.path.join( and os.sep.join( Pedroski55 2 9,486 Nov-17-2020, 08:38 AM
Last Post: Pedroski55
  Multi set string inputs/outputs kwmcgreal 2 2,086 Sep-26-2020, 10:44 PM
Last Post: kwmcgreal
  How to use subprocess to get multiple data outputs in desired folder? 3SG14 1 2,232 Sep-19-2020, 05:46 PM
Last Post: bowlofred
  Outputs missing SamAnw 4 2,643 Feb-12-2020, 04:32 PM
Last Post: adetheheat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020