Last active
January 14, 2021 10:58
-
-
Save ixs/4448b6a984080006581e587e49884c08 to your computer and use it in GitHub Desktop.
Apple iOS compatible image crop for contact/vcard images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from PIL import Image | |
import io | |
import logging | |
import vobject | |
import base64 | |
import hashlib | |
""" | |
Copyright 2021, Andreas Thienemann | |
This program is free software: you can redistribute it and/or modify it under | |
the terms of the GNU General Public License as published by the Free Software | |
Foundation, either version 3 of the License, or (at your option) any later | |
version. | |
This program is distributed in the hope that it will be useful, but WITHOUT | |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License along with | |
this program. If not, see <http://www.gnu.org/licenses/>. | |
""" | |
def apple_crop_photo(self, photo, vcard): | |
"""Crop an Apple contact picture accordingly. | |
Apple on iOS and MacOS seems to store vcards with an URL to the original | |
picture on the iCloud servers. | |
Any cropping needed happens on the end device at display time. | |
An example for this can look like the following taken from a vcard object: | |
PHOTO;X-ABCROP-RECTANGLE=ABClipRect_1&385&218&635&635&Zqy85whZ4ZukGCMpiKdrM | |
g;VALUE=uri:https://gateway.icloud.com/contacts/[redacted]/ck/card/[photo_... | |
..._id] | |
X-ABCROP-RECTANGLE has the following parameters, separated by ampersands (&): | |
* ABClipRect_1 = crop using a rectangle. Not seen any other cropping methods. | |
* 385 = x: Distance from the left edge of the source image | |
* 218 = y: Distance from the bottom edge of the source image | |
* 635 = x': Width of the cropped image | |
* 635 = y': Height of the cropped image | |
* Zqy85whZ4ZukGCMpiKdrMg = base64 encoded md5hash (.digest(), not .hexdigest()) | |
of the image on the server. | |
The y-axis is a bit weird as the y axis is normally taken from the top edge of the | |
picture but Apple seems to measure the distance from the bottom edge of the input | |
image to the bottom of the crop box...""" | |
params = vcard.photo.params["X-ABCROP-RECTANGLE"][0].split("&") | |
# No cropping needed | |
if params[0] != "ABClipRect_1": | |
logging.debug("Photo has no cropping parameters, returning as is.") | |
return photo | |
img = Image.open(io.BytesIO(photo)) | |
imgwidth, imgheight = img.size | |
x, y, width, height = [int(x) for x in params[1:5]] | |
# Recalculate y: Apple calculates y from the bottom edge while pillow calculates y | |
# from the top edge | |
y_orig = y | |
y = imgheight - y - height | |
box = (x, y, x + width, y + height) | |
img = Image.open(io.BytesIO(photo)) | |
img = img.crop(box) | |
with io.BytesIO() as output: | |
img.save(output, format="PNG") | |
contents = output.getvalue() | |
logging.debug( | |
"Photo cropped to %s.", {"apple": (x, y_orig, width, height), "pillow": box} | |
) | |
return contents |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment