Get pixels color value in float (HDR)

653 Views Asked by At

Is it possible in Python to read an image and get each pixels color (12 bits depth) value between 0-1 (or 0-255 in float) without doing the conversion to 0-255 -> 0-1 range. This way I won't get precision loss and get the exact color in HDR format.

It look like cv2 and PIL read pixels only in integer value.

Here is a good repo of Paul Rohde that I would like to use in example for image : https://github.com/codelogic/wide-gamut-tests

1

There are 1 best solutions below

2
Mark Setchell On

Updated Answer

You can open a proper HDR file like this to get it in full float32 scaling:

import cv2
import numpy as np

im = cv2.imread('HDR_029_Sky_Cloudy_Ref.hdr',cv2.IMREAD_UNCHANGED)

In [24]: im.dtype
Out[24]: dtype('float32')

In [25]: im.shape
Out[25]: (1536, 3072, 3)

Original Answer

I still don't understand your question. The image you have shared is 8-bits per sample, or RGB888. It is neither 12-bit nor HDR.

You can see that with exiftool, 6 lines from the bottom:

ExifTool Version Number         : 12.30
File Name                       : R2020-sRGB-red.jpg
Directory                       : .
File Size                       : 270 KiB
File Modification Date/Time     : 2022:03:01 14:07:19+00:00
File Access Date/Time           : 2022:03:01 14:21:38+00:00
File Inode Change Date/Time     : 2022:03:01 14:21:36+00:00
File Permissions                : -rw-r--r--
File Type                       : JPEG
File Type Extension             : jpg
MIME Type                       : image/jpeg
Exif Byte Order                 : Big-endian (Motorola, MM)
Orientation                     : Horizontal (normal)
X Resolution                    : 300
Y Resolution                    : 300
Resolution Unit                 : inches
Software                        : Adobe Photoshop CC 2017 (Windows)
Modify Date                     : 2018:05:16 00:08:12
Color Space                     : Uncalibrated
Exif Image Width                : 1000
Exif Image Height               : 1000
Compression                     : JPEG (old-style)
Thumbnail Offset                : 318
Thumbnail Length                : 2619
Current IPTC Digest             : d3c99f4b3d8dea288548356a84159c2b
Coded Character Set             : UTF8
Application Record Version      : 0
IPTC Digest                     : d3c99f4b3d8dea288548356a84159c2b
Displayed Units X               : inches
Displayed Units Y               : inches
Print Style                     : Centered
Print Position                  : 0 0
Print Scale                     : 1
Global Angle                    : 90
Global Altitude                 : 30
URL List                        : 
Slices Group Name               : R2020-sRGB-colors
Num Slices                      : 1
Pixel Aspect Ratio              : 1
Photoshop Thumbnail             : (Binary data 2619 bytes, use -b option to extract)
Has Real Merged Data            : Yes
Writer Name                     : Adobe Photoshop
Reader Name                     : Adobe Photoshop CC 2017
Photoshop Quality               : 12
Photoshop Format                : Standard
Progressive Scans               : 3 Scans
XMP Toolkit                     : Adobe XMP Core 5.6-c138 79.159824, 2016/09/14-01:09:01
Creator Tool                    : Adobe Photoshop CC 2017 (Windows)
Create Date                     : 2018:04:14 13:42:30-07:00
Metadata Date                   : 2018:05:16 00:08:12-07:00
Format                          : image/jpeg
Instance ID                     : xmp.iid:c33f28fc-ee14-6f4c-a98f-03836b827b8d
Document ID                     : adobe:docid:photoshop:df3a2df2-58d7-11e8-83e7-fac92a657c91
Original Document ID            : xmp.did:89fc91da-7e3b-5e42-a65d-2086afe61c0c
Color Mode                      : RGB
ICC Profile Name                : R2020-D65-2_2-Gamma
History Action                  : created, saved, saved, converted, derived, saved
History Instance ID             : xmp.iid:89fc91da-7e3b-5e42-a65d-2086afe61c0c, xmp.iid:bf59df79-a0d5-844d-9a1a-bfe11a6f5dea, xmp.iid:8b239d1a-036e-d746-a911-301848e62193, xmp.iid:c33f28fc-ee14-6f4c-a98f-03836b827b8d
History When                    : 2018:04:14 13:42:30-07:00, 2018:04:14 15:35:31-07:00, 2018:05:16 00:08:12-07:00, 2018:05:16 00:08:12-07:00
History Software Agent          : Adobe Photoshop CC 2017 (Windows), Adobe Photoshop CC 2017 (Windows), Adobe Photoshop CC 2017 (Windows), Adobe Photoshop CC 2017 (Windows)
History Changed                 : /, /, /
History Parameters              : from application/vnd.adobe.photoshop to image/jpeg, converted from application/vnd.adobe.photoshop to image/jpeg
Derived From Instance ID        : xmp.iid:8b239d1a-036e-d746-a911-301848e62193
Derived From Document ID        : adobe:docid:photoshop:b57016aa-40f6-11e8-b223-d728bea1d13d
Derived From Original Document ID: xmp.did:89fc91da-7e3b-5e42-a65d-2086afe61c0c
Text Layer Name                 : Hello R2020., Description
Text Layer Text                 : Hello R2020., .jpeg
Document Ancestors              : adobe:docid:photoshop:142342da-4034-11e8-b223-d728bea1d13d, adobe:docid:photoshop:27a3c052-40f2-11e8-b223-d728bea1d13d, adobe:docid:photoshop:37aa27c0-4028-11e8-b223-d728bea1d13d, adobe:docid:photoshop:3ff7034b-40f2-11e8-b223-d728bea1d13d, adobe:docid:photoshop:426a1762-40f3-11e8-b223-d728bea1d13d, adobe:docid:photoshop:87dac81c-4037-11e8-b223-d728bea1d13d, adobe:docid:photoshop:a9fcfe50-40f3-11e8-b223-d728bea1d13d, adobe:docid:photoshop:adb60761-40f6-11e8-b223-d728bea1d13d, xmp.did:071fe721-2b13-634f-970a-fcb42809f1b4, xmp.did:3ae63550-a464-dd44-bb96-bf97bb0a97e1
Profile CMM Type                : Adobe Systems Inc.
Profile Version                 : 2.1.0
Profile Class                   : Display Device Profile
Color Space Data                : RGB
Profile Connection Space        : XYZ
Profile Date Time               : 2018:04:14 23:02:20
Profile File Signature          : acsp
Primary Platform                : Apple Computer Inc.
CMM Flags                       : Not Embedded, Independent
Device Manufacturer             : none
Device Model                    : 
Device Attributes               : Reflective, Glossy, Positive, Color
Rendering Intent                : ICC-Absolute Colorimetric
Connection Space Illuminant     : 0.9642 1 0.82489
Profile Creator                 : Adobe Systems Inc.
Profile ID                      : 0
Profile Copyright               : Copyright 2018 Adobe Systems Incorporated
Profile Description             : R2020-D65-2_2-Gamma
Media White Point               : 0.95047 1 1.08905
Red Matrix Column               : 0.67348 0.27904 -0.00194
Green Matrix Column             : 0.16568 0.67535 0.02998
Blue Matrix Column              : 0.12505 0.04561 0.79684
Red Tone Reproduction Curve     : (Binary data 14 bytes, use -b option to extract)
Green Tone Reproduction Curve   : (Binary data 14 bytes, use -b option to extract)
Blue Tone Reproduction Curve    : (Binary data 14 bytes, use -b option to extract)
DCT Encode Version              : 100
APP14 Flags 0                   : [14]
APP14 Flags 1                   : (none)
Color Transform                 : YCbCr
Image Width                     : 1000
Image Height                    : 1000
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8                             <<< HERE >>>
Color Components                : 3
Y Cb Cr Sub Sampling            : YCbCr4:4:4 (1 1)
Image Size                      : 1000x1000
Megapixels                      : 1.0
Thumbnail Image                 : (Binary data 2619 bytes, use -b option to extract)

If you load the image with OpenCV, you can see the text that you say you cannot see:

import cv2
import numpy as np

# Load image as BGR888
im = cv2.imread('R2020-sRGB-red.jpg')

# Make the pixels you want to see white, all else black
hello = np.all(im==[0,0,254], axis=2) * 255

enter image description here