-5

I am trying to convert any .png images with a transparent background to a white background. however I am getting an error that says tuple object is not callable.

I have tried this:

    def transparent_to_white(img): 
     color = (255, 255, 255)
      for x in range(img.size()): 
       for y in range(img.size()): 
        r, g, b, a = img.getpixel((x, y))
         if a == 0: 
           img.putpixel((x, y), color)
     return img  

but I get this error:

Original Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/torch/utils/data/_utils/worker.py", line 302, in _worker_loop
    data = fetcher.fetch(index)
  File "/usr/local/lib/python3.8/dist-packages/torch/utils/data/_utils/fetch.py", line 58, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/usr/local/lib/python3.8/dist-packages/torch/utils/data/_utils/fetch.py", line 58, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/content/gdrive/My Drive/All_Deep_Learning/PythonCustomLibraries/pix2pixdatasetlib.py", line 49, in __getitem__
    y_label = self.resize(transparent_to_white(y_label))
  File "/content/gdrive/My Drive/All_Deep_Learning/PythonCustomLibraries/pix2pixdatasetlib.py", line 33, in transparent_to_white
    for x in range(img.size()):
TypeError: 'tuple' object is not callable

I am called it in my dataset class :

class Pix2PixDataset(Dataset):
 def __init__(self, data_points, transforms = None):
 self.data_points = data_points
 self.transforms = transforms
 self.resize = T.Resize((512,512))

def __getitem__(self, index) :
 image, y_label = process_images(self.data_points[index].reference_image, self.data_points[index].drawing )
 image = self.resize(image)
 y_label = self.resize(transparent_to_white(y_label))

 if self.transforms:
  image = self.transforms(image)
  y_label = self.transforms(y_label)
 return(image, y_label)
def __len__(self):
  return len(self.data_points)

I tried removing the open and close parenthesis but that did not help, I still get the same error

TypeError: Caught TypeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/torch/utils/data/_utils/worker.py", line 302, in _worker_loop
    data = fetcher.fetch(index)
  File "/usr/local/lib/python3.8/dist-packages/torch/utils/data/_utils/fetch.py", line 58, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/usr/local/lib/python3.8/dist-packages/torch/utils/data/_utils/fetch.py", line 58, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/content/gdrive/My Drive/All_Deep_Learning/PythonCustomLibraries/pix2pixdatasetlib.py", line 49, in __getitem__
    y_label = self.resize(transparent_to_white(y_label))
  File "/content/gdrive/My Drive/All_Deep_Learning/PythonCustomLibraries/pix2pixdatasetlib.py", line 33, in transparent_to_white
    for x in range(img.size()):
TypeError: 'tuple' object is not callable
  • 1
    `size` is a tuple, not a function, so (as the error clearly states) you can't call it. – Scott Hunter Dec 27 '22 at 19:46
  • @ Scott Hunter oh ! how to I get the height and width? Like this : range(zip(*img.size())[0]) and range(zip(*img.size())[1]) ? – Brandon Dec 27 '22 at 19:53
  • @Brandon No, img.size is a tuple itself, so img.size[0] _should_ be width and img.size[1] _should_ be height, or something along those lines... You could possibly unpack somewhere along the way. – user19513069 Dec 27 '22 at 19:58

1 Answers1

0

Disclaimer: I'm assuming img is an instance of Image class, from module PIL or it's fork Pillow

img.size is a tuple. For example, if you do:

print(img.size)

It prints a tuple with (width, height).

So, your code could be

def transparent_to_white(img):
    color = (255, 255, 255)
    width, height = img.size      # unpacking width/height beforehand
    for x in range(width):        # using unpacked values in range
        for y in range(height)):  # same as above
            r, g, b, a = img.getpixel((x, y))
            if a == 0:
                img.putpixel((x, y), color)
    return img

Or, alternatively, you could store x and y into a tuple of coordinates, to simplify passing it around:

def transparent_to_white(img):
    color = (255, 255, 255)
    width, height = img.size      # unpacking width/height beforehand
    for x in range(width):        # using unpacked values in range
        for y in range(height)):  # same as above
            coords = (x, y)                    # tuple of coordinates
            r, g, b, a = img.getpixel(coords)  # used here
            if a == 0:
                img.putpixel(coords, color)    # and here
    return img
user19513069
  • 317
  • 1
  • 9
  • Thanks that works, I am getting another error about : r, g, b, a = img.getpixel(x, y) TypeError: getpixel() takes 2 positional arguments but 3 were given I am not sure what that is about, but should I ask another question ? seeing as how this question has been answered ? – Brandon Dec 27 '22 at 20:13
  • @Brandon You're using `img.getpixel(x, y)` or `img.getpixel((x, y))`? I _believe_ (but not sure) you should be using the second option. Edit: Yeah I just tested. Assuming it's Image from PIL, getpixel It's a method, and because of that, there's the implicit `self` argument. The second argument (the one you supply) is expected to be a tuple with two coordinates. So yes, using `img.getpixel((x, y))` should work. – user19513069 Dec 27 '22 at 20:35
  • 1
    Are you sure that `r, g, b, a = img.getpixel(x, y)` caused the error? Also, you should try to solve the problem yourself before posting a question on stack overflow. Here is a [ressource](https://stackoverflow.com/questions/23944657/typeerror-method-takes-1-positional-argument-but-2-were-given) you might find useful. – Nonlinear Dec 27 '22 at 20:36
  • @nonlinear yes, I just ran it and it says not enough values to unpack – Brandon Dec 29 '22 at 16:13
  • @Brandon Try to print the return from `img.getpixel(coords)`, without unpacking. Also, try to print the property `img.mode`. Maybe your PNG doesn't have alpha. – user19513069 Dec 29 '22 at 17:14