Python 复制图像中的黑色像素,并用偏移量粘贴到同一图像中
我一直在尝试在我的照片中使用一些遮罩,但我想必须有一种更简单的方法来做到这一点: a) 我有一张BW图片(照片),显示了显示器上的数字,“test.png”(1000x300像素) b) 我想复制(仅)黑色像素并将它们粘贴到同一图像中 c) 粘贴时,我希望粘贴位置与“原始”位置偏移20px(x/y) 我尝试运行以下代码,但出现错误:Python 复制图像中的黑色像素,并用偏移量粘贴到同一图像中,python,image-processing,Python,Image Processing,我一直在尝试在我的照片中使用一些遮罩,但我想必须有一种更简单的方法来做到这一点: a) 我有一张BW图片(照片),显示了显示器上的数字,“test.png”(1000x300像素) b) 我想复制(仅)黑色像素并将它们粘贴到同一图像中 c) 粘贴时,我希望粘贴位置与“原始”位置偏移20px(x/y) 我尝试运行以下代码,但出现错误: import cv2 test = Image.open('test.png') np = Image.new('1', (1000, 300), 255) mas
import cv2
test = Image.open('test.png')
np = Image.new('1', (1000, 300), 255)
mask = np.bitwise_and(test, np.roll(test, 20, (0,1)))
mask.save('mask.png')
我得到AttributeError:“Image”对象没有属性“按位”和“如果Image是逻辑的(0/1),则:
如果映像是逻辑的(0/1),则:
您可以这样做:
from PIL import Image
import numpy as np
# Load image and make Numpy array
im = Image.open('image.png').convert('L')
na = np.array(im)
# Get x,y coordinates of black pixels
Y, X = np.where(na==0)
# Make pixels 30 across and 30 down from them black
na[Y+30, X+30] = 0
# Convert back to PIL Image and save
Image.fromarray(na).save('result.png')
您可以这样做:
from PIL import Image
import numpy as np
# Load image and make Numpy array
im = Image.open('image.png').convert('L')
na = np.array(im)
# Get x,y coordinates of black pixels
Y, X = np.where(na==0)
# Make pixels 30 across and 30 down from them black
na[Y+30, X+30] = 0
# Convert back to PIL Image and save
Image.fromarray(na).save('result.png')
一张图片抵得上1000个单词……添加图像。这张图片在使用前会经过处理和过滤以去除噪音等。一张图片相当于一张1000字的图片。此图像在使用前将进行处理和过滤,以去除噪音等。根据您的建议添加代码。如果我让它工作起来,它确实会删除我以前的很多代码:)添加:将numpy作为NP导入,并将图像转换为二进制。根据您的建议添加了代码。如果我让它工作的话,它确实会删除我以前的很多代码:)add:importnumpyasnp并将图像转换为二进制。