本文主要是介绍在PYTHON中用zlib模块对文本进行压缩,写入图片的EXIF中,后在C#中读取EXIF并用SharpZipLib进行解压获取压缩前文本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在PYTHON中用zlib模块对文本进行压缩长度,写入图片的EXIF中,并在C#中读取EXIF后用SharpZipLib进行解压缩获取压缩前文本。
PS:当压缩后的字节数组长度为单数时,无法写入EXIF的XPComment中,需要在后面增加一个以utf-8编码的空格, 使其在原基础上在1变为双数 。另外,解压时要根据压缩时的编码选择对应的解压编码。
PYTHON代码
def save_file(args):#增加在主图片主题属性中增加CV2抠图返回的点 START_, cnt_original, _ = cv2.findContours(synthetic, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)contours_list = cnt_original[0].tolist()process_contours_list = []for list in contours_list:process_contours_list.append(list[0])# 数据格式 [[12,33],[1232,212],[1233,244].....] # list转字符串process_contours_list_bytes = json.dumps(process_contours_list) compress_contors = zlibCompress(process_contours_list_bytes) exif_dict = piexif.load(image_name)if 'exif' not in image.info.keys():software = ""copyright = ""XPComment = ""else:exifData = piexif.load(image.info['exif'])exifData = exifData['0th']software = exifData[piexif.ImageIFD.Software]copyright = exifData[piexif.ImageIFD.Copyright]XPComment = exifData[piexif.ImageIFD.XPComment]zeroth_ifd = {piexif.ImageIFD.XPSubject: compress_contors,piexif.ImageIFD.XPComment: XPComment,piexif.ImageIFD.Software:software,piexif.ImageIFD.Copyright:copyright}# exif_dict['0th'] = zeroth_ifdexif_dict = {"0th": zeroth_ifd}# print(exif_dict)exif_bytes = piexif.dump(exif_dict)img.save(filename + "_s.jpg", format="JPEG", quality=quality, dpi=dpi,exif=exif_bytes)#用Zlib进行压缩
# 参数为字符串,返回为bytes[]
# 当压缩后的字节数组长度为单数时,无法写入EXIF的XPComment中,需要在后面增加一个以utf-8编码的空格,
# 使其在原基础上在1变为双数
def zlibCompress(data):compress_contors = zlib.compress(data.encode('utf-16'))if (len(compress_contors) % 2 != 0):add_byte = bytes(' ',encoding='utf-8')compress_contors += add_byte# tem_data = data + '.'# tem_data = tem_data.encode('utf-16')# compress_contors = zlib.compress(tem_data)return compress_contors
C#读取并用SharpZipLib库 进行解压解压文本
using MetadataExtractor;
using System;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.Zip.Compression;//读取图片EXIF并解压内容
private void button6_Click(object sender, EventArgs e)
{try{string filename = "G://test/test/_200_s.jpg";if (File.Exists(filename)){StringBuilder sb = new StringBuilder();IReadOnlyList<MetadataExtractor.Directory> directories = ImageMetadataReader.ReadMetadata(filename);foreach (MetadataExtractor.Directory directory in directories){if (directory.HasTagName(40095)){byte[] resDes = directory.GetByteArray(40095);byte[] res0 = SharpZipLibDecompress(resDes);string result = System.Text.Encoding.Unicode.GetString(res0);//根据压缩时的编码选择对应的解压编码//string result = System.Text.Encoding.UTF8.GetString(res0);Console.WriteLine(result);}}}}catch (Exception ex){System.Windows.Forms.MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);}
}//用SharpZipLib进行解压
public static byte[] SharpZipLibDecompress(byte[] data){MemoryStream compressed = new MemoryStream(data);MemoryStream decompressed = new MemoryStream();InflaterInputStream inputStream = new InflaterInputStream(compressed);inputStream.CopyTo(decompressed);return decompressed.ToArray();}
这篇关于在PYTHON中用zlib模块对文本进行压缩,写入图片的EXIF中,后在C#中读取EXIF并用SharpZipLib进行解压获取压缩前文本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!