python获取图像的长和宽_关于python读jpg照片长宽不对应问题

最近在做相机照片的物体检测,处理数据进行数据增强(data augument)时候,采用的读图操作:

img = cv2.imread(path)

然后在检查数据增强后的数据和对应的box框是否对应时候,发现,居然出问题了!新resize扩充的图像的Box与原图并不对应!!!

经查找资料,发现opencv 2.x版本在读取相机jpg照片时候,没有正确解析相机的EXIF信息,所以读取的长宽和实际的长宽是反的!

这里可以换用PIL Image库来读取照片,能够得到正确的长宽

img = Image.open(path)

也可以通过PIL Image库分析照片的exif中方向信息,然后对图像做对应的旋转变换:

def _orientation(image):

try:

exif = image._getexif()

except AttributeError:

exif = None

if exif:

orientation = exif.get(0x0112)

if orientation == 2:

image = image.transpose(Image.FLIP_LEFT_RIGHT)

elif orientation == 3:

image = image.rotate(180)

elif orientation == 4:

image = image.transpose(Image.FLIP_TOP_BOTTOM)

elif orientation == 5:

image = image.rotate(-90).transpose(Image.FLIP_LEFT_RIGHT)

elif orientation == 6:

image = image.rotate(-90)

elif orientation == 7:

image = image.rotate(90).transpose(Image.FLIP_LEFT_RIGHT)

elif orientation == 8:

image = image.rotate(90)

return image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值