【618】不同图片格式的压缩情况
在制作深度学习语义分割的标签数据的时候,会涉及到存储图片的步骤,默认就是存成了 JPG 格式,今天跟同事聊天,突然说 JPG 会压缩数据,突然我也回忆道自己在生成的白色 JPG 图片上看到了莫名的淡灰色,于是自己打算自己试验下 JPG、PNG、TIF 与 BMP 数据是否能还原为原来的 numpy.array。结论就是 JPG 压缩很明显,其他数据格式都能还原,而且 PNG 占用的存储空间是最小的。
代码:
>>> import numpy as np >>> from PIL import Image >>> arr = np.arange(100).reshape(10, 10) >>> arr = arr.astype("uint8") >>> Image.fromarray(arr).save("1.jpg") >>> Image.fromarray(arr).save("1.png") >>> Image.fromarray(arr).save("1.tif") >>> Image.fromarray(arr).save("1.bmp") >>> img_jpg = Image.open("1.jpg") >>> np.array(img_jpg) array([[ 1, 1, 2, 3, 4, 5, 6, 7, 10, 10], [ 9, 10, 11, 12, 13, 14, 15, 15, 18, 18], [21, 21, 22, 23, 25, 26, 27, 27, 30, 30], [31, 31, 32, 33, 34, 36, 36, 37, 40, 40], [39, 40, 40, 42, 43, 44, 45, 45, 48, 48], [49, 49, 50, 51, 53, 54, 55, 55, 58, 58], [61, 61, 62, 63, 64, 65, 66, 67, 70, 70], [69, 70, 71, 72, 73, 74, 75, 75, 78, 78], [80, 81, 82, 83, 84, 85, 86, 86, 89, 89], [90, 90, 91, 92, 94, 95, 96, 96, 99, 99]], dtype=uint8) >>> img_png = Image.open("1.png") >>> np.array(img_png) array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]], dtype=uint8) >>> img_tif = Image.open("1.tif") >>> np.array(img_tif) array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]], dtype=uint8) >>> img_bmp = Image.open("1.bmp") >>> np.array(img_tif) array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]], dtype=uint8)
图片大小:
结论就是优先存储为 PNG 格式。