top of page
Search
  • Writer's pictureYun'Harla

Google Portrait Mode Photos Conversion to RGBD for Looking Glass Portrait

Updated: Oct 28, 2021


Looking Glass Portrait's HoloPlay Studio has Not been able to import Google Portrait Mode Photos taken using Samsung mobile phones, directly.


Thankfully, I've found some useful info that enabled me to successfully cobble together a procedure for converting Portrait photos to RGBD photos, using ExifTool & ImageMagick.


Phil Harvey's ExifTool: https://exiftool.org/


Manual Conversion of Portrait Photo to RGBD

Note: info here is specifically for photos taken using Samsung mobiles.

  1. Use exiftool.exe to extract the Depth Map TIFF data into a new .TIFF image file form the original photo (say photo.jpg) by running this command: exiftool -b -DepthMapTiff photo.jpg > photo.dep.tiff You'll get photo.dep.tiff file which is a grayscale depth map image for your photo. It may be smaller in size (resolution) than the original photo.

  2. Run your favourite image editor program.

  3. Open the original photo (photo,jpg).

  4. Double its canvas size - specify the same height and twice the width. This is to make space on the right side of the photo for the depth map. Let's call this the RGBD photo.

  5. Open the .TIFF depth map image.

  6. Resize it to the same size as the original photo, if it's smaller. Copy it.

  7. Paste depth map as a new layer into the RGBD photo, and move it to the right side. In Paint Shop Pro for example, simply 'paste as new layer', switch to 'pick tool', then change the 'Position X' value in the toolbar to '0' (zero), hit <Enter> and viola~ the depth map is pasted & moved to the right side of the photo.

  8. Save the RGBD photo as a new .JPG.

  9. You can now drag and drop the RGBD .JPG image into HoloPlay Studio. Ref: HoloPlay guide: How to turn your iPhone photos into holograms

  10. Select the newly added image, and tick the 'Depth Inversion' checkbox (if 3D photo looks deflated in your Looking Glass Portrait).


[Best] ExifTool + ImageMagick

Both ExifTool and IamgeMagick are command-line tools.

  1. Download ExifTool (it's a single executable file), and portable release ImageMagick (it's a folder of tools).

  2. Unzip portable ImageMagick somewhere, and copy ExifTool.exe to the same folder. Add this path to your system PATH.

  3. Create this batch file (for Windows OS) (say rgbd.bat):

Now, you're ready to run the conversion. Execute command: rgbd.bat c:\photo.jpg which will give you the final output of photo.rgbd.jpg - the RGBD photo.


Batch Operation on a Folder of Photos

To process multiple photos, create batch file (say rgbd_batch.bat) to list the photos are run rgbd.bat on each of them:

Next, open a command prompt to your folder of photos and run rgbd_batch.bat from it.

Output options:

  • 1: generate only unblurred RGBD, i.e. call rgbd_detail.bat

  • 2: generate both unblurred and blurred RGBDs, i.e. call rgbd_both.bat

  • others: generate only blurred RGBD, i.e. call rgbd.bat


What's the script doing?

Basically use ExifTool to extract depth map.

  • For advanced users: in case you're using some other phone/app that stores depth data differently, than in the Exif 'DepthMapTiff' tag, try to find the correct tag:

    • Run command: exiftool photo.jpg to get a listing of all Exif information.

    • Look for 'binary' data sections and try those tags.

    • To extract that data for a specific tag, omit spaces and run command like: exiftool -b -TagName photo.jpg > photo.depth.jpg where: -b means extract binary data -TagName specifies which tag to extract from. E.g. Samsung's is 'Depth Map Tiff', so use '-DepthMapTiff'. > output.jpg means pipe the output to given file (i.e. save to disk).

Next, use ImageMagick for image processing:

  • Resize and rotate original photo and depth map to the correct orientation.

  • Stitch the 2 together side-by-side into RGBD photo.

    • PS: colour photo is on the left, while its grayscale depth data is on the right.

Alternate: Preserve Details

The unblurred version of a Portrait mode photo is stored in Exif tagged as 'EmbeddedImage'. Thus, we can extract as use this for the RGBD photo instead. An updated rgbd_detail.bat is as follows:

The script extracts the unblurred photo as an extra photo.jpg.img.jpg and creates another unblurred RGBD photo photo.jpg.unblurred-rgbd.jpg.


OR perhaps, let's just generate both versions together in rgbd_both.bat:


[Obsolete] Java Conversion App

I've written a Java command-line app to automate the manual process, given a photo. It's a little rough on the edges.


There are some issues encountered and additional steps for the app to do. Below is a brief overview of the inner workings of the app.


  • Uses Java's ImageIO APIs to load the images.

    • Problem: it can't read the TIFF depth map!

      • Tried TwelveMonkeys ImageIO plugin for TIFF like JDeli devs suggested, but there's some colour issue ruining the output image.

      • Maybe use an alternate, superior library like JDeli, which has fuller support for many image file formats.

    • Problem: ImageIO.read() doesn't read mobile photo orientation from Exif.

      • Extra workaround:

        • Use exiftool to extract the standard Exif information to .TXT. Run command: exiftool photo.jpg > photo.exif.txt

        • Read .TXT in app to look for orientation info like 'Orientation : Rotate 90 CW' (actually lots of spaces added by exiftool for pretty formatting, it seems).

        • Later, rotate both original image and depth map if needed.

  • Procedure:

    • Load original photo and depth map into separate BufferedImage objects.

    • Rotate them if needed.

    • Create new BufferedImage to store output. Double the width of course.

    • Use Graphics2D to paint the original photo's BufferedImage to 0, 0 position.

    • Use Graphics2D to paint the depth map's BufferedImage to width, 0 position, and scaled to width, height of original image.

    • Write output BufferedImage to .JPG file on disk. The End =)

Other Information

Depth Camera: It seems that an actual depth camera is not needed in your mobile phone, as I've successful converted photos from cameras with and without dedicated depth camera, for display in Looking Glass Portrait =)


Perhaps multi-cameras is sufficient for Camera app to deduce depth data and capture Portrait mode photos.


TODO: Should try out 'Portrait Video' mode of the Camera app.


Exif Data:

  • There're two images embedded in Exif data - one looks near, the other far, so perhaps from mobile's 2 cameras. You can use exiftool to extract them from these tags: 'EmbeddedImage' & 'EmbeddedImage2'.

    • It appears that EmbeddedImage gives the original captured image without applying blurring to create depth of field effect.

      • Use rgbd_detail.bat to use the original 'unblurred' image for more details instead, and let Looking Glass's 3D naturally attune the background.

    • EmbeddedImage2 gives the zoomed out capture.

  • Depth data is duplicated in Exif tag 'DepthMapData' but without the TIFF* header. I presume it's a TIFF header of sorts, since image editors don't recognize the file without the header.

What's Next?

Java App to Change Background Effect

A possibility is to create a Java app to manipulate the RGBD photo like in Camera app to 'change the background effect', i.e. change the focus and blur out different areas of the photo.


Presumed algorithm:

  1. Let user click on colour photo to pick a spot to focus on - i.e. selection is the desired clear, focused depth.

  2. Algo picks the corresponding point in grayscale depth map to determine the 'depth', say d, at selected point.

  3. Algo leaves areas with depth of d +/- delta in focus, i.e. don't touch these areas in EmbeddedImage data.

    1. Does algo need to allow user to select width/range of delta?

  4. Algo blurs all other areas with other depths.

    1. Wonder if algo needs to apply different amounts of blurring, say more blurring to further scene objects? For a start, probably just keep it simple: clear/focused, or one level of blur for all else.

  5. Algo outputs this new blurred colour photo to the left side of RGBD photo.

Android App to Convert Portrait Mode Photo to RGBD

Wonder if this would still be useful? I suppose it would make it easier (one step) to convert photos, than using the the 3-steps ExifTool+ImageMagick semi-manual method.


Difficulty: migrating Perl codes of Exiftool to Android & making Android app read TIFF.


Credits

Thanks to the developers of the awesome tools and the geniuses who pointed out the way to extract depth map =)

Recent Posts

See All
Post: Blog2_Post
bottom of page