일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- redux/toolkit
- 에뮬레이터 스킨
- Drawer
- ReactQuery
- oh-my-zsh
- useEffect
- 갤럭시 스킨
- bottom tabs
- 포트폴리오 사이트
- react native
- 리액트 쿼리
- iTerm2
- useCallback
- tabBarStyle
- rembg
- 이미지 배경 지우기
- useMemo
- reactnative
- Stack
- 리액트네이티브
- useState
- react
- bottom tabBar
- listeners
- mongoDB
- gitpages
- MonGo
- Hook 함수
- nodejs
- useRef
- Today
- Total
프론트엔드 개발자의 개발 놀이터
이미지 배경 지우는 기능 만들기 (feat. rembg, PIL) 본문
자주 사용하는 어플 중에 패션 아이템(옷, 모자, 신발 등등)의 이미지의 배경을 지워서 개인 옷장을 관리할 수 있는 어플이 있습니다.
해당 어플의 주요 기능이 이미지의 배경을 지워주고 각 아이템 이미지들로 스타일을 조합할 수 있도록 해주는 기능이 있는데,
해당 기능을 RN에 적용해보고 싶어서 찾다보니 Photoroom API를 찾게 되었지만 이미지 편집 갯수에 따라 요금이 발생하다보니 개인 프로젝트에 사용하기에는 부담이 되었습니다.
Remove Background API and Generative AI API by Photoroom
Enhance your e-commerce visuals and increase sales with Photoroom Remove Background API and Generative AI API. Leverage your existing assets to create a brand identity that shines through your product photos, with endless custom options. Free plan availabl
www.photoroom.com
다른 방법이 없을까 여러 검색을 통해 python의 rembg 라이브러리를 사용하여 이미지 배경을 지우는 기능을 손쉽게 만들 수 있는걸 알게 되었습니다.
GitHub - danielgatis/rembg: Rembg is a tool to remove images background
Rembg is a tool to remove images background. Contribute to danielgatis/rembg development by creating an account on GitHub.
github.com
RN에 사용할 수 있도록 API를 만들기 전 라이브러리를 사용하여 이미지 배경이 제대로 제거가 되는지 확인을 하기위해 rembg 사용법을 먼저 사용해 보았습니다.
(추가로 이미지 파일을 python 코드에서 손쉽게 처리할 수 있도록 도와주는 PIL 라이브러리를 사용하였습니다.)
1. rembg 설치
pip install rembg
2. PIL 설치
pip install Pillow
3. 사용법
# removeBg.py
from rembg import remove
from PIL import Image
input_path = 'images.png' # 코드 파일 경로에 배경을 지우고 싶은 이미지 이름을 지정
output_path = 'images_output.png' # 저장할 편집 이미지 이름 지정
input = Image.open(input_path) # 이미지 열기
output = remove(input) # 이미지 배경 지우기
print(output)
output.save(output_path) # 변경된 이미지 저장하기
4. 결과