注释JPEG问题

时间:2018-08-13 16:16:24

标签: python numpy opencv

好的,所以我有这些注释功能

def rotate_bound(image, angle):
    # grab the dimensions of the image and then determine the
    # center
    (h, w) = image.shape[:2]
    (cX, cY) = (w // 2, h // 2)

    # grab the rotation matrix (applying the negative of the
    # angle to rotate clockwise), then grab the sine and cosine
    # (i.e., the rotation components of the matrix)
    M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
    cos = np.abs(M[0, 0])
    sin = np.abs(M[0, 1])

    # compute the new bounding dimensions of the image
    nW = int((h * sin) + (w * cos))
    nH = int((h * cos) + (w * sin))

    # adjust the rotation matrix to take into account translation
    M[0, 2] += (nW / 2) - cX
    M[1, 2] += (nH / 2) - cY

    # perform the actual rotation and return the image
    return cv2.warpAffine(image, M, (nW, nH), borderValue=(255, 255, 255))

def blur_image(image, radius):
    pil_img = Image.fromarray(image)
    pil_img = pil_img.filter(ImageFilter.GaussianBlur(radius=radius))
    opencv_image = np.array(pil_img)
    return opencv_image

def enhance_brightness(image, value):
    for x in range(image.shape[0]):
        for y in range(image.shape[1]):
            if image[x][y][0] < 245 and image[x][y][1] < 245 and image[x][y][2] < 245:
                if image[x][y][0] + value <= 255 and image[x][y][0] + value >= 0:
                    image[x][y][0] += value
                if image[x][y][1] + value <= 255 and image[x][y][0] + value >= 0:
                    image[x][y][1] += value
                if image[x][y][2] + value <= 255 and image[x][y][0] + value >= 0:
                    image[x][y][2] += value

    return image

当我尝试使用它们时,出现无法修复的错误。

函数rotate_boundenhance_brightness都给出相同的错误:

'JpegImageFile' object has no attribute 'shape'. 

blur函数返回另一个错误:

a bytes-like object is required, not 'JpegImageFile'

如果有人帮我,我会很高兴。

1 个答案:

答案 0 :(得分:0)

您正在使用Pillow导入图像。枕头没有属性“ shape”。而是尝试

img = cv2.imread("test.jpg")

这应该解决所有问题。