[英]Python importing Vcard Photo
I'm trying to import a photo into my card using vobject.我正在尝试使用 vobject 将照片导入我的卡中。 I have the image stored inside my static folder and the filename inside my database.
我将图像存储在我的 static 文件夹中,文件名存储在我的数据库中。 I would like to import it in my VCard.
我想将它导入到我的 VCard 中。
So far I have made this:到目前为止,我已经做到了:
if request.method == "POST":
rowr = User.query.filter_by(user_id = user_id).first()
rowes = Profile.query.filter_by(user_id = user_id).first()
vcf_file_path = 'static/Contacto.vcf'
with open(vcf_file_path , 'w') as file_vcard:
vcard = vobject.vCard()
o = vcard.add('fn')
o.value = rowes.pname
if rowr.img_url != 'default.png':
o = vcard.add('PHOTO')
o.value = rowr.img_url
if rowes.pemail != None:
o = vcard.add('email')
o.type_param = 'INTERNET'
o.value = rowes.pemail
if rowes.pcellphone != None:
o = vcard.add('TEL')
o.type_param = 'Número Celular'
o.value = str(rowes.pcellphone)
if rowes.webpage != None:
o = vcard.add('url')
o.type_param = "Página Web"
o.value = rowes.webpage
if rowes.textarea != None:
o = vcard.add('note')
o.type_param = "note"
o.value = rowes.textarea
file_vcard.write(vcard.serialize())
But it's clearly no displaying the image in my VCard.但它显然没有在我的 VCard 中显示图像。 I've tried writing the entire path but also didn't work.
我试过写整个路径,但也没有用。 Thanks in advance
提前致谢
Looks like you're writing the string 'default.jpg'
to the photo field, when actually you need to write the base64 encoded image.看起来您正在将字符串
'default.jpg'
写入照片字段,而实际上您需要写入 base64 编码图像。 You could do this with a function like:您可以使用 function 来执行此操作,例如:
import base64
def b64_image(filename):
with open(filename, 'rb') as f:
b64 = base64.b64encode(f.read())
return b64.decode('utf-8')
Then call it like:然后这样称呼它:
o = vcard.add('PHOTO;ENCODING=b;TYPE=image/jpeg')
o.value = b64_image('default.jpg')
Obviously pass that function a valid path to the image file, which in your case may be rowr.img_url
.显然将 function 传递给图像文件的有效路径,在您的情况下可能是
rowr.img_url
。
I based this format on the vCard Format Specification .我基于vCard Format Specification的这种格式。
When tested this creates a line in the vcf
file which looks like:测试时,这会在
vcf
文件中创建一行,如下所示:
PHOTO;ENCODING=B;TYPE=IMAGE/JPEG:iVBORw0KGgoAAA...........
I opened this in Contacts v12.0 on OSx and it renders my sample image:我在 OSx 上的 Contacts v12.0 中打开了它,它呈现了我的示例图像:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.