代码之家  ›  专栏  ›  技术社区  ›  Andrew

如何从白色背景的JPG中提取照片?

  •  1
  • Andrew  · 技术社区  · 7 年前

    我有一个JPG文件,其中包含白色背景上的多张照片。

    我正在寻找一个CLI工具,它可以将照片从源JPG(不提供坐标)提取到单独的JPG文件中,以保持质量和照片分辨率。

    通过一些研究,我怀疑ImageMagick可以实现这一点,尽管我不确定CLI命令是否正确。

    如果有用的话,我将使用OSX 10.13.2并安装ImageMagick 7.0.7-28。

    Image extraction example

    1 回复  |  直到 7 年前
        1
  •  0
  •   fmw42    7 年前

    以下是在Unix中使用Imagemagick执行此操作的两种方法。我刚刚从图表中裁剪出了您的基础图像,因为我不确定它是否是您图像的一部分。如果它是图像的一部分,则必须先使用-trim将其修剪掉。

    输入:

    enter image description here

    第一个是我的脚本multicrop2:
    (-f 10是提取背景的模糊因子)
    (-u 3表示不尝试取消旋转结果)

    multicrop2 -f 10 -u 3 image.jpg resulta.jpg
    
    Processing Image 0
    Initial Crop Box: 113x84+81+89
    
    Processing Image 1
    Initial Crop Box: 113x67+144+10
    
    Processing Image 2
    Initial Crop Box: 113x66+10+11
    

    enter image description here enter image description here enter image description here

    第二种是使用Imagemagick连接的组件(这是我在脚本中使用的)

    其作用是:

    1) fuzzy flood fill the background to transparent (since jpg is loss and does not preserve a uniform background.
    2) change the color under the transparent to white and remove the transparency
    3) change anything not white to black
    4) apply -connected-components to throw out areas smaller than 400 pixel area and extract each bounding box and color
    5) if the color is gray(0), i.e. black, then crop the original image to the bounding box and save to disk
    
    
    OLDIFS=$IFS
    IFS=$'\n'
    arr=(`convert image.jpg -fuzz 10% -fill none -draw "matte 0,0 floodfill" \
    -background white -alpha background -alpha off \
    -fill black +opaque white -type bilevel \
    -define connected-components:verbose=true \
    -define connected-components:mean-color=true \
    -define connected-components:area-threshold=400 \
    -connected-components 4 null: | tail -n +2 | sed 's/^[ ]*//'`)
    IFS=$OLDIFS
    num=${#arr[*]}
    j=0
    for ((i=0; i<num; i++)); do
    bbox=`echo "${arr[$i]}" | cut -d\  -f2`
    color=`echo "${arr[$i]}" | cut -d\  -f5`
    if [ "$color" = "gray(0)" ]; then
    convert image.jpg -crop $bbox +repage resultb_$j.jpg
    j=$((j+1))
    fi
    done
    


    enter image description here enter image description here enter image description here

    编辑:添加实际图像的处理

    输入:

    enter image description here

    首先要注意的是,实际的两幅图像都在右侧,但有一条黑色边缘。还有一个在顶部。该黑色边缘将两个图像连接在一起,因此它们无法通过multicrop2脚本轻松分离。因此,您需要将右侧剃掉足够多的像素以删除该边缘。顶部还有边缘,如果你想的话可以把它剃掉。如果这样做,可以减少-d参数。d参数需要小于要提取的最小图像的面积,并大于任何其他小噪声或区域顶部的条纹。因此,我从右侧剪下20个像素,然后使用multicrop2,它的-d值非常大。我选择了-f的值8,由于背景不恒定,它似乎在一个相当窄的范围内。您可以添加-m save来查看脚本创建的掩码,以查看两个图像之间的良好分离。我在-c 20,20处设置处理种子,以避免图像顶部的黑色边框,这样脚本就可以很好地测量整体填充步骤的背景色。

    convert test.jpeg -gravity east -chop 20x0 tmp.png
    multicrop2 -c 20,20 -f 8 -d 100000 tmp.png result.jpg
    
    Processing Image 0
    Initial Crop Box: 2319x1627+968+2153
    
    Processing Image 1
    Initial Crop Box: 2293x1611+994+436
    

    enter image description here

    enter image description here