我可以使用cv2或numpy将图像转换为负片吗?类似于下面的内容,但我仍然需要编辑
我的问题主要是代码的顶部,如果我可以使用它来反转灰度和黑色&;白色都是负数吗
import cv2
import numpy as np
img = cv2.imageread('imagename.jpg')
print(img.dtype)
image_neg = 255 - img
cv2.imshow('negative',image_neg)
cv2.waitKey(0)
####################################### #######################################
from images import Image
def invert(image):
def blackAndWhite(image):
blackPixel = (0, 0, 0)
whitePixel = (255, 255, 255)
for y in range(image.getHeight()):
for x in range(image.getWidth()):
(r, g, b) = image.getPixel(x, y)
average = (r + g + b) // 3
if average < 128:
image.setPixel(x, y, blackPixel)
else:
image.setPixel(x, y, whitePixel)
def grayscale(image):
for y in range(image.getHeight()):
for x in range(image.getWidth()):
(r, g, b) = image.getPixel(x, y)
r = int(r * 0.299)
g = int(g * 0.587)
b = int(b * 0.114)
lum = r + g + b
image.setPixel(x, y, (lum, lum, lum))
def main():
filename = input("Enter the image file name: ")
image = Image(filename)
#Invert image
invert(image)
image.draw()
#Covert to greyscale, then invert
"""grayscale(image)
invert(image)
image.draw()"""
#Convert to black and white, then invert
"""blackAndWhite(image)
invert(image)
image.draw()"""
if __name__ == "__main__":
main()
我收到以下错误:
Traceback (most recent call last):
File "invert.py", line 14, in <module>
image_neg = 255 - image
NameError: name 'image' is not defined
我在开头修改了代码,说:
import cv2
import numpy as np
image = cv2.imageread('smokey.gif')
print(image.dtype)
image_neg = 255 - image
cv2.imshow('negative',image_neg)
cv2.waitKey(0)
我原以为这行可以,但它告诉我,“invertDimage=cv2.bitwise_not(imageToInvert)”有一个语法错误:无效的不可打印字符U+00A0
我在这里正确地编辑了我的代码(4个空格),我不知道为什么它仍然不能正确显示
from images import Image
import cv2
def invert(image):
imageToInvert = cv2.imread(filepath)
InverstDimage=cv2。按位\u非(imageToInvert)
cv2.imgwrite(“BWimage.png”,InvertDimage)
打印(“已保存反转图像”)
File_path='smokey.gif'
invert(File_path)
不确定您遇到了什么错误。也许这里有什么能帮上忙
语法:cv2.cv.flip(src,flipCode[,dst])
参数: src:输入数组。 dst:与src大小和类型相同的输出数组。 翻转代码:指定如何翻转数组的标志;0表示绕x轴翻转,正值(例如,1)表示绕y轴翻转。负值(例如,-1)表示围绕两个轴翻转
返回值:它返回一个图像。 如OpenCV中所示
相关问题 更多 >
编程相关推荐