Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Opencv:当我读写一个图像而不做任何更改时,为什么文件大小会改变?_Python_Opencv_Glob_Tiff_Filesize - Fatal编程技术网

Python Opencv:当我读写一个图像而不做任何更改时,为什么文件大小会改变?

Python Opencv:当我读写一个图像而不做任何更改时,为什么文件大小会改变?,python,opencv,glob,tiff,filesize,Python,Opencv,Glob,Tiff,Filesize,我有一个(.TIFF)文件列表,我正在重命名这些文件并以相同的格式保存它们。 我使用cv2模块来实现这一点 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)文件列表,我正在重命名这些文件并以相同的格式保存它们。 我使用cv2模块来实现这一点

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文件小?我没有做任何处理,图像在视觉上看起来是一样的。但我想知道,为什么会有不同


关于TIFF文件大小变化的原因,可能有很多解释。以下是一些:

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

  • 一个文件可以是8位,另一个文件可以是1位(双电平)、16位、32位或64位

  • 这些文件可能有不同的压缩-从none到LZW、RLE或最近的JPEG都有所不同

  • 一个编码者可能编写了IPTC或其他元数据,而另一个编码者则丢弃了它

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


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

exiftool image.tif
tiffinfo 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
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)
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

或者
tiffinfo
,它与
libtiff
一起提供,并且非常小,易于安装:

exiftool image.tif
tiffinfo 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
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)
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

ImageMagick,它安装在大多数Linux发行版上,可用于macOS和Windows,但安装量相当大:

magick identify -verbose 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
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)
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

胡乱猜测:tiff编码的不同实现/版本/参数?原始文件是如何生成的?而且,如果您再次读写第二个图像,第二个和第三个图像的大小是否相同?