• Image background

    From saito@saitology9@gmail.com to comp.lang.tcl on Thu Oct 17 15:04:22 2024
    From Newsgroup: comp.lang.tcl

    I wonder if it is possible to remove the background from an image and
    make it transparent using Tk or a package? Then I can save it as a png
    image. The background in my case consists of a single color. Perhaps I
    could replace that color with an "alpha" color but how?

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Rich@rich@example.invalid to comp.lang.tcl on Thu Oct 17 19:35:07 2024
    From Newsgroup: comp.lang.tcl

    saito <saitology9@gmail.com> wrote:
    I wonder if it is possible to remove the background from an image and
    make it transparent using Tk or a package? Then I can save it as a png image. The background in my case consists of a single color. Perhaps I
    could replace that color with an "alpha" color but how?

    man n photo:

    imageName transparency subcommand ?arg arg ...?
    Allows examination and manipulation of the transparency
    information in the photo image. Several subcommands are
    available:

    imageName transparency get x y
    Returns a boolean indicating if the pixel at (x,y) is
    transparent.

    imageName transparency set x y boolean
    Makes the pixel at (x,y) transparent if boolean is true,
    and makes that pixel opaque otherwise.

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From saito@saitology9@gmail.com to comp.lang.tcl on Thu Oct 17 15:44:41 2024
    From Newsgroup: comp.lang.tcl

    On 10/17/2024 3:35 PM, Rich wrote:
    saito <saitology9@gmail.com> wrote:
    I wonder if it is possible to remove the background from an image and
    make it transparent using Tk or a package? Then I can save it as a png
    image. The background in my case consists of a single color. Perhaps I
    could replace that color with an "alpha" color but how?

    man n photo:

    imageName transparency subcommand ?arg arg ...?
    Allows examination and manipulation of the transparency
    information in the photo image. Several subcommands are
    available:


    Thank you - I missed this page. Does this command apply to non-png
    images? Initially the image will have no transparency, since the
    background is a solid, opaque color?

    The challenge in my case is I don't know where the background starts and
    what color it would be. The latter can be solved by involving the user perhaps.

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Paul Obermeier@obermeier@poSoft.de to comp.lang.tcl on Thu Oct 17 21:49:27 2024
    From Newsgroup: comp.lang.tcl

    Am 17.10.2024 um 21:04 schrieb saito:
    I wonder if it is possible to remove the background from an image and make it transparent using Tk or a package? Then I can save it as a png image. The background in my case consists of a single color. Perhaps I could replace that color with an "alpha" color but how?


    proc SetTransparentColor { phImg { red 255 } { green 255 } { blue 255 } } {
    set colorStr [format "#%02x%02x%02x" $red $green $blue]
    set y 0
    foreach row [$phImg data] {
    set x 0
    foreach pixel $row {
    if { $colorStr eq $pixel } {
    $phImg transparency set $x $y 1
    }
    incr x
    }
    incr y
    }
    }

    Paul
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From saito@saitology9@gmail.com to comp.lang.tcl on Thu Oct 17 15:52:16 2024
    From Newsgroup: comp.lang.tcl

    On 10/17/2024 3:35 PM, Rich wrote:
    imageName transparency set x y boolean
    Makes the pixel at (x,y) transparent if boolean is true,
    and makes that pixel opaque otherwise.


    On that same page, there is a -background option which is just the
    opposite of what I was looking for:

    -background color
    If the color is specified, the data will not contain any transparency information. In all transparent pixels the color will be replaced by the specified color.

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From saito@saitology9@gmail.com to comp.lang.tcl on Thu Oct 17 15:53:39 2024
    From Newsgroup: comp.lang.tcl

    On 10/17/2024 3:49 PM, Paul Obermeier wrote:

        proc SetTransparentColor { phImg { red 255 } { green 255 } { blue 255 } } {
            set colorStr [format "#%02x%02x%02x" $red $green $blue]
            set y 0
            foreach row [$phImg data] {
                set x 0
                foreach pixel $row {
                    if { $colorStr eq $pixel } {
                        $phImg transparency set $x $y 1
                    }
                    incr x
                }
                incr y
            }
        }

    Paul

    Awesome! Thanks a lot.

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Rich@rich@example.invalid to comp.lang.tcl on Thu Oct 17 19:56:51 2024
    From Newsgroup: comp.lang.tcl

    saito <saitology9@gmail.com> wrote:
    On 10/17/2024 3:35 PM, Rich wrote:
    saito <saitology9@gmail.com> wrote:
    I wonder if it is possible to remove the background from an image and
    make it transparent using Tk or a package? Then I can save it as a png
    image. The background in my case consists of a single color. Perhaps I
    could replace that color with an "alpha" color but how?

    man n photo:

    imageName transparency subcommand ?arg arg ...?
    Allows examination and manipulation of the transparency
    information in the photo image. Several subcommands are
    available:


    Thank you - I missed this page. Does this command apply to non-png
    images?

    It applies to all Tk photo images.

    Whether you can save the transparency to a file will depend upon the
    file format you choose to save the result into.

    Initially the image will have no transparency, since the
    background is a solid, opaque color?

    That depends upon what you initially load into the Tk photo. If you
    load a file that can store transparency, and if Tk's image load routine
    can load the transparency, you'll get whatever transparency is already present.

    The challenge in my case is I don't know where the background starts and what color it would be. The latter can be solved by involving the user perhaps.

    One option is to presume that the most common color is likely the
    background, but that assumes flat, single color, backgrounds.

    If by 'background' you mean something like a building behind people
    posing for a photo, then you are getting very deep into advanced image processing and/or AI image recognition to "find" the background in any automated way.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From saito@saitology9@gmail.com to comp.lang.tcl on Thu Oct 17 16:42:15 2024
    From Newsgroup: comp.lang.tcl

    On 10/17/2024 3:56 PM, Rich wrote:
    Thank you - I missed this page. Does this command apply to non-png
    images?

    It applies to all Tk photo images.

    Whether you can save the transparency to a file will depend upon the
    file format you choose to save the result into.


    Thank you. I know image processing is a rabbit hole. My question was
    much simpler. I wanted to determine what the background color is (if
    possible, and it is ok if not), and replace it with an alpha color. Paul Obermeier's code answers my question perfectly.

    Thanks again to both of you!


    --- Synchronet 3.20a-Linux NewsLink 1.114