7
I’m trying to split an image in half using the code below, but I’m having a feedback error.
Code:
import cv2
import numpy as np
# Read the image
img = cv2.imread("IMD015.png")
width = img.shape
# Cut the image in half
width_cutoff = width / 2
s1 = img[:, width_cutoff]
s2 = img[:, width_cutoff:]
# Save each half
cv2.imsave("face1.png", s1)
cv2.imsave("face2.png", s2)
i1 = cv2.imread("face1.png")
i2 = cv2.imread("face2.png")
assert i1.mode == i2.mode, "Different kinds of images."
assert i1.size == i2.size, "Different sizes."
pairs = zip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
# for gray-scale jpegs
dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = i1.size[0] * i1.size[1] * 3
print ("Difference (percentage):"+ str((dif / 255.0 * 100) / ncomponents))
Error:
line 10, in <module> width_cutoff = width / 2 TypeError: unsupported operand type(s) for /: 'tuple' and 'int'
I know the error is in the division, but how do I correct?
worked out, that’s right. Very obgdo
– Carlos Diego
now I am going through this error: line 19, in <module> assert I1.mode == I2.mode, "Different Kinds of images." Attributeerror: numpy.ndarray 'Object has no attribute 'mode'
– Carlos Diego
This is another mistake and needs to be in another question. Anyway I think you need programming training. It seems that you’re trying to make complex codes without even knowing the basics, this doesn’t usually work.
– Maniero
It worked, only I realized that this split in the middle it gets in the whole picture, and I want to split in the middle according to my region of interest, which in my case is a skin spot. In this question that I asked, explain better what I am trying to develop, if you can help me in this, I thank you. https://stackoverflow.com/questions/53301086/how-to-identify-if-the-shapes-of-an-image-are-symmetric-or-asymmetric-using-open?noredirect=1#comment93492798_53301086
– Carlos Diego