0

I am reading a .gif image using the imageio library in Python (which reads the .gif format as a list of frames, each are numpy arrays). I want to replace all the RGB colors in the .gif image with a certain fixed color (to possibly use an if statement later to change some parts of the image).

The data are on the form of something like [[23 234 23 24], [34, 46, 57 255]]...

This is my code:

gifimage = imageio.mimread(inputgif)
for x in range(len(gifimage)):
  shape = gifimage[x].shape
  for l in range(0, shape[0]):
    for y in range(0, shape[1]):
      gifimage[x][l][y] = [50, 205, 50, 255] #255 is just the alpha channel which I can ignore.

Also tried:

for x in range(len(gifimage)):
  shape = gifimage[x].shape
  for l in range(0, shape[0]):
    for y in range(0, shape[1]):
      gifimage[x][l][y][0] = 50
      gifimage[x][l][y][1] = 205
      gifimage[x][l][y][2] = 50
      gifimage[x][l][y][2] = 255

But when I run it, it never ends. Why is that?

1 Answer 1

1

For one thing you can eliminate some for loops. If you just want to reassign all of the pixels to a single color you can do the following:

gifimage[:][:][:] = [50, 205, 50, 255]

Just to be sure I follow, this will change every pixel to lime green.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This is what I was looking for. Now I can play with it by if statements to replace only the ones I desire.
But I am also curious to know why my for loops didn't work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.