Torchvision.transforms set fillcolor for centercrop

username: athaker
cooked: <p>Hi Torch Community,</p>
<p>This is my first post! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>How do I set the fillcolor for a CenterCrop to something other than (0,0,0)?</p>
<p>The example below shows using an affine transform and setting the fillcolor to be a custom color. However, I cannot set for CenterCrop to be anything other than PIL (0,0,0). Random crop has this ability with fill.</p>
<pre><code class="lang-auto">    white = (255, 255, 255)
    data_transforms = transforms.Compose([
        transforms.ToPILImage(),
        #transforms.RandomCrop((75, 75), fill=white),
        transforms.CenterCrop(300),
        transforms.RandomAffine((-30, 30), translate=(0.05, 0.05), fillcolor=white),
        transforms.ToTensor()
    ])
</code></pre>

username: Caruso
cooked: <p>Hi,</p>
<p>yes if you are right, you cant add the padding. I don’t know why you want to pad and not resize in this case but here is something that should work:</p>
<pre><code class="lang-auto">import torchvision.transforms.functional as F

class PadCenterCrop(object):
    def __init__(self, size, pad_if_needed=False, fill=0, padding_mode='constant'):
        if isinstance(size, (int, float)):
            self.size = (int(size), int(size))
        else:
            self.size = size
        self.pad_if_needed = pad_if_needed
        self.padding_mode = padding_mode
        self.fill = fill

    def __call__(self, img):

        # pad the width if needed
        if self.pad_if_needed and img.size[0] &lt; self.size[1]:
            img = F.pad(img, (self.size[1] - img.size[0], 0), self.fill, self.padding_mode)
        # pad the height if needed
        if self.pad_if_needed and img.size[1] &lt; self.size[0]:
            img = F.pad(img, (0, self.size[0] - img.size[1]), self.fill, self.padding_mode)

        return F.center_crop(img, self.size)
</code></pre>

username: athaker
cooked: <aside class="quote no-group" data-username="Caruso" data-post="2" data-topic="98098">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/caruso/48/24651_2.png" class="avatar"> Caruso:</div>
<blockquote>
<pre><code class="lang-auto">import torchvision.transforms.functional as F

class PadCenterCrop(object):
    def __init__(self, size, pad_if_needed=False, fill=0, padding_mode='constant'):
        if isinstance(size, (int, float)):
            self.size = (int(size), int(size))
        else:
            self.size = size
        self.pad_if_needed = pad_if_needed
        self.padding_mode = padding_mode
        self.fill = fill

    def __call__(self, img):

        # pad the width if needed
        if self.pad_if_needed and img.size[0] &lt; self.size[1]:
            img = F.pad(img, (self.size[1] - img.size[0], 0), self.fill, self.padding_mode)
        # pad the height if needed
        if self.pad_if_needed and img.size[1] &lt; self.size[0]:
            img = F.pad(img, (0, self.size[0] - img.size[1]), self.fill, self.padding_mode)

        return F.center_crop(img, self.size)
</code></pre>
</blockquote>
</aside>
<p>Code works brilliantly! I confirm I was able to handle different PIL values! Thank you for the help.</p>