python 修改图片详细信息,部分写入信息显示乱码,求指导

img

from bs4 import BeautifulSoup
import requests
from PIL import Image
import piexif
import lxml
from lxml import etree
 
 
 
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36',
    'Accept-Language':'zh-CN'
}
 
 
page_respond = requests.get(url = 'https://www.gooood.cn/huahui-engineering-design-and-technological-innovation-center-china-by-huahui-group-we-studio.htm',headers = headers)
page_Soup = BeautifulSoup(page_respond .content, 'lxml')
 
 
 
 
page_spec_label = page_Soup.findAll('span', class_= 'label-text')
page_spec_data = page_Soup.findAll('div', class_= 'spec-data')
 
 
key_word = []
value_word = []
 
for i in page_spec_label:
    # print(i.string)
    key_word.append(i.string)
 
for j in page_spec_data:
    s = j.find_all('a')
    if len(s)>1:
        l=';'.join([x.string for x in s])
    elif len(s)==1:
        l=s[0].string
    else:
        l=''
    value_word.append(l)
 
 
address = 'https://www.gooood.cn/huahui-engineering-design-and-technological-innovation-center-china-by-huahui-group-we-studio.htm'
dict_word = dict(zip(key_word,value_word))
print(dict_word)
save_filename = 'F:/CodeWar/spider/exif/00.jpg'
im = Image.open(save_filename)
exif_dict = piexif.load(im.info["exif"])
 
exif_dict["0th"][piexif.ImageIFD.Artist] = address.encode()
exif_dict["0th"][piexif.ImageIFD.Software] = dict_word['材料'].encode()
exif_dict["0th"][piexif.ImageIFD.Copyright] = dict_word['设计公司'].encode()
exif_dict["0th"][piexif.ImageIFD.Make] = dict_word['标签'].encode()
exif_dict["0th"][piexif.ImageIFD.Model] = dict_word['分类'].encode()
exif_dict["0th"][piexif.ImageIFD.XPTitle] = dict_word['位置'].encode()
exif_dict["0th"][piexif.ImageIFD.XPSubject] = dict_word['类型'].encode()
exif_dict["0th"][piexif.ImageIFD.XPKeywords] = dict_word['设计公司'].encode()
exif_dict["0th"][piexif.ImageIFD.XPComment] = dict_word['位置'].encode()
 
exif_bytes = piexif.dump(exif_dict)
im.save(save_filename, exif=exif_bytes)

试过修改chcp,65001的UTF-8和936的gbk,都不行

用utf-16来设置就可以了

exif_dict["0th"][piexif.ImageIFD.XPTitle] = dict_word['位置'].encode('utf-16')
exif_dict["0th"][piexif.ImageIFD.XPSubject] =dict_word['类型'].encode('utf-16')
exif_dict["0th"][piexif.ImageIFD.XPKeywords] =dict_word['设计公司'].encode('utf-16')
exif_dict["0th"][piexif.ImageIFD.XPComment] = dict_word['位置'].encode('utf-16')

全部修改成 gbk

这个是乱码了,重新生成文件试一试,
采纳,谢谢

请先采纳谢谢,CSDN-砖家默认排在最开始
img