한 번 이미지가 알파 투명도로 흰색 배경과 검은 색 배경 위에 겹쳐졌습니다. 원본 이미지가 아닌 결과 이미지 두 개에 액세스 할 수 있으며 원본을 검색하고 싶습니다.ImageMagick을 사용하여 두 개의 합성 이미지에서 원래의 마스크를 검색하십시오.
나는 Ruby 코드를 작성했는데, Ruby에있는 것만 큼 빠르다고 생각합니다.
if pixel_on_black == pixel_on_white
# Matching pixels indicate 100% opacity in the original.
original_pixel = pixel_on_black
elsif color_on_black == BLACK && color_on_white == WHITE
# Black on black and white on white indicate 0% opacity in the original.
original_pixel = TRANSPARENT
else
# Since it's not one of the simple cases, then we'll do some math.
# Fancy algebra tells us the following. (MAX_VALUE is the largest value
# a channel can have. So, in most cases, 255.)
# First, find the alpha value. This equation follows from the equations
# for composing on black and composing on white.
alpha = pixel_on_black.red - pixel_on_white.red + MAX_VALUE
# Now that we know the alpha value, undo its multiplicative effect on the
# pixel on black. By dividing. Ta da.
alpha_ratio = alpha/MAX_VALUE
original_pixel = Pixel.new
original_pixel.red = pixel_on_black.red /alpha_ratio
original_pixel.green = pixel_on_black.green/alpha_ratio
original_pixel.blue = pixel_on_black.blue/alpha_ratio
original_pixel.alpha = alpha
end
는 그래서 좋은이고, 그것은 작동하고 모든 : 여기에 픽셀로 기본 로직, 반복하는 픽셀입니다. 그러나이 코드는 매우 빠르게 실행되어야하며 Ruby에서 픽셀 반복은 허용되지 않습니다. 이 함수가 이미 어딘가에 존재하지 않는다면 트릭을 수행 할 일련의 ImageMagick 옵션을 생각해내는 것이 가장 좋습니다.
ImageMagick의 명령 줄 도구가 정말 강력하게 보이므로 -fx
또는 멋진 -function
인수가 위의 코드와 같은 역할을하기 때문에 ImageMagick의 명령 줄 도구를 지금 연구 중입니다. 나는 계속 노력할 것이지만 이미 모든 것을 함께 모으는 방법을 알고있는 ImageMagick 전문가가 있습니까?
편집
:가 나는-fx
버전 지금 실행 :
convert image_on_black.png image_on_white.png -matte -channel alpha -fx "u.r + 1 - v.r" -channel RGB -fx "(u.a == 0) ? 1 : (u/u.a)" output.png
원래 코드의 거의 정확한 번역, 채널로 깨진있다. 알파 채널을 반복하고 정확한 알파 값을 설정하십시오. 그런 다음 RGB 채널을 반복하고 채널을 알파 값으로 나눕니다 (0이 아닌 경우 0으로 나누면 오류가 발생하므로이 값을 1로 설정합니다.이 경우 1을 흰색으로 선택했습니다).
-fx
표현식이 각 픽셀에 대해 재평가되기 때문에이를 더 명시적인 인수로 변환해야합니다.