Python Forum
remove all color but red, then replace it with black
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
remove all color but red, then replace it with black
#1
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
Reply
#2
There is PythonMagick
Reply
#3
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#4
PythonMagick as posted is outdated.
Wand is and updated and simple ImageMagick binding.
Reply
#5
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
Reply
#6
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()
kucingkembar likes this post

Attached Files

Thumbnail(s)
       
Reply
#7
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?
mikefirth likes this post
Reply
#8
(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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#9
thank you for the reply DPaul,
sorry if I am rude and quite stupid,
can you provide the code, please
Reply
#10
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
BashBedlam likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 1,041 Oct-25-2023, 09:09 AM
Last Post: codelab
  Checkbuttons always come up as black boxes regardless of the state kenwatts275 5 4,746 Jul-07-2020, 08:00 PM
Last Post: kenwatts275
  How to use nb-black python cde formatter ErnestTBass 3 6,852 Jun-04-2020, 03:51 PM
Last Post: ErnestTBass
  Finance: Black Scholes Model not working pwt 5 3,944 May-27-2020, 10:14 AM
Last Post: buran
  after using openpyxl to add colors to script, black shows up white online in excel Soundtechscott 1 3,698 Jun-08-2019, 10:33 PM
Last Post: Soundtechscott
  Because the emoji appears black and white at the exit ? nerd 3 5,625 Jan-28-2019, 11:34 PM
Last Post: nerd
  How to remove whitespace from a string when .replace and .strip do not work winnetrie 7 4,484 Jan-05-2019, 08:44 AM
Last Post: DeaD_EyE
  Search & Replace - Newlines Added After Replace dj99 3 3,414 Jul-22-2018, 01:42 PM
Last Post: buran
  Need to replace (remove) Unicode characters in text ineuw 1 8,607 Jan-02-2018, 08:01 PM
Last Post: micseydel
  Replace only '-' values with NaN, don't remove minus sign for negative numbers. rajpython 2 6,068 Sep-10-2017, 01:23 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020