Aug-22-2023, 04:18 PM
(This post was last modified: Aug-22-2023, 04:20 PM by deanhystad.)
It helps to break things up into smaller pieces.
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.
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