This is what I like to do with code. Nothing is manually done in the process with Photoshop, so I think there is a way? but can’t quite figure it out.
This is what I did in Python:
from PIL import Image
im_rgb = Image.open('lena.jpg')
im_a = Image.open('frame.png').convert('L').resize(im_rgb.size)
im_rgba = im_rgb.copy()
im_rgba.putalpha(im_a)
im_rgba.save('final.png')
but I’m looking for a solution in Java/Kotlin on Android Studio while I could live with a sample in Dart or C++ as well.
2
Answers
`
I figured it out by myself on Python. but it is not really as complete as I wanted it to be initially. I would still like to know how to do it with Java/Kotlin on Android Studio or with C++ or Dart.
What OP described, I know from GIMP where it is called Color to Alpha.
While I used that command from time to time I tried to imagine how this could be implemented.
Multiple approaches come into my mind:
Out of curiosity, I wrote a sample application to try this out.
First a C++ code for color to alpha transformation:
imageColorToAlpha.h
:and the corresponding
imageColorToAlpha.cc
:This image manipulation code is based on the C++
std
library only.This is intended to make the code as exemplary and re-usable as possible.
However, the code for decoding image file formats is often neither short nor simple.
Hence, I wrote a wrapper application in Qt to show this in action.
Qt provides image support as well as the frame work for a desktop application and seemed to me as most appropriate for this task (beside of the fact that I’ve some experience with it).
The Qt wrapper application
testQImageColorToAlpha.cc
:and a helper class
qColorButton.h
:When started, a default image is loaded and the simple matching is applied:
I downloaded the sample image from jloog.com/images/.
The result looks a bit poor.
The white background is matched but there appear white artefacts around black drawing.
This results from sampling where pixels which covered the drawing as well as the background got respective shades of gray.
So, a better approach is to turn the distance from pivot color into a respective alpha value whereby the threshold defines the range as well as a limit upto that colors shall be considered:
That looks better.
Now, I became curious how well this works in “real” photos:
The result is better when I was afraid.
However, it shows the limits of the approach I got so far.
Update:
While I was researching the web to get the precise conversion from RGB to HSV, I learnt a lot about the different HSL and HSV models I was not aware of before.
Finally, I stumbled into Color difference where I found some interesting statements:
…
So, I discarded the idea with matching in HSV space.
Instead, I made a very simple extension which IMHO provides a significant improvement concerning monochrom drawings:
The pixels with mixed drawing and background are transformed into shades of alpha but the RGB values are left untouched.
This is not quite correct because it should actually become the foreground color (pencil color) blended with alpha.
To fix this, I added an option to override the RGB values of the output with a color of choice.
This is the result with overridden color:
Btw. it allows a nice little extra effect – the pencil color can be modified:
(The above sample source code has been updated to reflect the last changes.)
To build the sample, either CMake can be used with this
CMakeLists.txt
:which I used to build it in VS2017.
Alternatively, a minimal Qt project file
testQImageColorToAlpha.pro
:which I tested in cygwin: