3

How can I remove the alpha channel from jpg images automatically?

I am using a deep learning code based on PyTorch that requires 3 channels but my images have 4 channels. I saw this answer but I do not know how to convert all the images in a folder to a counterpart image that does not have an alpha channel.

Here is the error I receive:

[jalal@goku example]$ python img_to_vec_nearest_neighbors.py 
/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  return f(*args, **kwds)
/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  return f(*args, **kwds)
/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  return f(*args, **kwds)
/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  return f(*args, **kwds)
/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/transforms/transforms.py:188: UserWarning: The use of the transforms.Scale transform is deprecated, please use transforms.Resize instead.
Traceback (most recent call last):
  File "img_to_vec_nearest_neighbors.py", line 18, in <module>
    vec = img2vec.get_vec(img)
  File "../img_to_vec.py", line 43, in get_vec
    h_x = self.model(image)
  File "/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/models/resnet.py", line 139, in forward
  File "/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torch/nn/modules/conv.py", line 301, in forward
    self.padding, self.dilation, self.groups)
RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[1, 4, 224, 224] to have 3 channels, but got 4 channels instead

For example, how can I perform the following command for all the images I have in one folder? My images have the extensions jpg, jpeg, and png, but I don't mind if they all become jpg.

convert cat.png -background white -alpha remove cat.jpg

1 Answer 1

5
find . -name "*.jpeg" -exec convert "{}" -alpha off "{}" \;
find . -name "*.png" -exec convert "{}" -alpha off "{}" \;
find . -name "*.jpg" -exec convert "{}" -alpha off "{}" \;
1
  • 1
    Well, best is set this as accepted answer. it will be easier to find.
    – user.dz
    Sep 22, 2021 at 10:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .