Reputation: 1131
I have created a .vcf contact with an iPhone and sent the file to myself in email. In that .vcf, I took a photo which is directly saved in the vCard, not in the phone's memory.
In the source of the .vcf, there is a code part starting like this:
PHOTO;ENCODING=b;TYPE=JPEG:/9j/4AAQSkZJRgABAQAAAQABAAD/4QBYRXhpZgAATU0AKgAA
And it continues on... Now, I would like to get this photo and save it as a .JPEG. Any ideas how to do that?
Thanks.
Upvotes: 17
Views: 20707
Reputation: 169
Here's a dirty Python script to do it:
#!/usr/bin/env python3
import argparse
import base64
parser = argparse.ArgumentParser('vcard-to-jpeg')
parser.add_argument('-i', '--input', type=argparse.FileType('r'), default='-')
parser.add_argument('-o', '--output', type=argparse.FileType('wb'), default='-')
args = parser.parse_args()
vcard_data = args.input.read()
lines = vcard_data.splitlines()
photo_start = next(idx for idx, line in enumerate(lines) if line.startswith('PHOTO'))
# We split on any whitespace here, not just lines, as these lines are often padded with a space.
# str.split() will transparently take care of that.
photo_lines = '\n'.join(lines[photo_start:]).split()
photo_lines[0] = photo_lines[0].replace('PHOTO;ENCODING=b;TYPE=JPEG:', '')
photo_data = ''.join(photo_lines)
args.output.write(base64.decodebytes(photo_data.encode('utf-8')))
Upvotes: 0
Reputation: 3249
Because this isn't https://apple.stackexchange.com/ I'll suggest a quick bash script that I've used to extracted images from .vcf files on the command line:
#!/bin/bash
#vcf_photo_extractor ver 20180207094631 Copyright 2018 alexx, MIT Licence
if [ ! -f "$1" ]; then
echo "Usage: $(basename $0) [path/]any/contact.vcf"
exit 1
fi
DATA=$(cat "$1" |tr -d "\r\n"|sed -e 's/.*TYPE=//' -e 's/END:VCARD.*//')
NAME=$(grep -a '^N;' $1|sed -e 's/.*://')
#if [ $(wc -c <<< $DATA) -lt 5 ];then #bashism
if [ $(echo $DATA|wc -c) -lt 5 ];then
echo "No images found in $1"
exit 2
fi
EXT=${DATA%%:*}
if [ "$EXT" == 'BEGIN' ]; then echo "FAILED to extract $EXT"; exit 3; fi
IMG=${DATA#*:}
FILE=${1%.*}
Fn=${FILE##*/}
if [ -f "${FILE}.${EXT}" ]; then
echo "Overwrite ${FILE}.${EXT} ? "
read -r YN
if [ "$YN" != 'y' ]; then exit; fi
fi
echo $IMG | base64 -id > ${FILE}.${EXT} || \
echo "Failed to output $NAME to ${FILE}.${EXT}"
This script tries to extract the base64 data, decode it using base64 and create an image file. I found on linux that base64 -id
worked but base64 -d
threw errors.
If you are a fan of single-line code or code-golf then this might work:
cat 1.vcf | tr -d " \n\r" | sed 's/.*TYPE=[^:]*://;s/END:V.*//' | base64 -d > 1.jpg
If you want something cleaner then Matt Brock's vCard_photo_extractor.sh might be what you are looking for.
Upvotes: 2
Reputation: 35331
Another vCard parser is ez-vcard, which is written in Java (disclaimer: I am the author).
File file = new File("vcard.vcf");
VCard vcard = Ezvcard.parse(file).first();
for (PhotoType photo : vcard.getPhotos()){
byte data[] = photo.getData();
//save byte array to file
}
Upvotes: 1
Reputation: 2051
In macOS, it easy to to from the line command with "vi" and "base64.
For example,
Export the "Apple Inc." contact that comes with every user account.
PHOTO;ENCODING=b;TYPE=JPEG:
# base64 -D -i Apple\ Inc..vcf -o Apple_Logo.jpeg
Upvotes: 12
Reputation: 1117
The encoding is Base64. You can find a tool for decoding online.
I can recommend Freeformatter.com's decoder, which lets you save as a binary file. You will then need to rename that file to photo.jpg
.
Upvotes: 7
Reputation: 1671
Used http://www.sobolsoft.com/convertvcfjpg/ with vCards from OSX, with success.
Upvotes: 0
Reputation: 16121
You should use a vCard parser (like vpim) that provides the ability to pull photo data from the vCard.
Upvotes: 5