Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The fill color in Image.transform doesn't work. #428

Closed
bool-dev opened this issue Nov 28, 2013 · 13 comments
Closed

The fill color in Image.transform doesn't work. #428

bool-dev opened this issue Nov 28, 2013 · 13 comments

Comments

@bool-dev
Copy link

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.

@aclark4life
Copy link
Member

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

@wiredfool
Copy link
Member

@bool-dev
Copy link
Author

bool-dev commented Dec 2, 2013

@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.

@wiredfool
Copy link
Member

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.

@bool-dev
Copy link
Author

bool-dev commented Dec 3, 2013

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

@0b01
Copy link

0b01 commented Jan 13, 2017

So is it possible to set the fill to transparent?

@0b01
Copy link

0b01 commented Jan 13, 2017

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

@0b01
Copy link

0b01 commented Jan 13, 2017

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')

@0b01
Copy link

0b01 commented Jan 13, 2017

Even better:

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

@Zerphed
Copy link

Zerphed commented Oct 23, 2017

@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 :)

@0b01
Copy link

0b01 commented Oct 23, 2017

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

@Zerphed
Copy link

Zerphed commented Oct 23, 2017

@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!

@jdbcode
Copy link

jdbcode commented Jul 14, 2021

For those finding themselves here wondering about setting fill color, use the fillcolor parameter to set fill color on background pixels. It was added in #2852.

transd = img.transform(size, Image.PERSPECTIVE, coeffs,
                                  Image.BICUBIC, fillcolor=(150, 10, 30))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants