-1

I want to load the image using img=cv2.imread('....jpg') and change the colors of the pixels whose:

  • x coordinate divide by 2 without remainder.

  • y coordinate divide by 3 without remainder

I'm using Python 3 in Anaconda/Jupyter.

Any suggestions?

mousetail
  • 7,009
  • 4
  • 25
  • 45
  • 4
    Sounds like a riddle. Please show your input image, your latest/greatest code and expected result. Thank you. – Mark Setchell Dec 18 '20 at 20:51
  • from PIL import Image import sys img = Image.open('moo.jpg') img = img.convert("RGB") white = (255,255,255) width, height = img.size for x in range(width): for y in range(height): if img.getpixel( (x,y) ) == (2,3): img.putpixel( (x,y), white) img.show() – Nadīna Tarnovska Dec 18 '20 at 21:14
  • I also tried this(from PIL import Image import sys img = Image.open('moo.jpg') img = img.convert("RGB") pixdata = img.load() # change with your color for j in range(img.size[1]): for i in range(img.size[0]): if pixdata[i, j] == (2, 3): pixdata[i, j] = (255,255, 255) img.show()) – Nadīna Tarnovska Dec 18 '20 at 21:15
  • 2
    Please don't put code in comments. It's unformatted and difficult to read. Instead, click `edit` under your question and update all the details in there where everyone can see it and understand your question without needing to search through the comments. Thank you. – Mark Setchell Dec 18 '20 at 21:48
  • 1
    Welcome to Stack Overflow. Please read the information guides in the **help center** (https://stackoverflow.com/help), in particular, "How to Ask A Good Question" (https://stackoverflow.com/help/how-to-ask) and "How to create a Minimal, Reproducible Example" (https://stackoverflow.com/help/minimal-reproducible-example). – fmw42 Dec 19 '20 at 00:30

2 Answers2

1

I strongly suggestion to have a look to these Stack Overflow answers:

Your are asking how change color of image with:

  • x Coordinate is a divisor of 2 (hence no reminder)

  • Y Coordinate is a divisor of 3 (hence no reminder)

I suggest you to look at @Mark Setchell answer here.

Federico Baù
  • 6,013
  • 5
  • 30
  • 38
0

The most straight forward way is to go through x,y pixels in nested for loops and modify the Mat image container whenever there is match with the conditions.

kaarre
  • 51
  • 4
  • Could you provide minimal executable source code to test your current solution? Also, the image (or similar) which you need to modify. – kaarre Dec 18 '20 at 20:58
  • from PIL import Image import sys img = Image.open('moo.jpg') img = img.convert("RGB") white = (255,255,255) width, height = img.size for x in range(width): for y in range(height): if img.getpixel( (x,y) ) == (2,3): img.putpixel( (x,y), white) img.show() – Nadīna Tarnovska Dec 18 '20 at 21:12
  • I also tried this(from PIL import Image import sys img = Image.open('moo.jpg') img = img.convert("RGB") pixdata = img.load() # change with your color for j in range(img.size[1]): for i in range(img.size[0]): if pixdata[i, j] == (2, 3): pixdata[i, j] = (255,255, 255) img.show()) – Nadīna Tarnovska Dec 18 '20 at 21:15