gpt4 book ai didi

shell - 循环图像,检测是否包含颜色,放入子文件夹

转载 作者:行者123 更新时间:2023-12-04 23:09:57 25 4
gpt4 key购买 nike

我的文件夹中有两种图像:一种是全黑的,另一种是黑底黄的 (#f8fa27)。我正在尝试将所有黄色图像放在一个子文件夹中。但我不知道这是如何适用的。
我想用 ImageMagick 或 FFMPEG 来实现它。如果可能,shell 是多余的,我希望通过 CMD 循环。如果您碰巧知道另一个也可以使用的选项,那也没有问题。
我读过 https://imagemagick.org/script/fx.php但我不知道如何以我糟糕的技能应用它。
现在编辑我设法用python修复它(糟糕的代码,但它有效)

import cv2
import os
import glob

#check if extension is png
for filename in glob.glob("*.png"):
#return to folder where all images are saved
os.chdir('C:/Users/.../.../images')
#make image black and white
image = cv2.imread(filename, 0)
#if image is fully black
if cv2.countNonZero(image) == 0:
print ("Black image, skipped")
else:
#colored image
print ("Colored image")
#restore true colors (rgb in my case, check wiki)
image = cv2.imread(filename, cv2.COLOR_BGR2RGB)
#folder to save colored images
os.chdir(os.getcwd()+"/yellow")
#save image to subfolder
cv2.imwrite(filename,image)
谢谢 :) !

最佳答案

您可以在 ImageMagick 中使用均值 = 0 的三元测试来测试图像是否全是均匀黑色的 rgb(0,0,0),如下所示:

test=$(convert image.suffix -format "%[fx:mean==0?1:0]" info:)
if [ $test -eq 1 ]; then
echo "fully black"
else
echo "not fully black"
如果它统一为其他颜色,则测试 std=0:
test=$(convert image.suffix -format "%[fx:standard-deviation==0?1:0]" info:)
if [ $test -eq 1 ]; then
echo "uniform color"
else
echo "not uniform color"
所以黑色图像中的黄色会失败。
如果您的黑色不均匀,则在 (0<=thresh<=1)) 范围内选择一些阈值并执行
test=$(convert image.suffix -format "%[fx:mean<$thresh?1:0]" info:)
if [ $test -eq 1 ]; then
echo "fully black"
else
echo "not fully black"

关于shell - 循环图像,检测是否包含颜色,放入子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71891514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com