删除文件夹中所有 jpg 图像的 alpha 通道

删除文件夹中所有 jpg 图像的 alpha 通道

我怎样才能自动从 jpg 图像中删除 alpha 通道?

我正在使用基于 PyTorch 的深度学习代码,它需要 3 个通道,但我的图像有 4 个通道。我看到这个答案但我不知道如何将文件夹中的所有图像转换为没有 alpha 通道的对应图像。

这是我收到的错误:

[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

例如,我如何对一个文件夹中的所有图像执行以下命令?我的图像的扩展名是 jpg、jpeg 和 png,但我不介意它们都变成 jpg。

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

答案1

find . -name "*.jpeg" -exec convert "{}" -alpha off "{}" \;
find . -name "*.png" -exec convert "{}" -alpha off "{}" \;
find . -name "*.jpg" -exec convert "{}" -alpha off "{}" \;

相关内容