Automated vCard writer (with photo)

I’ve done some more research and testing, and I found a two ways to increase photo quality even more:

1/ Decrease the image saturation: the end result looks less colorful, but the size of the compressed image decreases significantly

2/ Change file format from JPEG to WEBP. I’ve done tests on a few Android cellphone, and it turns out they all support WEBP natively down to at least Android v5. And boy! is WEBP better than JPEG or what! For the same final size, a WEBP image has a higher resolution, and much fewer compression artifacts than a JPEG.

Here for example, a photo of our Glorious Leader, before (JPEG, 686 x 686, 315 KB) and after (WEBP, 60 x 60, 555 bytes - fits on an NTAG 216):

It looks fantastic on a cellphone. Fucking brilliant image format: it really blows JPEG out of the water.

So, here’s an updated version of my script, that does both JPEG and WEBP - but does WEBP by default. All you gotta do is stick your photo as a photo.jpg file, edit your information, and play around with the compression parameters until the NDEF fits on your tag. In addition to the packages listed in the first post of this thread, you’ll need the webp package (apt-get install webp should do the trick):

#!/bin/bash

### vCard information
FIRSTNAME=Joe
LASTNAME=Blow
TELEPHONE=(311)555-1212
EMAIL=joeblow@aol.com
PHOTO=./photo.jpg
NOTE=

### What type of photo encoding/compression to use (JPEG or WEBP)
PHOTO_ENCODING=WEBP

### Parameters for the final JPEG image to put in the vCard, if we do JPEG
JPEG_SIZE=52x52
JPEG_SATURATION=43
JPEG_QUALITY=73

### Parameters for the final WEBP image to put in the vCard, if we do WEBP
WEBP_SIZE=60x60
WEBP_SATURATION=56
WEBP_QUALITY=69

### Path to the various utilities we need
GUETZLI=./guetzli/bin/Release/guetzli
CWEBP=cwebp
NDEFTOOL=ndeftool
TAGTOOL="python3 nfcpy/examples/tagtool.py"
 


# Resize / desaturate the original photo
echo "Resizing the photo for JPEG"
convert $PHOTO -resize $JPEG_SIZE -modulate 100,$JPEG_SATURATION,100 \
	./photo_resized_for_jpeg.png

echo "Resizing the photo for WEBP"
convert $PHOTO -resize $WEBP_SIZE -modulate 100,$WEBP_SATURATION,100 \
	./photo_resized_for_webp.png

echo "Compressing the photo for JPEG"
$GUETZLI --quality $JPEG_QUALITY ./photo_resized_for_jpeg.png \
	./photo_compressed.jpg

echo "Compressing the photo for WEBP"
$CWEBP -q $WEBP_QUALITY ./photo_resized_for_webp.png -o \
	./photo_compressed.webp

# Create the .vcf file
echo "Creating the vCard file"

echo "BEGIN:VCARD" > vcard.vcf
echo "VERSION:2.1" >> vcard.vcf
echo "N:$LASTNAME;$FIRSTNAME" >> vcard.vcf
echo "TEL:$TELEPHONE" >> vcard.vcf
echo "EMAIL:$EMAIL" >> vcard.vcf

if [ $PHOTO_ENCODING = JPEG ];then
  B64ENCPHOTO=$(base64 -w0 ./photo_compressed.jpg)
else
  B64ENCPHOTO=$(base64 -w0 ./photo_compressed.webp)
fi
B64ENCPHOTO="PHOTO;ENCODING=BASE64;TYPE=$PHOTO_ENCODING:$B64ENCPHOTO"
echo $B64ENCPHOTO | head -c 73 >> vcard.vcf
for L in $(echo $B64ENCPHOTO | tail -c +74 | sed -e 's/.\{72\}/&\n/g'); do
  echo -en "\n $L" >> vcard.vcf
done
echo >> vcard.vcf

if [ "$NOTE" ];then
  echo "NOTE:$NOTE" >> vcard.vcf
fi

echo "END:VCARD" >> vcard.vcf

# Dump the size of the .vcf file, for information
VCARDSIZE=$(wc -c < vcard.vcf)
echo "vCard size: $VCARDSIZE bytes"

# Create the NDEF record
echo "Encapsulating the vCard into an NDEF record"
$NDEFTOOL load --pack vcard.vcf tn text/vcard id '' save --force vcard.ndef

# Dump the size of the NDEF file, for information
NDEFSIZE=$(wc -c < vcard.ndef)
echo "NDEF size: $NDEFSIZE bytes"

# Read the tag
echo "Reading the tag"
CARD_INFO=$($TAGTOOL)

# Make sure the tag is writable
if [ ! "$(echo $CARD_INFO | grep -i 'NDEF Capabilities.*writeable *= *yes')" ]; then
  echo "Tag is not writeable!"
  exit -1
fi

# Make sure there's enough room on the tag for the NDEF record
TCAPA=$(echo $CARD_INFO | sed -r 's/^.*NDEF Capabilities.*capacity *= *([0-9]+) byte.*$/\1/')
if [ $TCAPA -lt $NDEFSIZE ]; then
  echo "NDEF size exceeds the tag's capacity ($TCAPA bytes)!"
  exit -1
fi

# Ask the user if they really want to write the vCard on the tag
read -p "Write NDEF onto the tag [Y/N]? " YN
if [ "$YN" != "y" ] && [ "$YN" != "Y" ]; then
  echo "Aborting"
  exit 0
fi

# Write the NDEF file onto the tag
echo "Writing NDEF onto the tag"
$TAGTOOL load vcard.ndef

echo "All done!"

Sorry, Linux-only. I don’t have Windows.

5 Likes