I'm just learning about image processing, and have chosen go as my preferred language to begin working with images.
I have an array of uint32
values, such as:
[[287 277 283 297 295 279 279 292 305 295 285 282 290 297 293 290 285 282 279 282 297 300 300 297 290 290 290 293 306 310 303 293 300 295 288 288 292 292 295 311 301 295 285 288 298 298 288 292 288 295 306 303 297 293 306 313 313 310 303 303 306 310 305 305 303 303 300 297 290 293 303 313 295 292 298 305 301 301 301 295 310 300 297 300 300 300 306 316 303 306 306 310 310 306 297 290 301 305 311 308 301 295 292 292 295 295 298 298 298 298 298 298 306 303 303 297 293 297 303 310 303 300 300 303 300 293 297 303 303 303 303 300 300 297 300 300 297 300 303 310 310 310 306 303 305 301 301 301 301 298 298 298 301 301 301 298 298 301 301 295 298 301 298 295 298 301 298 295 292 288 285 282 288 295 301 308 298 292 292 292 295 298 292 288 288 292 288 285 282 285 285 282 290 290 297 300 293 283 283 290 290 293 297 293 287 280 277 274 283 287 287 283 283 283 287 290 290 287 287 283 280 287 290 280 287 290 293 287 283 283 282 275 288 288 287 283 277 277 277 280 277 280 283 280 277 270 270 274 287 280 277 277 280 280 280 280 277 277 274 274 274 274 277 277 270 274 277 280 283 283 280 277 279 282 280 275 277 295 315 310 306 321 341 432 620 754 768 751 660 478 357 326 316 313 305 301 293 297 297 292 290 290 293 295 282 277 277 280 280 280 277 274 287 293 297 287 280 283 287 290 297 300 303 303 300 297 300 303 287 297 300 293 293 300 303 301 303 306 306 297 288 295 298 298 292 295 293 297 297 297 300 300 305 305 305 305 305 301 301 301 308 305 298 292 295 301 301 295 293 290 293 300 300 300 300 300 310 300 303 306 303 300 306 306 313 310 306 306 306 306 301 301 305 305 305 305 305 305 303 300 311 311 308 305 305 308 305 301 288 298 288 292 298 301 315 301 295 301 305 298 301 308 305 292 305 298 292 292 298 301 301 301 298 298 298 295 301 305 298 285 292 295 295 295 295 298 292 285 285 288 288 292 292 288 287 283 287 283 280 283 290 293 297 297 293 290 287 283 280 280 283 287 280 283 283 280 274 267 270 274 264 280 290 280 267 261 274 277 277 277 274 270 270 274 272 265 254 261 270 277 277 270 264 257 256 259 265 265 259 252 246 243 246 249 249 246 246 249 251 247 244 244 244 241 234 231 228 228 231 238 234 225 218 218 221 221 212 212 212 212 210 210 210 210 210 203 207 213 203 197 197 197 184 180 184 187 187 180 177 177 167 171 174 177 174 171 161 158 149 153 156 149 143 136 140 143 136 136 140 143 140 133 130 133 130 123 120 123 130 130 117 107 128 126 122 122 115 112 113 117], [], ... []]
I'm thinking of this problem as more what steps should I take to take this array and be able to convert it to an image?
I'm assuming there will be a number of bitwise operations, and convertions etc, but does anyone know of any specific algorithms perhaps even a core Go library, or perhaps some materials I can reference to be able to achieve this conversion?
I'm sure something exists, but I'm struggling to reference it without surety and confidence.
I'd be eternally grateful if someone can point me in the right direction ...
I have the following information on the sensor type, if that is useful, for understanding pixel values...
SensorType is Monochrome, RGGB, CMYG, CMYG2 or LRGB *or* Color
It is a greyscale image with dimensions 600x800. If you run your file through wc
to count the words:
wc YOURFILE.TXT
0 480000 1015388 YOURFILE.TXT
you will see it has:
The 480,000 is a clue that is 600x800 pixels, and greyscale because it would have 600x800x3 if 3-channel RGB and 600x800x4 if 4-channel RGBA.
You can probably extract it now with Go
, but here is how I did it...
First write a simple NetPBM PGM header to say it is greyscale, ASCII (not binary) and 600x800 with 255 as the maximum value. Then remove all the []
by replacing (tr
means "translate/transpose") them with linefeeds to make a PGM file:
{ printf "P2\n600 800\n255\n"; tr '[]' '\n\n' < YOURFILE.TXT ; } > image.pgm
image.pgm
now looks like this:
P2
600 800
255
7 7 7 8 8 7
...
...
Note that PGM
files can be readily viewed in GIMP, feh
, xv
, Photoshop, macOS Preview.app
and most likely in IrfanView and maybe even MS Paint.
Then I used ImageMagick to auto-level the contrast and make a JPEG:
magick image.pgm -auto-level image.jpg
As you can see, auto-level
is a bit harsh for such a low contrast image and it has created banding effects, but you can contrast stretch in other, less harsh, ways.