Python Forum

Full Version: remove all color but red, then replace it with black
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
hi, sorry for my bad English,
I googling it for hours and still don't understand how this thing works,

I like to remove all non-red of an image, then replace the red color with black,
this is the working syntax using software called ImageMagick,
convert "%~1" -fill white -fuzz 25%% +opaque red -fill black -opaque red "%~dpn1.Temp%~x1"
but I want the script 100% of python(and its packages),
so please anyone, help me
There is PythonMagick
Hi,

Red is RGB (255,0,0)
Or do you mean only the R in rgb ?

But I don't understand the question.
If you "remove all non- red", what do you replace it with ?
Then you are going to replace the (255,0,0) with black (0,0,0) ?

If we know what you want to do, it should be easy, probably using Pillow and numpy.

Paul
PythonMagick as posted is outdated.
Wand is and updated and simple ImageMagick binding.
thank you to all who replied,
to be honest, if there is any solid tutorial I will not ask here

@Axel_Erfurt can you show me how it works

@DPaul I can't really explain it, just imagine there is a jpeg of a rainbow with white background, then except red, removal all the all of color line with white, then replace red color with black, the result is a line color of black, (if I can reply with images from my PC I will upload it)
if using Pillow and NumPy. can do it, please tell me how to do it

@snippsat please tell me how to do it
I got pretty close. Perhaps you can tweek it a little further.
from PIL import Image
import numpy as np
img = Image.open('color_field.jpg')
width = img.size[0] 
height = img.size[1] 
for i in range(0,width):# process all pixels
	for j in range(0,height):
		data = img.getpixel((i,j))
		if data[0] < 235 :
			img.putpixel((i,j),(255, 255, 255))
		else :
			img.putpixel((i,j),(0, 0, 0))
img.show()
thank you BashBedlam for the reply
when I tried pic from your sample it worked great,
but when I tried it using random jpeg pictures, the result is not good,

sorry out of topic, how did you attach pictures? can you upload directly from your PC?
(Dec-27-2021, 03:03 AM)kucingkembar Wrote: [ -> ]but when I tried it using random jpeg pictures, the result is not good,
Well, if you throw away all colors except 1 and replace that with black...
It's not a work of art that is going to emerge.

Except if what you really want is to convert a picture into Black & white.
That is a slightly different ptocedure. The simplest form is to do x = (R+G+B)/3
for every pixel. R=x,G=x,B=x

It could also be that you are attempting a B&W conversion, using only the red channel.
That is possible, but then you need to do R=R, G=R, B=R

Paul
thank you for the reply DPaul,
sorry if I am rude and quite stupid,
can you provide the code, please
after researching the code BashBedlam provide,
I edit it and the code is working as I intended

from PIL import Image
import numpy as np
imgT = Image.open('rainbow.png')
img = imgT.convert("RGB")
width = img.size[0] 
height = img.size[1] 
for i in range(0,width):# process all pixels
    for j in range(0,height):
        data = img.getpixel((i,j))
        # data[0] = Red,  [1] = Green, [2] = Blue
        # data[0,1,2] range = 0~255
        if data[0] > 150 and data[1] < 50 and data[2] < 50 :
            #put black
            img.putpixel((i,j),(0, 0, 0))
        else :
            #Put white
            img.putpixel((i,j),(255, 255, 255))
img.show()
thank you again for all who reply, stay healthy and have a nice day
Pages: 1 2