Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pngalpha

Two-pass alpha extraction tool for creating transparent PNG images.

흰색/검정색 배경 이미지 쌍에서 투명 PNG를 생성하는 도구입니다.


Download Antigravity / 안티그래비티 다운로드 / Antigravity ダウンロード

Usage / 사용법 / 使い方

Demo Video

Refer to the video above, open the cloned folder with Antigravity, and use the prompt below to generate images. It works with any language supported by Gemini.

"Generate a high-quality Japanese anime illustration style image of moist fruit in a glass bowl with rich mid-tones, using a low-angle shot. Strong backlighting is needed to express subsurface scattering. The background should be a solid color. Apply the transparent png generation workflow to sequentially generate the necessary images and convert them into a transparent png. The final output path is the examples directory of the antigravity project."

위 영상을 참고하여 클론한 폴더를 안티그래비티로 열고 아래의 프롬프트를 사용해 이미지를 생성합니다. 언어는 Gemini 가 지원하는 언어면 동작합니다.

"미드톤표현이 풍부한 고품질의 일본 애니 일러스트풍으로 유리 보울에 담긴 촉촉하게 젖어 있는 과일을 로우 앵글샷으로 그려줘 역광으로 인한 피하산란 표현필요 배경은 단색으로 하고 투명 png 생성 워크 플로우를 적용해 순차적으로 필요한 이미지 생성후 투명 png 로 변환해. 최종출력은 안티그라비티 프로젝트의 examples 경로야"

上の動画を参考に、クローンしたフォルダをAntigravityで開き、以下のプロンプトを使用して画像を生成してください。 言語はGeminiがサポートしている言語であれば動作します。

"ミッドトーン表現が豊かな高品質な日本のアニメイラスト風で、ガラスのボウルに入ったしっとりと濡れている果物をローアングルショットで描いて。逆光による表面下散乱の表現が必要。背景は単色にし、透明png生成ワークフローを適用して順次必要な画像を生成した後、透明pngに変換して。最終出力先はAntigravityプロジェクトのexamplesパスだよ"


Installation / 설치

Using uv (Recommended) / uv 사용 (권장)

This project can be installed directly from GitHub using uv tool.

이 프로젝트는 uv tool을 사용하여 GitHub에서 직접 설치할 수 있습니다.

uv tool install https://github.com/BK927/transparent-png-python.git

Development / 개발

To run from source:

소스에서 직접 실행하려면:

uv sync

Tool Usage / 도구 사용법

CLI

pngalpha <image_on_white> <image_on_black> <output_file>

Example / 예시

# General usage / 일반 사용
pngalpha white.png black.png output.png

# Running from source with uv / uv로 소스 실행
uv run pngalpha white.png black.png output.png

GUI (Simple) / GUI (간단 버전)

# Installed tool / 설치형 실행
pngalpha-gui

# Running from source with uv / uv로 소스 실행
uv run pngalpha-gui

Drag and drop the white-background image and black-background image into each box. As soon as both are set, conversion runs automatically. Output is saved automatically as <white_image_name>_transparent.png in the same folder.

흰 배경 이미지와 검정 배경 이미지를 각 박스에 드래그앤드랍하면, 두 이미지가 모두 설정되는 즉시 자동 변환됩니다. 출력은 같은 폴더에 <흰이미지파일명>_transparent.png로 자동 저장됩니다.

Arguments / Inputs / 인자

Argument Description 설명
image_on_white Image captured on white background 흰색 배경에서 촬영한 이미지
image_on_black Image captured on black background 검정색 배경에서 촬영한 이미지
output_file Output transparent PNG file 출력될 투명 PNG 파일

Algorithm / 알고리즘

This tool uses a two-pass alpha extraction technique to recover transparency from two images of the same subject photographed against white and black backgrounds.

이 도구는 투-패스 알파 추출 기법을 사용하여 흰색과 검정색 배경에서 촬영한 동일 피사체의 두 이미지로부터 투명도를 복원합니다.

Alpha Calculation / 알파 계산

The alpha value is calculated based on the color distance between corresponding pixels in both images.

알파 값은 두 이미지에서 대응하는 픽셀 간의 색상 거리를 기반으로 계산됩니다.

pixelDist = √[(Rw - Rb)² + (Gw - Gb)² + (Bw - Bb)²]
bgDist = √(255² + 255² + 255²) ≈ 441.67

alpha = 1 - (pixelDist / bgDist)

Where / 여기서:

  • (Rw, Gw, Bw) = RGB values from white background image / 흰 배경 이미지의 RGB 값
  • (Rb, Gb, Bb) = RGB values from black background image / 검정 배경 이미지의 RGB 값
  • bgDist = Maximum possible distance (white to black) / 최대 가능 거리 (흰색에서 검정색)

Principle / 원리

Pixel Type White BG Black BG Distance Alpha
Opaque (불투명) Same color Same color 0 1.0
Transparent (투명) White (255,255,255) Black (0,0,0) 441.67 0.0
Semi-transparent (반투명) Blended with white Blended with black 0 < d < 441.67 0 < α < 1
  • Opaque pixels appear identical on both backgrounds → distance = 0 → alpha = 1

  • Transparent pixels show the background color → distance = max → alpha = 0

  • Semi-transparent pixels show partial blending → proportional alpha

  • 불투명 픽셀은 두 배경에서 동일하게 보임 → 거리 = 0 → 알파 = 1

  • 투명 픽셀은 배경색을 그대로 보여줌 → 거리 = 최대 → 알파 = 0

  • 반투명 픽셀은 부분적으로 섞임 → 비례적인 알파 값

Color Recovery / 색상 복원

Once alpha is calculated, the original foreground color is recovered by un-premultiplying:

알파가 계산되면, 원본 전경색은 프리멀티플라이 해제를 통해 복원됩니다:

R_out = R_black / alpha
G_out = G_black / alpha
B_out = B_black / alpha

This works because on a black background (0,0,0), the observed color is:

이것이 작동하는 이유는 검정 배경(0,0,0)에서 관찰되는 색상이 다음과 같기 때문입니다:

C_observed = C_foreground × alpha + C_background × (1 - alpha)
C_observed = C_foreground × alpha + 0 × (1 - alpha)
C_observed = C_foreground × alpha

Therefore / 따라서:
C_foreground = C_observed / alpha

Requirements / 요구사항

  • Python 3.10+
  • uv (for dependency management)
  • Pillow, NumPy
  • tkinterdnd2 (for drag-and-drop GUI)

Workflow with AI Image Generation / AI 이미지 생성 워크플로우

When using AI image generation tools, follow this workflow to create transparent PNGs:

AI 이미지 생성 도구를 사용할 때, 다음 워크플로우를 따라 투명 PNG를 생성하세요:

Step 1: Generate Base Image / 기본 이미지 생성

Generate your subject on a neutral solid color background (not white or black) that contrasts with the subject.

피사체와 대비되는 중간색 단색 배경 (흰색/검정색 제외)에 생성합니다.

Example colors / 예시 색상: Blue #0066CC, Green #00CC66, Purple #6600CC

Step 2: Convert to White Background / 흰색 배경으로 변환

Use the base image as reference and change only the background to pure white.

기본 이미지를 참조로 사용하고 배경만 순수 흰색으로 변경합니다.

Step 3: Convert to Black Background / 검정색 배경으로 변환

Use the white background image as reference and change only the background to pure black.

흰색 배경 이미지를 참조로 사용하고 배경만 순수 검정색으로 변경합니다.

Step 4: Extract Alpha / 알파 추출

Run pngalpha with the white and black background images.

흰색과 검정색 배경 이미지로 pngalpha를 실행합니다.

pngalpha white.png black.png transparent_output.png

Workflow Diagram / 워크플로우 다이어그램

[Base Image]     [White BG]      [Black BG]      [Transparent PNG]
    🔵      →       ⚪       →       ⚫       →         🔲
 (neutral)      (reference)     (reference)       (final output)

Note / 참고: AI image generation may introduce slight variations between images, which can cause artifacts in the final result. For best results, use 3D rendering software or physical photography with fixed camera positioning.

AI 이미지 생성은 이미지 간에 약간의 변형이 발생할 수 있어 최종 결과에 아티팩트가 생길 수 있습니다. 최상의 결과를 위해 3D 렌더링 소프트웨어 또는 카메라 고정 실물 촬영을 사용하세요.


Tips / 팁

  1. Image alignment is critical - Both images must be pixel-perfectly aligned.

    이미지 정렬이 중요합니다 - 두 이미지는 픽셀 단위로 완벽하게 정렬되어야 합니다.

  2. Use even lighting - Avoid shadows or reflections that differ between shots.

    균일한 조명 사용 - 촬영 간에 다른 그림자나 반사를 피하세요.

  3. Camera must be fixed - Use a tripod to ensure identical framing.

    카메라 고정 필수 - 삼각대를 사용하여 동일한 프레이밍을 보장하세요.

  4. For rendered images - Simply export the same scene with white and black backgrounds.

    렌더링 이미지의 경우 - 동일한 장면을 흰색과 검정색 배경으로 각각 내보내세요.


Example / 예시

The examples/ folder contains a complete workflow demonstration using a magic potion bottle:

examples/ 폴더에 마법 포션 병을 사용한 완전한 워크플로우 예시가 포함되어 있습니다:

Step File Description 설명
1 01_potion_base.png Base image on purple background 보라색 배경의 기본 이미지
2 02_potion_white.png Converted to white background 흰색 배경으로 변환
3 03_potion_black.png Converted to black background 검정색 배경으로 변환
4 04_potion_transparent.png Final transparent PNG 최종 투명 PNG

Running the Example / 예시 실행

# Using uv
uv run pngalpha examples/02_potion_white.png examples/03_potion_black.png examples/output.png

Credits / 크레딧

This project is a Python port of the original C# project by octopus7. Special thanks for the excellent algorithm and documentation.

이 프로젝트는 octopus7의 원본 C# 프로젝트를 파이썬으로 포팅한 것입니다. 훌륭한 알고리즘과 문서를 제공해주셔서 감사합니다.

About

A tool for creating transparent PNGs using Antigravity and NanoBanana.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages