Tip/Trick: Speeding up RAW to JPEG conversion for previews

221 views
Skip to first unread message

Paul

unread,
Oct 6, 2011, 6:41:39 PM10/6/11
to ResourceSpace
Hi everyone,

At my company, we are using StaticSync and leaving our source images
in their original location. The only images that are created for
ResourceSpace are for preview/thumbnail purposes. So, we were finding
ImageMagick/ufraw pretty slow at converting CR2 files (20-30secs a
file). Then I learned the Canon 5D Mark II embeds a full res JPG in
the CR2 (I think many other cameras do this as well... although maybe
not full res jpegs). Extracting this embedded jpg with ufraw-batch is
super-fast. The embedded jpg might not be the highest quality, but it
looks pretty nice and works for our purposes. So I wrote some quick
code to skip the RAW conversion if an embedded jpg of sufficient
resolution is found.

This code probably doesn't take every case into consideration. I am
not familiar enough with ResourceSpace as a whole yet. BUT, I am
sharing as a reference for anyone who finds themselves in a similar
situation:


image_processing.php -- in function create_previews_using_im()
..
..

$hpr_path=get_resource_path($ref,true,"hpr",false,"jpg",-1,1,false,"",
$alternative);
if (file_exists($hpr_path) && !$previewbased) {unlink($hpr_path);}

$lpr_path=get_resource_path($ref,true,"lpr",false,"jpg",-1,1,false,"",
$alternative);
if (file_exists($lpr_path) && !$previewbased) {unlink($lpr_path);}

$scr_path=get_resource_path($ref,true,"scr",false,"jpg",-1,1,false,"",
$alternative);
if (file_exists($scr_path) && !$previewbased) {unlink($scr_path);}

$scr_wm_path=get_resource_path($ref,true,"scr",false,"jpg",-1,1,true,"",
$alternative);
if (file_exists($scr_wm_path) && !$previewbased)
{unlink($scr_wm_path);}


$emb_jpg_path=get_resource_path($ref,true,"emb",false,"jpg",-1,1,false,"",
$alternative);
if (file_exists($emb_jpg_path)) {unlink($emb_jpg_path);}

$prefix = '';
# Camera RAW images need prefix
if (preg_match('/^(dng|nef|x3f|cr2|crw|mrw|orf|raf|dcr)$/i',
$extension, $rawext)) { $prefix = $rawext[0] .':'; }

# Locate imagemagick.
$identcommand=$imagemagick_path . "/bin/identify";
if (!file_exists($identcommand)) {$identcommand=$imagemagick_path .
"/identify";}
if (!file_exists($identcommand)) {$identcommand=$imagemagick_path .
"\identify.exe";}
if (!file_exists($identcommand)) {exit("Could not find ImageMagick
'identify' utility.'");}
$orig_identcommand=$identcommand;

$sizes="";
if ($thumbonly) {$sizes=" where id='thm' or id='col'";}
if ($previewonly) {$sizes=" where id='thm' or id='col' or id='pre'
or id='scr'";}

$ps=sql_query("select * from preview_size $sizes order by width
desc, height desc");


#################################################################################################################################
# FOR SPEED PURPOSES
# if RAW format, attempt to pull embedded jpg out.
# if the embedded jpg is large enough to generate all our preview
formats go with it. This forgoes the timely process of
# converting the actual RAW data.

##################################################################################################################################
$sw=-1;$sh=-1;
$using_emb_jpg = false;
if(count($ps)>0 && in_array($extension, array("cr2", "crw", "dng",
"nef")))
{
$emb_jpg_command = "/usr/bin/ufraw-batch --
output=".escapeshellarg($emb_jpg_path)." --embedded-image
".escapeshellarg($file);
$emb_jpg_output=shell_exec($emb_jpg_command);

if(file_exists($emb_jpg_path))
{
# Get image's dimensions.
$identcommand = $orig_identcommand . ' -format %wx%h '.
escapeshellarg($emb_jpg_path) .'[0]';

$identoutput=shell_exec($identcommand);
preg_match('/^([0-9]+)x([0-9]+)$/ims',$identoutput,$smatches);
if ((@list(,$emd_jpg_sw,$emd_jpg_sh) = $smatches)===false)
{ return false; }

if($emd_jpg_sw>=$ps[0]["width"] && $emd_jpg_sh>=$ps[0]["height"])
{
$using_emb_jpg = true;
$file = $emb_jpg_path;
$extension = "jpg";
$sw = $emd_jpg_sw;
$sh = $emd_jpg_sh;
}
}
}

# If an embedded jpeg wasn't found in a RAW format or the orig
source is not RAW, determine orig source's dimensions
# Get image's dimensions.
if(!$using_emb_jpg)
{
$identcommand = $orig_identcommand . ' -format %wx%h '.
escapeshellarg($prefix . $file) .'[0]';

$identoutput=shell_exec($identcommand);
preg_match('/^([0-9]+)x([0-9]+)$/ims',$identoutput,$smatches);
if ((@list(,$sw,$sh) = $smatches)===false) { return false; }
}

for ($n=0;$n<count($ps);$n++)
{

..
..
..


# if we extracted an embedded jpg from a RAW file, delete it now.
if (file_exists($emb_jpg_path)) {unlink($emb_jpg_path);}

return true;

Paul

unread,
Oct 6, 2011, 6:44:51 PM10/6/11
to ResourceSpace
Well that got formatted to hell. Let me know if there's a proper way
to post code OR if anyone wants this via a different means.

-Paul

Tom Gleason

unread,
Oct 6, 2011, 6:50:41 PM10/6/11
to resour...@googlegroups.com
$cr2_thumb_extract=true;
doesn't do that already?

> --
> You received this message because you are subscribed to the Google Groups "ResourceSpace" group.
> To post to this group, send email to resour...@googlegroups.com.
> To unsubscribe from this group, send email to resourcespac...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/resourcespace?hl=en.
>
>

--
Tom Gleason

Andy Blackham

unread,
Oct 7, 2011, 3:46:59 AM10/7/11
to resour...@googlegroups.com
Its worth noting that embedding a JPEG at the time he RAW is saved is a
'option' in the camera, it doesn't have to be full res, but it can.

You cannot assume that all CR's will have an embedded preview.

Andy

Paul

unread,
Oct 12, 2011, 7:55:11 PM10/12/11
to ResourceSpace
$cr2_thumb_extract=true;

does seem to work. However, I'm not sure what it would do if the
embedded jpeg was not of sufficient resolution. The code snippet I
attached checks the embedded image's resolution against the format(s)
that will be generated. If the resolution is too low, it reverts back
to the orginal RAW source. This logic also addresses what Andy
brought up. I was careful in not assuming there is an embedded jpeg.


-Paul

On Oct 7, 12:46 am, Andy Blackham <andy.black...@ntlworld.com> wrote:
> Its worth noting that embedding a JPEG at the time he RAW is saved is a
> 'option' in the camera, it doesn't have to be full res, but it can.
>
> You cannot assume that all CR's will have an embedded preview.
>
> Andy
>
> On 06/10/2011 23:50, "Tom Gleason" <t...@buildadam.com> wrote:
>
>
>
>
>
>
>
> > $cr2_thumb_extract=true;
> > doesn't do that already?
>
> > On Thu, Oct 6, 2011 at 6:44 PM, Paul <phudson1...@gmail.com> wrote:
> >> Well that got formatted to hell.  Let me know if there's a proper way
> >> to post code  OR  if anyone wants this via a different means.
>
> >> -Paul
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "ResourceSpace" group.
> >> To post to this group, send email to resour...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> resourcespac...@googlegroups.com.
> >> For more options, visit this group at
> >>http://groups.google.com/group/resourcespace?hl=en.
>
>
>
>  smime.p7s
> 4KViewDownload
Reply all
Reply to author
Forward
0 new messages