← Back to team overview

phatch-dev team mailing list archive

Re: variable border action

 

On Tue, Jun 2, 2009 at 1:16 PM, Nadia Alramli <nadiana@xxxxxxxxx> wrote:
> 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.
I agree, so converting to RGBA in that case is fine too. However I
think it would be good if you or Erich report this issue on the PIL
mailing list. PIL 1.1.7 rc will come out soon and maybe this bug can
be fixed for this new release of PIL.

Thanks for clarifying,

Stani

-- 
Phatch Photo Batch Processor - http://photobatch.stani.be
SPE Python IDE - http://pythonide.stani.be



Follow ups

References