← Back to team overview

phatch-dev team mailing list archive

Re: variable border action

 

Hey,


> > I agree we should only convert when we have to. And only convert back to
> 'P'
> > if absolutely necessary. In this case the border action can convert to
> > 'RGBA' if the border is negative
> I agree
> > or if the image was mode 'p' with
> > transparency.
> As I didn't dive as deep in the subject as you, maybe I am forgetting
> something. Let's consider the case where the image has mode 'P' and
> transparency set. I thought it could be possible to keep the
> transparency color index (stored inside photo.info['transparency'])
> and add the border color to the palette (in case it was not part of it
> already). This will only fail if the palette has 255 colors (+1 for
> transparency) already, in which case a conversion to RGBA is needed by
> a lack of colors in the palette, but not by transparency. (Note that
> this would also be case for non transparent P images which have
> already 256 colors and to which e.g. the variable border action wants
> to add a color.)

That would have been the right way to do it if PIL created palettes with the
right size. I'm not an expert in this. But from playing around with convert.
It seems that getpalette always return a 256 colors palette no mater what
the image is actually using. We can't use palette.palette size to determine
unused colors because of the mentioned PIL bug. You can tell unused colors
in other ways. But then to make this work you need to do the following (or
something similar)

palette = im.getpalette()
size = len(im.palette.mode)
unused_color_index = getUnUsedColor(im)
if unused_color_index >= 0:
    palette[unused_color_index*size: unused_color_index * size + size] =
color
    newimg = Image.new('P', size, unused_color_index)
    newimg.putpalette(palette)
else:
   # No unused color was found convert to 'RGBA'

I don't have access to PIL to test this pseudo code, but I think it should
work. However, it seems overly complicated to me. If you think it is good
enough we can use it.

Best Regards,
Nadia


>
> One other reason to limit hidden conversions to a minimum, is that
> less experienced users don't expect and experienced users will be very
> critical against it if they think there is no reason to do so.
>
> But as I said, I might overlook something. So I am very curious to
> your reaction ;-)
>
> Stani
>
> --
> Phatch Photo Batch Processor - http://photobatch.stani.be
> SPE Python IDE - http://pythonide.stani.be
>
> _______________________________________________
> Mailing list: https://launchpad.net/~phatch-dev<https://launchpad.net/%7Ephatch-dev>
> Post to     : phatch-dev@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~phatch-dev<https://launchpad.net/%7Ephatch-dev>
> More help   : https://help.launchpad.net/ListHelp
>

Follow ups

References