python / cpython

The Python programming language
https://www.python.org
Other
62.46k stars 29.98k forks source link

Implementation of colorsys.rgb_to_yuv and vice versa #89933

Closed 7bd57a44-49bc-4346-bb63-842932cc2c90 closed 2 years ago

7bd57a44-49bc-4346-bb63-842932cc2c90 commented 2 years ago
BPO 45775
Nosy @terryjreedy, @vstinner, @merwok, @encukou, @tstolarski
PRs
  • python/cpython#29512
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields: ```python assignee = None closed_at = created_at = labels = ['type-feature', 'library', '3.11'] title = 'Implementation of colorsys.rgb_to_yuv and vice versa' updated_at = user = 'https://github.com/tstolarski' ``` bugs.python.org fields: ```python activity = actor = 'eric.araujo' assignee = 'none' closed = True closed_date = closer = 'vstinner' components = ['Library (Lib)'] creation = creator = 'thomas.stolarski' dependencies = [] files = [] hgrepos = [] issue_num = 45775 keywords = ['patch'] message_count = 12.0 messages = ['406067', '406247', '406252', '406263', '406540', '406544', '406845', '406854', '406855', '406864', '406865', '406867'] nosy_count = 5.0 nosy_names = ['terry.reedy', 'vstinner', 'eric.araujo', 'petr.viktorin', 'thomas.stolarski'] pr_nums = ['29512'] priority = 'normal' resolution = 'rejected' stage = 'resolved' status = 'closed' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue45775' versions = ['Python 3.11'] ```

    7bd57a44-49bc-4346-bb63-842932cc2c90 commented 2 years ago

    Since the implementation of rgb_to_yiq roughly 30 years ago now, the advent of HDTV has resulted in most broadcasting and video processing having moved towards Rec 709 (or BT.709).

    While I know colorsys has been on the chopping block for a while, it seemed a little silly that what is now arguably the most common color space used in video editors isn't available as part of this module, despite being a matrix mapping like much of the others.

    YUV is a bit more contentious on its definition than YIQ is, but it is still widely used to refer to Rec 709 and this is the ATSC standardization for HDTV.

    I've written a PR for both conversions and will add it to this ticket once it's up.

    terryjreedy commented 2 years ago

    A link to an authoritative reference with the formulas would be necessary to merge this. I inquired of pydev list whether improvement is allowed.

    vstinner commented 2 years ago

    A link to an authoritative reference with the formulas would be necessary to merge this.

    The PR says: "ATSC BT.709 standard constants".

    For exmaple, I found: https://en.wikipedia.org/wiki/Rec.\_709#Luma_coefficients

    "When encoding Y’CBCR video, BT.709 creates gamma-encoded luma (Y’) using matrix coefficients 0.2126, 0.7152, and 0.0722 (together they add to 1)."

    terryjreedy commented 2 years ago

    On pydev, Guido said "There was talk of deprecating colorsys, but PEP-594 now lists it under "modules to keep". Victor, do you know enough to review?

    encukou commented 2 years ago

    I argued for keeping colorsys: RGB/HLS/HSV are useful in several fields. Colorsys is a nice low-power battery to have around for e.g. picking a color for highlighting any kind of output.

    YIQ is the odd one out: it describes actual color, rather than instructions for a monitor. I think that if you need YIQ, YUV, CMYK, sRGB, XYZ or Lab, you should reach for a more specialized library. In other words, I don't see YIQ as an example to be followed.

    vstinner commented 2 years ago

    The GIMP image editor uses https://gegl.org/babl/ which supports tons of formats and supports color spaces, ICC profiles, etc.

    I would also prefer to keep colorsys simple and so not support ICC profiles for example.

    YUV is a common color encoding system. It's used by the JPEG image format and MPEG video format for example. https://en.wikipedia.org/wiki/YUV

    In terms of complexity and maintenance burden, supporting YUV (PR 29512) sounds reasonable me.

    If colorsys is limited to RGB/HLS/HSV, I'm not sure if it is useful. Nowadays, there are more color formats which are getting popular. If they are not supported, maybe it's better to just remove the module. I don't know.

    Well, I don't have a strong opinion on adding YUV support or not.

    encukou commented 2 years ago

    YUV is a common color encoding system. It's used by the JPEG image format and MPEG video format for example.

    As far as I understand it, the YUV/RGB conversion used by JPEG is not the same as the one used by MPEG, and neither of those is the Rec 709 YUV proposed here.

    YUV is used in video production and photography. If you need to work with it, you most likely need some kind of precise color matching. If colorsys implements the wrong one *for you* (even if it's the most commonly used one, and what HDTV uses), then the stdlib has failed you.

    Meanwhile, RGB/HLS is used by Web designers writing CSS, GUI authors giving you a big green button to click, or color pickers for your avatar's hair. They're designing for uncalibrated monitors. *That* is the target audience for colorsys. The image libraries these people use to read JPGs invariably convert to RGB and throw away any precise color information, which is meaningless on uncalibrated monitors.

    I cannot see a use case for YUV in colorsys that doesn't also require a more complex image processing library. It's that library that should provide the necessary colorspace conversions, depending on if it's dealing with JPEG or MPEG or HDTV.

    But please point out my mistakes and clear my doubts!

    vstinner commented 2 years ago

    Ok, let's reject this feature and restrict colorsys to simple color spaces which don't require profiles.

    vstinner commented 2 years ago

    You can write your own module on PyPI with more advanced features if you want to extend colorsys ;-)

    7bd57a44-49bc-4346-bb63-842932cc2c90 commented 2 years ago

    I figured this would probably be the case, but since YIQ also requires a profile (and the FCC one it implements is pretty weird for digital work), I thought I'd give it a shot anyway.

    Would it be worth moving the test/formatting changes over to a different ticket or should we just leave them?

    merwok commented 2 years ago

    I think that there’s no need to change the existing tests. The CPython project prefers to not do tiny improvements for their own sake.

    merwok commented 2 years ago

    Thank you for suggesting the improvement and working on a PR nonetheless! Hope the experience can be useful for other tickets in the future.