Python 3.x 如何将具有相同属性的图像保存为不同的图像

Python 3.x 如何将具有相同属性的图像保存为不同的图像,python-3.x,opencv,matplotlib,image-processing,Python 3.x,Opencv,Matplotlib,Image Processing,我想使用特定图像的属性,例如: t0 = cv2.cvtColor(cv2.imread('original.jpg'), cv2.COLOR_BGR2RGB) 并保存具有相同属性的新图像 重新表述我的问题: 我想用与“原始”相同的dpi、尺寸、颜色属性(RGB/BGR/灰度)等保存“新图像” original = cv2.cvtColor(cv2.imread('original.jpg'), cv2.COLOR_BGR2RGB) new_image = cv2.cvtColor(cv2.i

我想使用特定图像的属性,例如:

t0 = cv2.cvtColor(cv2.imread('original.jpg'), cv2.COLOR_BGR2RGB)
并保存具有相同属性的新图像

重新表述我的问题:

我想用与“原始”相同的dpi、尺寸、颜色属性(RGB/BGR/灰度)等保存“新图像”

original = cv2.cvtColor(cv2.imread('original.jpg'), cv2.COLOR_BGR2RGB)
new_image = cv2.cvtColor(cv2.imread('new.jpg'), cv2.COLOR_BGR2RGB)

如何通过opencv或matplotlib执行此操作?

使用opencv中的imwrite方法

import cv2

img='original.jpg'
t0 = cv2.cvtColor(cv2.imread(img), cv2.COLOR_BGR2RGB)

# To save your processed image
cv2.imwrite('img_t0.jpg',t0)

#To save your original file with save properties 
cv2.imwrite('new_original.jpg',img)

另外,在使用OpenCV编写JPEG时,您无法设置dpi,因此您可以在使用
cv2.imwrite()
写入图像后,使用Python的
子进程
命令将dpi“输出”到
exiftool


另一种方法是,这是一种相当恶劣的黑客行为,可以覆盖OpenCV生成的JPEG文件中的dpi、x分辨率和y分辨率:

#!/usr/bin/env python3

import struct
import numpy as np
import cv2

def writeJPEGwithdpi(im, filename, dpi=(72,72)):
   """Save the image as JPEG with embedded dpi"""

   # Encode as JPEG into memory
   retval, buffer = cv2.imencode(".jpg", im)
   s = bytearray(buffer)

   # APP0 segment looks like this
   # 0xFF, 0xE0,                     // APP0 segment
   # 0x00, 0x10,                     // size of segment, including these 2 bytes; 0x10 = 16 bytes
   # 0x4A, 0x46, 0x49, 0x46, 0x00,   // identifier string: "JFIF"
   # 0x01, 0x01,                     // JFIF version 1.01
   # 0x00,                           // density units (0=no units, 1=dpi)
   # 0x00, 0x01,                     // horizontal density
   # 0x00, 0x01,                     // vertical density
   # 0x00,                           // X thumbnail size
   # 0x00                            // Y thumbnail size

   # Find JFIF marker
   JFIF = s.find(b'JFIF\0')

   # Overwrite units, and x-resolution, and y-resolution 
   s[JFIF+7:JFIF+8]   = b'\x01'                                # density units = 1, i.e. dpi
   s[JFIF+8 :JFIF+10] = (dpi[0]).to_bytes(2, byteorder='big')  # 2 bytes of x-resolution
   s[JFIF+10:JFIF+12] = (dpi[1]).to_bytes(2, byteorder='big')  # 2 bytes of y-resolution

   with open(filename, "wb") as out:
      out.write(s)

################################################################################
# main
################################################################################

# Load sample image
im = cv2.imread('/Users/mark/sample/images/lena.png')

# Save at specific dpi
writeJPEGwithdpi(im, "result.jpg", (77,309))

使用exiftool检查结果:

exiftool result.jpg

ExifTool Version Number         : 12.00
File Name                       : result.jpg
Directory                       : .
File Size                       : 105 kB
File Modification Date/Time     : 2021:02:08 14:48:52+00:00
File Access Date/Time           : 2021:02:08 14:48:54+00:00
File Inode Change Date/Time     : 2021:02:08 14:48:52+00:00
File Permissions                : rw-r--r--
File Type                       : JPEG
File Type Extension             : jpg
MIME Type                       : image/jpeg
JFIF Version                    : 1.01
Resolution Unit                 : inches      <--- LOOKS GOOD
X Resolution                    : 77          <--- LOOKS GOOD
Y Resolution                    : 309         <--- LOOKS GOOD
Image Width                     : 512
Image Height                    : 512
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8
Color Components                : 3
Y Cb Cr Sub Sampling            : YCbCr4:2:0 (2 2)
Image Size                      : 512x512
Megapixels                      : 0.262
exiftool result.jpg
ExifTool版本号:12.00
文件名:result.jpg
目录:。
文件大小:105 kB
文件修改日期/时间:2021:02:08 14:48:52+00:00
文件访问日期/时间:2021:02:08 14:48:54+00:00
文件索引节点更改日期/时间:2021:02:08 14:48:52+00:00
文件权限:rw-r--r--
文件类型:JPEG
文件扩展名:jpg
MIME类型:图像/jpeg
JFIF版本:1.01

分辨率单位:英寸你确切的意思是什么?dpi和尺寸这里有一些相关的概念。。。如果您使用的是JPEG,而不是上面我的链接中的PNG,我想您可以
cv2.imencode()
进入内存,然后搜索JPEG APP0标记,并用原始图像中的字节替换这些字节。是的,这是一个黑客。
exiftool result.jpg

ExifTool Version Number         : 12.00
File Name                       : result.jpg
Directory                       : .
File Size                       : 105 kB
File Modification Date/Time     : 2021:02:08 14:48:52+00:00
File Access Date/Time           : 2021:02:08 14:48:54+00:00
File Inode Change Date/Time     : 2021:02:08 14:48:52+00:00
File Permissions                : rw-r--r--
File Type                       : JPEG
File Type Extension             : jpg
MIME Type                       : image/jpeg
JFIF Version                    : 1.01
Resolution Unit                 : inches      <--- LOOKS GOOD
X Resolution                    : 77          <--- LOOKS GOOD
Y Resolution                    : 309         <--- LOOKS GOOD
Image Width                     : 512
Image Height                    : 512
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8
Color Components                : 3
Y Cb Cr Sub Sampling            : YCbCr4:2:0 (2 2)
Image Size                      : 512x512
Megapixels                      : 0.262