Direct answer
OpenCV does not store or expose timezone information; you must supply the offset yourself and attach it to the frame data externally.
Likely explanation
OpenCV’s VideoCapture::get(CAP_PROP_POS_MSEC) returns raw milliseconds elapsed since the stream started, derived from the system clock without any conversion to civil time. imwrite writes images using the C library’s time/localtime functions, which produce a DateTimeOriginal string without a timezone offset, and the library does not provide an API to embed one.
Confirmed facts
CAP_PROP_POS_MSEC is a monotonic offset from stream start, not a wall‑clock timestamp.
- When
imwrite saves JPEG/PNG, any EXIF DateTime field is generated from localtime and lacks a +HH:MM offset.
- No OpenCV function currently accepts or returns a timezone‑aware
std::chrono::zoned_time or datetime object.
Steps to associate a known zone with VideoCapture timestamps
- At the moment you open the capture (or before the first frame), record the current timezone offset using your language’s datetime library (e.g.,
datetime.now().astimezone().utcoffset() in Python).
- For each frame, read
pos_msec = cap.get(cv2.CAP_PROP_POS_MSEC).
- Convert the elapsed milliseconds to a UTC
datetime (utc = datetime.utcfromtimestamp(pos_msec/1000.0)) and then add the previously captured offset to obtain the civil time in the desired zone.
- Store the resulting zone‑aware timestamp alongside the frame index (e.g., in a CSV, JSON side‑car, or as a text overlay on the frame).
Steps to preserve or inject timezone‑aware EXIF DateTimeOriginal when using imwrite
- After calling
cv2.imwrite, use an external tool to rewrite the EXIF block with a timezone‑aware string. Example with exiftool (command line):
exiftool -DateTimeOriginal="2025:09:26 14:32:10+02:00" image.jpg
- If you prefer to stay within Python, use
Pillow to read the saved image, modify the EXIF dict, and write it back:
from PIL import Image, ExifTags
import piexif
img = Image.open('image.jpg')
exif_dict = piexif.load(img.info.get('exif', b''))
# Create a timezone‑aware string, e.g. '2025:09:26 14:32:10+02:00'
exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = b'2025:09:26 14:32:10+02:00'
exif_bytes = piexif.dump(exif_dict)
img.save('image.jpg', exif=exif_bytes)
- For formats that do not support EXIF (e.g., PNG), store the timestamp in a separate side‑car file or embed it as a visible text overlay using
cv2.putText before saving.
Optional post‑processing for video files
If you need the timezone inside the video container, write the raw frames with cv2.VideoWriter and then use ffmpeg to add a user‑data tag or metadata entry:
ffmpeg -i raw.mp4 -metadata:s:v:0 timezone="+02:00" -c copy output.mp4
This does not alter frame timing; it only adds interpretive context.
Missing diagnostic detail
To confirm whether the above approach fits your workflow, please specify: Which programming language binding (Python, C++, Java, etc.) are you using for OpenCV? The answer influences whether you can use datetime with zoneinfo directly or need to rely on std::chrono and external libraries.