Opencv:为什么在不做任何更改的情况下读写图像时文件大小会发生变化? [英] Opencv: Why does the file size change when I read and write an image without making any changes?

查看:203
本文介绍了Opencv:为什么在不做任何更改的情况下读写图像时文件大小会发生变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(.TIFF)文件列表,正在重命名并以相同格式保存. 我正在使用cv2模块来做到这一点.

I have a list of (.TIFF) files which I am renaming and saving in the same format. I am using cv2 module to do this.

import cv2
import os
import glob

os.chdir('C:/99_Temp/')

for file in glob.glob("*.tiff"):
   f = os.path.splitext(file)
   time_val = f[0][:2]
   a1 = cv2.imread(file)
   cv2.imwrite(time_val+'.tiff',a1)

为什么文件大小从原始TIFF文件减小了?我没有做任何处理,并且图像看起来还是一样的.但是我想知道,为什么会有所不同?

Why are the file sizes reduced from the original TIFF file? I haven't done any processing and visually the images look the same. But I am wondering, why the difference?

推荐答案

对于TIFF文件的大小为何会发生变化,可能有很多解释.这里有一些:

There could be many explanations of why the size of a TIFF file changes. Here are a few:

  • 一个文件可能是RGB,每个像素具有3个字节的红色,绿色和蓝色,而另一个编码器可能会看到该文件的颜色少于256种,并决定每个像素写入一个调色板索引的单个字节(并且将256种颜色存储在单独的调色板中),而不是3个字节的RGB.

  • one file may be RGB with 3 bytes of red, green and blue per pixel, while another encoder may see that the file has fewer than 256 colours and decide to write a single byte of palette index per pixel (and store the 256 colours in a separate palette) rather than 3 bytes of RGB.

一个文件可以是8位,另一个文件可以是1位(双级),16位,32位或64位.

one file may be 8-bit, the other may be 1-bit (bi-level), 16 bit, 32-bit or 64-bit.

文件的压缩率可能有所不同-到LZW,RLE或最近的JPEG压缩率都不一样.

the files may have different compression - varying through none, to LZW, RLE or more recently JPEG.

一个编码器可能已经编写了IPTC或其他元数据,而另一个则丢弃了它.

one coder may have written IPTC or other metadata, whilst the other discarded it.

一个编码器可能包含低分辨率预览,而另一个则没有.

one coder may have included a low resolution preview, the other not.

为了进行检查,您可以使用exiftool,它只是 一个Perl脚本,安装起来既简单又小:

In order to check, you could use exiftool which is just a Perl script and simple and small to install:

exiftool image.tif

示例输出

ExifTool Version Number         : 11.11
File Name                       : image.tif
Directory                       : .
File Size                       : 91 kB
File Modification Date/Time     : 2018:11:28 09:38:03+00:00
File Access Date/Time           : 2018:12:05 13:15:15+00:00
File Inode Change Date/Time     : 2018:12:05 13:15:10+00:00
File Permissions                : rw-r--r--
File Type                       : TIFF
File Type Extension             : tif
MIME Type                       : image/tiff
Exif Byte Order                 : Little-endian (Intel, II)
Image Width                     : 784
Image Height                    : 1466
Bits Per Sample                 : 8
Compression                     : LZW
Photometric Interpretation      : BlackIsZero
Strip Offsets                   : (Binary data 827 bytes, use -b option to extract)
Samples Per Pixel               : 1
Rows Per Strip                  : 10
Strip Byte Counts               : (Binary data 642 bytes, use -b option to extract)
Planar Configuration            : Chunky
Predictor                       : Horizontal differencing
Image Size                      : 784x1466
Megapixels                      : 1.1


libtiff附带的tiffinfo,它也很小,易于安装:


Or tiffinfo which comes with libtiff and is also pretty small and easy to install:

tiffinfo image.tif

示例输出

TIFF Directory at offset 0x16894 (92308)
  Image Width: 784 Image Length: 1466
  Bits/Sample: 8
  Compression Scheme: LZW
  Photometric Interpretation: min-is-black
  Samples/Pixel: 1
  Rows/Strip: 10
  Planar Configuration: single image plane
  Predictor: horizontal differencing 2 (0x2)


ImageMagick (已安装在大多数Linux发行版中,可用于macOS和Windows)-但安装量很大:


Or ImageMagick which is installed on most Linux distros and is available for macOS and Windows - but is quite a large install:

magick identify -verbose image.tif

示例输出

Image: image.tif
  Format: TIFF (Tagged Image File Format)
  Mime type: image/tiff
  Class: DirectClass
  Geometry: 784x1466+0+0
  Units: PixelsPerInch
  Colorspace: Gray
  Type: Grayscale
  Endianess: LSB
  Depth: 8-bit
  Channel depth:
    Gray: 8-bit
  Channel statistics:
    Pixels: 1149344
    Gray:
  ...
  ...
  Matte color: grey74
  Background color: white
  Border color: srgb(223,223,223)
  Transparent color: none
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 784x1466+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: LZW
  Orientation: TopLeft
  Properties:
    date:create: 2018-12-05T13:15:10+00:00
    date:modify: 2018-11-28T09:38:03+00:00
    signature: 5f9afdc8efd4757daa7f6bdba105f6ae149833c1c8103dd544f0073bb302069d
    tiff:alpha: unspecified
    tiff:endian: lsb
    tiff:photometric: min-is-black
    tiff:rows-per-strip: 10
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 93622B
  Number pixels: 1.14934M
  Pixels per second: 114.935MP
  User time: 0.010u
  Elapsed time: 0:01.009
  Version: ImageMagick 7.0.8-14 Q16 x86_64 2018-11-16 https://imagemagick.org

这篇关于Opencv:为什么在不做任何更改的情况下读写图像时文件大小会发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆