Export VCF image to JPEG

16,336

Solution 1

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.

Solution 2

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.

  1. Use vi to manually remove the other lines.
  2. Remove the heading and the meta-data for that line

PHOTO;ENCODING=b;TYPE=JPEG:

  1. base64 decode the remaining file

# base64 -D -i Apple\ Inc..vcf -o Apple_Logo.jpeg

Solution 3

You should use a vCard parser (like vpim) that provides the ability to pull photo data from the vCard.

Solution 4

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
}

Solution 5

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"|sed -e 's/.*TYPE=[^:]*://' -e 's/END:V.*//'|base64 -id >1.jpg

If you want something cleaner then Matt Brock's vCard_photo_extractor.sh might be what you are looking for.

Share:
16,336

Related videos on Youtube

pentzzsolt
Author by

pentzzsolt

Enthusiastic, dedicated, and passionate user interface engineer who wants to create great products that help people. On an intergalactic mission with Bitrise.

Updated on September 14, 2022

Comments

  • pentzzsolt
    pentzzsolt over 1 year

    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.

  • adamlogan
    adamlogan almost 6 years
    Matt Brock's vCard_Photo_Extractor.sh created 0 byte image for me just FYI. I think an option was missing that is now needed for the base64 decoding, I'm not sure. Also, I'm not sure what the option -h option is for in Matt Brock's script.
  • adamlogan
    adamlogan almost 6 years
    Using your script, I also get a zero byte file, and interestingly, the output filename has an extension of .home Here's the output: Unable to open 'd': No such file or directory Failed to output to name.Home
  • MacFreek
    MacFreek over 4 years
    The one-liner fails on macOS. On Debian or Ubuntu, the correct syntax is indeed base64 -id. However, on macOS, I had to use base64 -D instead. (there the -i referred to the input file, giving the error "Unable to open 'd': No such file or directory")