워라밸 중독자

[Python] 사진 및 영상 배경 쉽게 제거해주는 프로그램 (무료, 여러개 한번에) 본문

코딩, 딱 지금이 시작할때! (코딱지)

[Python] 사진 및 영상 배경 쉽게 제거해주는 프로그램 (무료, 여러개 한번에)

Ufungi 2023. 1. 12. 23:11

https://github.com/nadermx/backgroundremover

 

GitHub - nadermx/backgroundremover: Background Remover lets you Remove Background from images and video with a simple command li

Background Remover lets you Remove Background from images and video with a simple command line interface that is free and open source. - GitHub - nadermx/backgroundremover: Background Remover lets ...

github.com

일단 파이썬이 깔려있어야 사용 가능합니다.

저는 윈도우 11에 wsl2 설치후 Ubuntu에 프로그램을 설치했습니다. 다음 코드를 입력하여 설치했습니다.

pip install --upgrade pip
pip install backgroundremover

설치 시간이 좀 걸립니다.

이제 여러 새 사진의 배경을 한번에 지워보겠습니다.

 

# cd를 이용해 사진이 들어있는 폴더로 이동
# backgroundremover -i "input 파일 이름 또는 절대 경로" -o "output 파일 이름 또는 절대 경로"
backgroundremover -i "IMG_7971.JPG" -o "IMG_7971_modified.JPG"
backgroundremover -i "IMG_8072.JPG" -o "IMG_8072_modified.JPG"
backgroundremover -i "IMG_8089.JPG" -o "IMG_8089_modified.JPG"
backgroundremover -i "IMG_8138.JPG" -o "IMG_8138_modified.JPG"

ㅋㅋㅋㅋㅋㅋ처참하네요... 하지만 논문에 실을 실험 사진처럼 단순하거나 배경이 깔끔한 사진에 대해선 잘 작동한답니다^^

 

#!/bin/bash

# 입력 디렉토리와 출력 디렉토리 설정
input_dir="/home/snyoo/pictures/"
output_dir="/home/snyoo/pictures/output/"

# 출력 디렉토리가 없는 경우 생성
mkdir -p "$output_dir"

# 모든 파일에 대해 loop 실행
for file in "$input_dir"*; do
    # 파일 이름에서 확장자를 제외한 부분 추출
    filename=$(basename "$file")
    
    # output 파일 경로 설정
    output_file="${output_dir}${filename}"
    
    # backgroundremover 명령어 실행
    backgroundremover -i "$file" -o "$output_file"
    
    echo "Processed: $file -> $output_file"
done