Combine two single color images into one with CLI (imagemagick?) [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I'm generating "single" color images (B/W and red/white) of our todo schedule to write images to an 3-color e-ink screen, with my Raspberry Pi. First write the black one, then overwrite with red. This works really great, so no help required there.
But I would also like to have the same image available on a website. So I need to have my red image "overwrite" the black one, so I also have the black/red/white image the e-ink shows, as a png/bmp/jpg/whatever. For now we're just using the black one, but I want my overdue stuff in red as well.
I already have imagemagick for the conversions from SVG to PNG/BMP etc., and I'm guessing it should be able to do this. However, I could not find any examples. It's also hard to define the right search parameters, since a lot of people want to combine two images next to or below eachother, not actually combining them.
Does anyone here know how to do this? (I'm not stuck on ImageMagick, but that's the tool I already use)
My guess would be I'd need to make the white in the red/white transparent, then somehow layer it on top... But how?

Change the RED image to 50% opacity
convert red.png -alpha set -background none -channel A -evaluate multiply 0.5 +channel red2.png
Then overlay the 50%RED on the black
convert black.png red2.png -gravity center -composite all.png

Thanks to answers I found elsewhere on transparency, and above here from Bruce on 'composite', I ended up doing:
convert screen-output-red.png -fuzz 30% -transparent white -fill red -opaque black trans.png
composite trans.png screen-output.png combo.png
Haven't figured out if I could do it in one step, but this is good enough for now.

There are several ways to do this with ImageMagick. A very simple method is to make the white transparent then composite the red over the black.
magick img_black.png img_red.png -colorspace rgb \
-fuzz "10%" -transparent white -background white -flatten result.png
First it reads in both images and sets the working colorspace to "rgb" so the result will be color instead of grayscale.
Then it sets a "-fuzz" value of "10%", which is the amount of variance from pure white that will be affected by the "-transparent" operation. You can adjust this amount to suit your needs.
Next the "-transparent white" operation removes all the white and near-white specified by the fuzz value. Since that removes the white from both images, we add a background color of white so the result will be flattened onto that.
Finish by flattening the red image onto the black image onto a white background and write the result to a file.
This command uses ImageMagick v7 on a *nix system. To use it with IMv6 change "magick" to "convert". To use this command in Windows change the continued-line backslash "\" to a caret "^".

Related

Is ZBar unable to recognize colored barcodes?

I am using ZBar (http://zbar.sourceforge.net/) in one of my project and I noticed that the library is unable to recognize barcodes if they are colored: let's say yellow background, and blue foreground (the bars). The application requires to have colored barcodes when impressed on a paper label.
Is there a way to trick the issue, or is there another library that make this thing possible?
NOTES: I am using Python 3.7.1 for this application.
Zbar processes pictures in black and white, so it would be possible to edit the pictures so that they recognize the QR code better. You could edit the image so that the pixels differ from white to a certain percentage, black. All the other Prxel you dye white. Darduch would have to give a good contrast.
Here possible formula, as an example:
(R/255*100)>6||(G/255*100)>6||(B/255*100)>6
How big the deviation must be, you have to test.

Given a gray value, what color provides highest visual contrast?

Assume that I've a gray image, and I want to draw e.g. text on it. Now the image has some dark and some bright regions. So if I choose for every character a separate color, in what way do I compute such a color to gain highest contrast of the text?
A pragmatic approach is to use yellow. (I don't know why, but its often used for subtitles in movies and documentaries)
Furthermore I could darken the yellow in regions of bright background, and highlight it in regions of dark background. But this may provide some layer-effects.
I know that the color space may be important. I start with an RGB gray value, but LAB, HSV, or HSL may be better suited to compute the optimal color.
EDIT:
As there was the question for a useful use-case: I really do not want to paint letters of text in different color. It is about color choosing for particular glyphs on gray textured background. (E.g. an MR image.)
The simplest answer to your question is to maximize the distance between the background and your text color.
If you convert to HSL, you can do this by maximizing the distance between L (V in HSV). And all that requires is to select white when the background lightness is less than 50% and black otherwise. Here is an example: http://jsfiddle.net/E2kU4/
if(bgLightness < 50){
color = "white";
}else{
color = "black";
}
I think that pretty much solves it, but on to a few other points:
I'm not sure what the use case is for this. A word with different colored characters might look really bad. Typically, subtitles select a single color for consistency.
Yellow does stand out against a black and white image because of its saturation. Furthermore (and I'm not sure how to put this into words exactly), yellow has a really high chroma compared to other colors with similar lightness. It is best demonstrated on the HUSL page; by the way, HUSL is a great library for creating readable colors.
Yellow easily contrasts with dark colors because it is very light. It doesn't contrast with light colors as well, but that is usually solved by adding a shadow/outline in subtitles. Another example: http://jsfiddle.net/E2kU4/1/
But you can apply the same technique (of applying a shadow or outline) to the black/white example for maximum contrast. Now, the outline has the maximal contrast against the text. The outline stands out from the background too, unless those colors are similar, in which case the contrast is already extremely high (e.g. Near black background, black outline, white text) http://jsfiddle.net/E2kU4/2/
Lastly, converting to and from HSL/RGB should be trivial. There are plenty of libraries to do it.

Is there a library for c++ (or a tool) to reduce colors in a PNG image with alpha values?

I have a PNG-Image with alpha values and need to reduce the amount of colors. I need to have no more than 256 colors for all the colors in the image and so far everything I tried (from paint shop to leptonica, etc...) strips the image of the alpha channel and makes it unusable. Is there anything out there that does what I want ?
Edit: I do not want to use a 8-bit palette. I just need to reduce the numbers of color so that my own program can process the image.
Have you tried ImageMagick?
http://www.imagemagick.org/script/index.php
8-bit PNGs with alpha transparency will only render alpha on newer webbrowsers.
Here are some tools and website that does the conversion:
free pngquant
Adobe Fireworks
and website: http://www.8bitalpha.com/
Also, see similar question
The problem you describe is inherent in the PNG format. See the entry at Wikipedia and notice there's no entry in the color options table for Indexed & alpha. There's an ability to add an alpha value to each of the 256 colors, but typically only one palette entry will be made fully transparent and the rest will be fully opaque.
Paint Shop Pro has a couple of options for blending or simulating partial transparency in a paletted PNG - I know because I wrote it.

Why rgb and not cmy? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
Seeing as how the three primary colors are cyan, magenta and yellow (CMY), why do monitors and almost all the GUI components out there use red, green and blue (RGB)? (If I'm not mistaken, printers use the CMYK model.)
Is there a historical, hardware/software, or other reason for it?
There's a difference between additive colors (http://en.wikipedia.org/wiki/Additive_color) and subtractive colors (http://en.wikipedia.org/wiki/Subtractive_color).
With additive colors, the more you add, the brighter the colors become. This is because they are emitting light. This is why the day light is (more or less) white, since the Sun is emitting in almost all the visible wavelength spectrum.
On the other hand, with subtractive colors the more colors you mix, the darker the resulting color. This is because they are reflecting light. This is also why the black colors get hotter quickly, because it absorbs (almost) all light energy and reflects (almost) none.
Specifically to your question, it depends what medium you are working on. Traditionally, additive colors (RGB) are used because the canon for computer graphics was the computer monitor, and since it's emitting light, it makes sense to use the same structure for the graphic card (the colors are shown without conversions). However, if you are used to graphic arts and press, subtractive color model is used (CMYK). In programs such as Photoshop, you can choose to work in CMYK space although it doesn't matter what color model you use: the primary colors of one group are the secondary colors of the second one and viceversa.
P.D.: my father worked at graphic arts, this is why i know this... :-P
The difference lies in whether mixing colours results in LIGHTER or DARKER colours. When mixing light, the result is a lighter colour, so mixing red light and blue light becomes a lighter pink. When mixing paint (or ink), red and blue become a darker purple. Mixing paint results in DARKER colours, whereas mixing light results in LIGHTER colours.
Therefore for paint the primary colours are Red Yellow Blue (or Cyan Magenta Yellow) as you stated. Yet for light the primary colours are Red Green Blue. It is (virtually) impossible to mix Red Green Blue paint into Yellow paint, or mixing Red Yellow Blue light into Green light.
The basic colours are RGB not RYB. Yes most of the softwares use the traditional RGB which can be used to mix together to form any other color i.e. RGB are the fundamental colours (as defined in Physics & Chemistry texts).
The printer user CMYK (cyan, magenta, yellow, and black) coloring as said by #jcomeau_ictx.
You can view the following article to know about RGB vs CMYK: RGB Vs CMYK
A bit more information from the extract about them:
Red, Green, and Blue are "additive
colors". If we combine red, green and
blue light you will get white light.
This is the principal behind the T.V.
set in your living room and the
monitor you are staring at now.
Additive color, or RGB mode, is
optimized for display on computer
monitors and peripherals, most notably
scanning devices.
Cyan, Magenta and Yellow are
"subtractive colors". If we print
cyan, magenta and yellow inks on white
paper, they absorb the light shining
on the page. Since our eyes receive no
reflected light from the paper, we
perceive black... in a perfect world!
The printing world operates in
subtractive color, or CMYK mode.
the 3 additive colors are in fact red, green, and blue. printers use cmyk (cyan, magenta, yellow, and black).
and as http://en.wikipedia.org/wiki/Additive_color explains: if you use RYB as your primary colors, how do you make green? since yellow is made from equal amounts of red and green.
This is nothing to do with hardware nor software. Simply that RGB are the 3 primary colours which can be combined in various ways to produce every other colour. It is more about the human convention/perception of colours which carried over.
You may find this article interesting.

How to change the colors of a PNG image easily? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have PNG images that represent playing-cards.
They are the standard colours with Clubs and Spades being blank and Diamonds and Hearts being red.
I want to create a 4-colour deck by converting the the Clubs to green and the Diamonds to blue.
I don't want to re-draw them but just changing them by hand seems a lot of work since the colours are not all "pure" but graded near the edges.
How do I do it?
Photoshop - right click layer -> blending options -> color overlay
change color and save
This should be fairly straightforward in the gimp http://gimp.org/
First make sure your image is RGB (not indexed color) then use the "color to alpha" feature to turn the clubs/diamonds clear, then fill or set the background or whatever to get the color you want.
If you are going to be programming an application to do all of this, the process will be something like this:
Convert image from RGB to HSV
adjust H value
Convert image back to RGB
Save image
Use Photoshop, Paint.NET or similar software and adjust Hue.
Ok guys it can be done easy in photoshop.
Open png photo and then check image -> mode value(i had indexed color). Go image -> mode and check rgb color. Now change your color EASY.
If you are like me and Photoshop is out of your price range or just overkill for what you need. Acorn 5 is a much cheaper version of Photoshop with a lot of the same features. One of those features being a color change option. You can import all of the basic image formats including SVG and PNG. The color editing software works great and allows for basic color selection, RBG selection, hex code, or even a color grabber if you do not know the color. These color features, plus a whole lot image editing features, is definitely worth the $30. The only downside is that is currently only available on Mac.

Resources