图片存储问题总结

参考博客:
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格式的,就会存在压缩,导致这个信息造成损失的

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 06:16:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 06:16:01       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 06:16:01       58 阅读
  4. Python语言-面向对象

    2024-07-13 06:16:01       69 阅读

热门阅读

  1. yarn使用

    2024-07-13 06:16:01       23 阅读
  2. C语言排序之快速排序

    2024-07-13 06:16:01       26 阅读