为什么在numpy数组中替换颜色进程永远不会结束

时间:2022-03-31 08:19:11

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).

我正在使用Python中的imageio库读取.gif图像(它将.gif格式读取为帧列表,每个都是numpy数组)。我想用一定的固定颜色替换.gif图像中的所有RGB颜色(以后可能使用if语句来更改图像的某些部分)。

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

数据的形式类似于[[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 个解决方案

#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:

首先,你可以消除一些for循环。如果您只想将所有像素重新分配给单一颜色,则可以执行以下操作:

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

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

为了确保我遵循,这将把每个像素改为柠檬绿。

#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:

首先,你可以消除一些for循环。如果您只想将所有像素重新分配给单一颜色,则可以执行以下操作:

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

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

为了确保我遵循,这将把每个像素改为柠檬绿。