How To Save An Image With The Same Properties As A Different Image
I want to use the properties of a specific image, for example: t0 = cv2.cvtColor(cv2.imread('original.jpg'), cv2.COLOR_BGR2RGB) and save a new image with the same properties. Reph
Solution 1:
AFAIK, you cannot set the dpi when writing a JPEG with OpenCV, so you could maybe "shell out" to exiftool
with Python's subprocess
command after writing the image with cv2.imwrite()
:
exiftool -jfif:Xresolution=301 -jfif:Yresolution=302 result.jpg
As an alternative, this is a rather nasty hack to overwrite the dpi and x-resolution and y-resolution in a JPEG file generated by OpenCV:
#!/usr/bin/env python3import struct
import numpy as np
import cv2
defwriteJPEGwithdpi(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-resolutionwithopen(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))
Check result with exiftool
:
exiftoolresult.jpgExifTool Version Number :12.00File Name :result.jpgDirectory :.File Size :105kBFile Modification Date/Time :2021:02:0814:48:52+00:00File Access Date/Time :2021:02:0814:48:54+00:00File Inode Change Date/Time :2021:02:0814:48:52+00:00File Permissions :rw-r--r--File Type :JPEGFile Type Extension :jpgMIME Type :image/jpegJFIF Version :1.01Resolution Unit :inches<---LOOKSGOODX Resolution :77<---LOOKSGOODY Resolution :309<---LOOKSGOODImage Width :512Image Height :512Encoding Process :BaselineDCT,HuffmancodingBits Per Sample :8Color Components :3Y Cb Cr Sub Sampling :YCbCr4:2:0(22)Image Size :512x512Megapixels :0.262
Solution 2:
Use imwrite method from opencv.
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)
Post a Comment for "How To Save An Image With The Same Properties As A Different Image"