Pillow: The fill color in Image.transform doesn't work.

Created on 28 Nov 2013  路  12Comments  路  Source: python-pillow/Pillow

Exactly what the issue header says, the fill option in Image.transform() doesn't seem to work. Here is what i have been trying:

transd = img.transform(size, Image.PERSPECTIVE, coeffs,
                                  Image.BICUBIC, fill=255)
# coeffs is a numpy array that was calculated in another function

The transform works perfectly, it's only the fill color that doesn't work, no matter what color i try, it always ends up being black.

Most helpful comment

@rickyhan Ahh, I see. I just realized that since I don't need the alpha channel, and since everything over the image boundaries is memset to 0, I can get a mask of the outside image pixels by adding an alpha channel to my image before the transform and setting alpha for all pixels to 255. This way all the pixels outside the image will get alpha channel values of 0 and I can extract a mask to replace those values with the value of my choosing :). Thanks for the tip though!

All 12 comments

Are you sure fill accepts an integer (and not a boolean)? If not, check the Image.py source.

@wiredfool Thanks, i had also traced/checked that already. That's why i was confused, about how to move forward after @aclark4life 's comment :) Are you guys considering this a non-issue, or not expecting to fix it or waiting for effbot to fix? There are ofcourse workarounds, using masks, and pastes. But this would have been nice if it worked by itself.
ps- using either 0 or 1 has no effect on the fill.

Generally I'd consider the behavior of an unwired, undocumented option a won't fix. I'm not sure why the option is there, it's likely to be something back from the PIL era.

Additionally, I strongly suspect that the actual function is redundant now that we're memsetting new images to 0.

I think the code would do what you wanted it to do if the fill was disabled at the _imaging.c level, and a fill color was added to the new Image line in https://github.com/python-imaging/Pillow/blob/master/PIL/Image.py#L1634 . I'd lean to making the argument fillcolor, since it's got a different meaning than the current ignored fill parameter.

:+1: Agree with the fillcolor suggestion, hopefully i or someone else can find the time to add the feature.

So is it possible to set the fill to transparent?

The default parameter for fill is 1 and setting it to 0 does nothing.

Here is a workaround:

http://stackoverflow.com/questions/5252170/specify-image-filling-color-when-rotating-in-python-with-pil-and-setting-expand

# original image
img = Image.open('test.png')
# converted to have an alpha layer
im2 = img.convert('RGBA')
# rotated image
rot = im2.rotate(22.2, expand=1)
# a white image same size as rotated image
fff = Image.new('RGBA', rot.size, (255,)*4)
# create a composite image using the alpha layer of rot as a mask
out = Image.composite(rot, fff, rot)
# save your work (converting back to mode='1' or whatever..)
out.convert(img.mode).save('test2.bmp')

Even better:

Image.paste(im, box=(x,y), mask=im)

@rickyhan Hey, I am currently fighting with the same issue. Did you figure out a performant workaround for this? Unfortunately I can't deduce what you did exactly from your last comment :)

@Zerphed not sure about performance. regarding the above line, Image is the one you want to paste im onto iirc.

@rickyhan Ahh, I see. I just realized that since I don't need the alpha channel, and since everything over the image boundaries is memset to 0, I can get a mask of the outside image pixels by adding an alpha channel to my image before the transform and setting alpha for all pixels to 255. This way all the pixels outside the image will get alpha channel values of 0 and I can extract a mask to replace those values with the value of my choosing :). Thanks for the tip though!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nomarek picture nomarek  路  3Comments

thinrhino picture thinrhino  路  3Comments

SysoevDV picture SysoevDV  路  3Comments

HansHirse picture HansHirse  路  3Comments

indirectlylit picture indirectlylit  路  4Comments