图片存储问题总结

参考博客:
https://blog.csdn.net/BUPT_Kwong/article/details/100972964

今天发现图片保存的一个神奇的问题,就是说原始的jpg图片打开后,重新保存成jpg格式,会发现这个结果不是很对的

example

from PIL import Image
import numpy as np 

img_path = './Training/meningioma/M1.jpg'
# 读取图片
image = Image.open(img_path)
width, height = image.size
print("图片的宽度为={},高度为={}".format(width,height))
print("图片的mode为{}".format(image.mode))
print("图片的format为{}".format(image.format))
pixel = image.load()
img_arr = np.array(image)
print("图片数组大小为{}".format(img_arr.shape))

print("图片像素值最大为{}".format(np.max(img_arr)))
print("图片像素值最大为{}".format(np.min(img_arr)))
#显示图片
image.show()
# 

print(image.info)

image.save("./test_pic/M1_new.jpg")
# 这个图像的dpi就自然而然变啦,就很奇怪的的这个东西
image.save("./test_pic/M1_V2.jpg",dpi=(300,300))

image.save("./test_pic/M1_new.png")
image.save("./test_pic/M1_V2.png",dpi=(300,300))

在这里插入图片描述结果如下
在这里插入图片描述

test1

# 存成png格式就不会压缩呢,这个结果是有问题的哈
img_path = './test_pic/M1_V2.png'
# 读取图片
image_new = Image.open(img_path)
width, height = image_new.size
print("图片的宽度为={},高度为={}".format(width,height))
print("图片的mode为{}".format(image_new.mode))
print("图片的format为{}".format(image_new.format))
img_arr_new = np.array(image_new)
print("图片数组大小为{}".format(img_arr_new.shape))

print("图片像素值最大为{}".format(np.max(img_arr_new)))
print("图片像素值最大为{}".format(np.min(img_arr_new)))

print(image_new.info)

print(np.sum(img_arr - img_arr_new))

在这里插入图片描述

test2

# 存成png格式就不会压缩呢,这个结果是有问题的哈
img_path = './test_pic/M1_new.png'
# 读取图片
image_new = Image.open(img_path)
width, height = image_new.size
print("图片的宽度为={},高度为={}".format(width,height))
print("图片的mode为{}".format(image_new.mode))
print("图片的format为{}".format(image_new.format))
img_arr_new = np.array(image_new)
print("图片数组大小为{}".format(img_arr_new.shape))

print("图片像素值最大为{}".format(np.max(img_arr_new)))
print("图片像素值最大为{}".format(np.min(img_arr_new)))

print(image_new.info)

print(np.sum(img_arr - img_arr_new))

在这里插入图片描述

test3

img_path = './test_pic/M1_new.jpg'
# 读取图片
image_new = Image.open(img_path)
width, height = image_new.size
print("图片的宽度为={},高度为={}".format(width,height))
print("图片的mode为{}".format(image_new.mode))
print("图片的format为{}".format(image_new.format))
img_arr_new = np.array(image_new)
print("图片数组大小为{}".format(img_arr_new.shape))

print("图片像素值最大为{}".format(np.max(img_arr_new)))
print("图片像素值最大为{}".format(np.min(img_arr_new)))

print(image_new.info)

print(np.sum(img_arr - img_arr_new))

在这里插入图片描述

test4


img_path = './test_pic/M1_V2.jpg'
# 读取图片
image_new = Image.open(img_path)
width, height = image_new.size
print("图片的宽度为={},高度为={}".format(width,height))
print("图片的mode为{}".format(image_new.mode))
print("图片的format为{}".format(image_new.format))
img_arr_new = np.array(image_new)
print("图片数组大小为{}".format(img_arr_new.shape))

print("图片像素值最大为{}".format(np.max(img_arr_new)))
print("图片像素值最大为{}".format(np.min(img_arr_new)))

print(image_new.info)
print(np.sum(img_arr - img_arr_new))

结果如下
在这里插入图片描述
所以保存成图片需要保存成png格式的,而不能保存成jpg格式的,一旦保存成jpg格式的,就会存在压缩,导致这个信息造成损失的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值