python-3.x - 如何将具有相同属性的图像保存为不同的图像
问题描述
我想使用特定图像的属性,例如:
t0 = cv2.cvtColor(cv2.imread('original.jpg'), cv2.COLOR_BGR2RGB)
并保存具有相同属性的新图像。
改写我的问题:
我想用与“原始”相同的 dpi、尺寸、颜色属性(RGB/BGR/GRAYSCALE)等保存“new_image”
original = cv2.cvtColor(cv2.imread('original.jpg'), cv2.COLOR_BGR2RGB)
new_image = cv2.cvtColor(cv2.imread('new.jpg'), cv2.COLOR_BGR2RGB)
我怎样才能通过 opencv 或 matplotlib 做到这一点?
解决方案
AFAIK,在使用OpenCV编写 JPEG 时无法设置 dpi ,因此您可以在使用以下命令编写图像后使用 Python 的命令“退出”:exiftool
subprocess
cv2.imwrite()
exiftool -jfif:Xresolution=301 -jfif:Yresolution=302 result.jpg
作为替代方案,这是覆盖OpenCV生成的 JPEG 文件中的 dpi 和 x 分辨率和 y 分辨率的相当讨厌的 hack :
#!/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
推荐阅读
- sql - 编写查询以获取要在每条记录上显示的相似 id 的总和
- javascript - 为什么 [1,2,3].prototype === Array.prototype 返回 false?
- python - 京东的价格如何?我使用 python 硒
- python-3.x - Trace Loop Python,为什么输出是90?
- python - 在协程中尝试/除外/最终
- mysql - SQL 转储中的 UTF-8 无效
- python - 我想知道 PYTHON 上的这个赏金挑战是什么
- jupyter-notebook - Jupyter Lab, R kernel - 通缉:阻止 Jupyter 打开 R 帮助服务器
- yaml - CloudFormation yaml - 如何强制数字类型?
- python - python:运行需要较长时间的脚本时,子进程不返回任何内容