Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python更改x轴和y轴上每n个像素的颜色_Python_Opencv_Python Imaging Library - Fatal编程技术网

Python更改x轴和y轴上每n个像素的颜色

Python更改x轴和y轴上每n个像素的颜色,python,opencv,python-imaging-library,Python,Opencv,Python Imaging Library,正如标题所说,我必须拍摄一幅图像并编写代码,在x轴和y轴上的每个第n个像素上着色 我尝试过使用for循环,但它在整个轴线上显示颜色,而不是我需要的一个像素。我必须使用OpenCV或枕头来完成此任务 #pillow from PIL import Image import numpy as np import matplotlib.pyplot as plt picture = Image.open('e92m3.jpg') picture_resized = picture.resize(

正如标题所说,我必须拍摄一幅图像并编写代码,在x轴和y轴上的每个第n个像素上着色

我尝试过使用for循环,但它在整个轴线上显示颜色,而不是我需要的一个像素。我必须使用OpenCV或枕头来完成此任务

#pillow
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

picture = Image.open('e92m3.jpg')

picture_resized = picture.resize( (500,500) )

pixels = picture_resized.load()
#x,y
for i in range(0,500):
    pixels[i,10] = (0,255,0)
for i in range(0,500):
    pixels[10,i] = (255,0,0)

%matplotlib notebook
plt.imshow(picture_resized)
这是它的大致外观:


我想我不明白你的问题,但这是我对它的理解的答案

def interval_replaceimg,offset_x:int=0,interval_x:int,offset_y:int=0,interval_y:int,replace_pxl:tuple: 对于范围偏移量y中的y,img.shape[0]: 对于范围偏移量x中的x,图像形状[1]: 如果x%间隔_x==0和y%间隔_y==0: img[y][x]=替换_pxl
我想我不明白你的问题,但这是我对它的理解的答案

def interval_replaceimg,offset_x:int=0,interval_x:int,offset_y:int=0,interval_y:int,replace_pxl:tuple: 对于范围偏移量y中的y,img.shape[0]: 对于范围偏移量x中的x,图像形状[1]: 如果x%间隔_x==0和y%间隔_y==0: img[y][x]=替换_pxl 在Python中,使用图像处理确实应该避免for循环。它们的速度非常慢,效率也非常低。由于几乎所有图像处理套件都使用Numpy阵列存储图像,因此您应该尝试使用矢量化Numpy访问方法,如切片、索引和广播:

import numpy as np
import cv2

# Load image
im = cv2.imread('lena.png')

# Use Numpy indexing to make alternate rows and columns black
im[0::2,0::2] = [0,0,0]
im[1::2,1::2] = [0,0,0]

cv2.imwrite('result.png', im)
如果要使用PIL/Pillow代替OpenCV,请按如下方式加载并保存图像:

from PIL import Image

# Load as PIL Image and make into Numpy array
im = np.array(Image.open('lena.png').convert('RGB'))

... process ...

# Make Numpy array back into PIL Image and save
Image.fromarray(im).save('result.png')
也许可以阅读一下索引。

您确实应该避免使用Python中的图像处理进行for循环。它们的速度非常慢,效率也非常低。由于几乎所有图像处理套件都使用Numpy阵列存储图像,因此您应该尝试使用矢量化Numpy访问方法,如切片、索引和广播:

import numpy as np
import cv2

# Load image
im = cv2.imread('lena.png')

# Use Numpy indexing to make alternate rows and columns black
im[0::2,0::2] = [0,0,0]
im[1::2,1::2] = [0,0,0]

cv2.imwrite('result.png', im)
如果要使用PIL/Pillow代替OpenCV,请按如下方式加载并保存图像:

from PIL import Image

# Load as PIL Image and make into Numpy array
im = np.array(Image.open('lena.png').convert('RGB'))

... process ...

# Make Numpy array back into PIL Image and save
Image.fromarray(im).save('result.png')

也许你可以阅读一下索引。

你能发布你尝试过的代码和错误的结果吗?因此,您应该提供一个。看到你能发布你尝试过的代码和错误的结果吗?因此,您应该提供一个。看见