Page 1 of 1

Converting HEIF to JPEG with depth map

Posted: 2018-10-05T07:56:14-07:00
by talyPaleAle
HEIF images support a "depth map" layer, which is used also by iPhone cameras to store "portrait" photos. I am trying to convert HEIF images to JPEG using ImageMagick, and not sure how to apply the depth effect.

The depth map looks like another channel in Photoshop. With the RGB channels hidden:

Image

How can I apply the blurriness to the photo, according to the depth map, when using the "convert" command?

Re: Converting HEIF to JPEG with depth map

Posted: 2018-10-05T08:20:04-07:00
by snibgo
The first step is to extract the depth mask from the HEIF file. I don't know how to do that. Please post a link to the file.

When that is done, the depth mask can be used either as a mask to composite a blurred image over the original, or (better) to vary the sigma of the blur (using "-compose Blur").

But the first step needs to be done first.

Re: Converting HEIF to JPEG with depth map

Posted: 2018-10-05T08:49:51-07:00
by talyPaleAle
Thanks for responding fast! You can see a sample photo (it's not the same one as above, but same idea) here: https://www.dropbox.com/s/zn0uio8a36o62 ... .HEIC?dl=0

Re: Converting HEIF to JPEG with depth map

Posted: 2018-10-05T09:54:03-07:00
by snibgo
Sorry, IM can't see more than the three colour channels. Exiftool can't see any extra images.

With a quick internet search, I can't find any tools that will read the depth mask, other than Photoshop.

If you can extract a depth mask, save as a PNG and paste a link to it, we can show you the blur stage.

Re: Converting HEIF to JPEG with depth map

Posted: 2018-10-05T10:59:03-07:00
by fmw42
I tried to load your image into my Photoshop CC 2018 on my Mac OSX Sierra. But PS said my OS needed to be upgraded. So I suppose I would need High Sierra to utilize your image in PS.

This document https://support.apple.com/en-us/HT207022 confirms that High Sierra is needed.

Re: Converting HEIF to JPEG with depth map

Posted: 2018-10-05T11:41:23-07:00
by talyPaleAle
Interesting. Yes, I am on High Sierra. I wonder if PS is using some libraries Apple provides (would make sense, to be honest, also considering that HEIF is covered by patents). I'll dig deeper and check if I can export the channel into PNG.

Re: Converting HEIF to JPEG with depth map

Posted: 2018-10-05T18:30:43-07:00
by talyPaleAle
@snibo I was able to extract the depth map layer from the image into a PNG file (using libheif). Here it is: https://www.dropbox.com/sh/owbkv37go0vs ... kDSOa?dl=0 The folder contains the original HEIF file, the file converted to PNG (again, using libheif) and the depth map as PNG file.

Could you please show me how the "lens blur" would work here?

Re: Converting HEIF to JPEG with depth map

Posted: 2018-10-05T19:06:38-07:00
by fmw42
See https://imagemagick.org/Usage/mapping/#blur

Note that your depth image is much smaller than your Portrait2 image. So you must resize one or the other. Try this

Code: Select all

convert \( Portrait2.png -resize 576x768 \) \( Portrait2-depth.gif -negate \) -define compose:args=3 -compose blur -composite Portrait2_blur.png
You will see that the background is now slightly blurry and varies by the shade of gray in the depth image. The depth map needs to be inverted (negate), since it gets blurred more where it is white and not blurred where it is black. Your depth image has the opposite polarity so I negated it.

I used Imagemagick 6.9.10.12 Q16 on my Mac OSX Sierra.

Re: Converting HEIF to JPEG with depth map

Posted: 2018-10-06T07:49:00-07:00
by snibgo
When the input image is all in focus, we can make an animated travelling focus effect.

Image

My v7.0.7-28 can't read the MEIC file, so I use v6.9.9-28 for that. Windows BAT syntax:

Code: Select all

for /F "usebackq" %%L in (`%IM%convert ^
  portrait2-depth.png ^
  -format "WW=%%w\nHH=%%h\n" ^
  info:`) do set %%L

%IM%convert portrait2.HEIC -resize "%WW%x%HH%^!" p.miff


for /L %%I in (0,10,100) do (

  set GRAYPC=%%I

  set LZ=000!GRAYPC!
  set LZ=!LZ:~-3!

  %IMG7%magick ^
    portrait2-depth.png ^
    ^( +clone ^
       -fill gray^(!GRAYPC!%%^) -colorize 100 ^
    ^) ^
    -compose Difference -composite ^
    p.miff ^
    +swap ^
    -define compose:args=5 -compose Blur -composite ^
    portrait_b_!LZ!.miff
)
  
%IMG7%magick ^
  portrait_b_*.miff ^
  ^( -clone 0--1 ^
     -reverse ^
  ^) ^
  portrait2.gif
The code could be far more efficient.

The low resolution of the depth mask limits the usefulness, but it is quite cool.

Re: Converting HEIF to JPEG with depth map

Posted: 2018-10-06T18:40:28-07:00
by talyPaleAle
Thanks for the answer, this is amazing.

I didn't notice that the depth map was much smaller. Not sure why, and I will look into it. I tried enlarging the map with the command below, and while it works (and takes forever), the edges are a bit blurry. Anyways, this is beyond ImageMagick, and I really appreciate your help

Code: Select all

# IM 7 on *NIX
convert \
    \( Portrait2.png \) \
    \( Portrait2-depth.png -resize 3024x4032 -negate \) \
    -define compose:args=20 \
    -compose blur \
    -composite Portrait2_blur.png

Re: Converting HEIF to JPEG with depth map

Posted: 2019-02-24T16:20:56-07:00
by ippon
talyPaleAle wrote: 2018-10-05T18:30:43-07:00 @snibo I was able to extract the depth map layer from the image into a PNG file (using libheif). Here it is: https://www.dropbox.com/sh/owbkv37go0vs ... kDSOa?dl=0 The folder contains the original HEIF file, the file converted to PNG (again, using libheif) and the depth map as PNG file.

Could you please show me how the "lens blur" would work here?
How did you extract the depth map layer? can you send me the code?

Posted: 2019-06-01T16:52:16-07:00
by josephotoservices
Sorry New in here; I don't know how to erase someting I posted wrong.

convert 2 jpg files into single HEIC file

Posted: 2019-06-01T16:56:05-07:00
by josephotoservices
Even when I've been posting my own POST I wonder if someone here could help me out. Thanx in Adavanced

I got an iMac running Mojave, yesterday I successfully installed ImageMagick from Terminal; my question is . . How to create a HEIC file based on 2 JEPGs one with the image itself and the second JPG file with depth map; in order to build something like an iPhone Portrait file.

I tried this but didn’t work as I supposed it; anyway I gave it a try.

magick DSC_0035.jpg +matte DSC_0035_depth.jpg jays.heic

Anyone could please Clarify me how get it accomplished