Determine Total Canvas Size WITH Transparent Margins?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Determine Total Canvas Size WITH Transparent Margins?

Post by fmw42 »

see http://www.imagemagick.org/script/escape.php

convert image -format "%[exif:...]" info:`
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Determine Total Canvas Size WITH Transparent Margins?

Post by anthony »

In that case you should be able to reset the virtual canvas to those dimensions using

Code: Select all

   -set page '%[exif:PixelXDimension]x%[exif:PixelYDimension]+%X+%Y'
after reading in the image.

That assumes the PNG virtual offset was also set correctly. It may be that the offsets should also come from EXIF tags as well. As some formats like JPEG does not even provide the normal virtual canvas 'offset', I would think offsets also are available in the EXIF tag. Perhaps a special -repage EXIF type operation can be added as a shortcut.

NOTE: I have not tested the above as I do not have an example image, and you have not provided a link to one

I do not know if IM can change those settings before writing. If currently can modify 'exif:Orientation'!
http://www.imagemagick.org/Usage/photos/#orient
But should IM do read/write this setting, if such a EXIF tag exists in an image?
This is something that may need some debate by users.
I think it would be a 'cool' addition and could possibly be used to replace the existing 'special IM meta-data' it provides for PNG images.

WARNING: Adding virtual canvas settings can effect many IM image processing methods, such as distortions, cropping, and even resize. Removing virtual canvas settings is however easy using +repage
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
dfelder
Posts: 42
Joined: 2011-02-25T16:49:09-07:00
Authentication code: 8675308

Re: Determine Total Canvas Size WITH Transparent Margins?

Post by dfelder »

First off, I really appreciate the help here. You can see I only joined this forum for this question. I do actively provide help on several other boards (if it makes you feel better!), so again, thanks...

Second, I have posted a series of files here: http://garmentdeli.com/IM/
At the end of the day, I need to extract true total canvas X & Y, and DPI (NOT DP-Centimeter!) from PNGs and PSDs similar to these samples. Any help would be greatly appreciated.

BTW, I'm working under the assumption IM won't work so I keep experiementing with Shell code, and EXIF extractors. I'm holding out for IM, though, because I already got my application to integrate with it.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: Determine Total Canvas Size WITH Transparent Margins?

Post by Drarakel »

Well, you first wrote a lot about PNG and that getting the canvas works with e.g. Windows Explorer, but not with ImageMagick. When there's obviously no problem at all with PNGs. That makes helping a bit difficult..


Anyway.. Your PNGs don't even have an offset - so getting the canvas is of course easy there.
With PSDs, it normally should work, too. But there seems to be a bug with some of these files within the current IM. (I'll write a bug report.)
Till there's a fix for that, you could theoretically use that workaround with the dimensions in the EXIF profile. But I wouldn't recommend that at all. There doesn't have to be such a profile in PSD files. And even if there is, the information doesn't have to be correct (it could be the information from an older version of the image). It's just metadata which has no influence on the image itself.

So.. right now you can either go back to an older ImageMagick version - where this type of PSD files still works. Or use e.g. ExifTool to get the right properties from the (Photoshop) header (not from the EXIF tags!):

Code: Select all

exiftool -S -s -ImageSize file.psd
(Or use the separate tags "-Photoshop:ImageWidth" and "-Photoshop:ImageHeight" instead.)

dfelder wrote:I need to extract ... DPI (NOT DP-Centimeter!) from PNGs and PSDs
You can do that with ImageMagick like that, for example:

Code: Select all

identify -units PixelsPerInch -format "%x\n%y" file
That should work ok - at least for PNGs. Note that the information for the density values and the unit can be stored in a lot of different places/profiles. If you have differing (outdated) values stored at these places, you'll get into trouble with ImageMagick. And with PSD, it's especially risky. (Here, IM takes the density from the Photoshop profile, but the unit from the XMP profile. And if there's no XMP profile, the unit in IM is 'undefined' at first. It seems that the unit in the Photoshop profile doesn't get read by IM.)
The other possibility is again with e.g. ExifTool. There, you could specifically read only the info from the Photoshop profile (with PSDs), or from the pHYs chunk (with PNGs). But then, if you have a value in PixelsPerCentimeter, you'll get no automatic conversion to PixelsPerInch, of course.
dfelder
Posts: 42
Joined: 2011-02-25T16:49:09-07:00
Authentication code: 8675308

Re: Determine Total Canvas Size WITH Transparent Margins?

Post by dfelder »

Thanks for the suggestion. I have been concurrently working with EXIFTOOL, but have had a difficult time incorporating it into VBA. I have IM working, so it's much preferred.

Can you confirm that this DID work with previous versions of IM? Before I go through a re-install and testing, I'd like to make sure I'm using a version that works.

Thanks.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: Determine Total Canvas Size WITH Transparent Margins?

Post by Drarakel »

dfelder wrote:Can you confirm that this DID work with previous versions of IM?
Yes. See some details in the bug report.

But I think, you don't need to go back to an older IM version now - as the fix for that problem has already been incorporated into the new version 6.6.8-1. (Can't test it though, as at least the current Q16 Win32 binary is still version 6.6.8-0.)
So, with that new version, you should be able to get the size again with just:

Code: Select all

identify -format "%Wx%H" file.psd
dfelder
Posts: 42
Joined: 2011-02-25T16:49:09-07:00
Authentication code: 8675308

Re: Determine Total Canvas Size WITH Transparent Margins?

Post by dfelder »

I am replying only because we are both actively look at this today. Is this the 6.8.8-1 to which you are referring:

http://www.imagemagick.org/script/binar ... hp#windows

I actually think it was just released.
dfelder
Posts: 42
Joined: 2011-02-25T16:49:09-07:00
Authentication code: 8675308

Re: Determine Total Canvas Size WITH Transparent Margins?

Post by dfelder »

I just tried the version with 6.6.8-1 in the release, but:

1) it installed as "6.6.8-0"
2) it still doesn't work

Just thought I'd pass this along.

What I'm expecting to see is this:

identify -format %Wx%H offset.png
400x400

identify -format %Wx%H offset.psd
400x400

Is my assumption correct? (These are the files downloaded from my link above.)
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: Determine Total Canvas Size WITH Transparent Margins?

Post by Drarakel »

dfelder wrote:Is my assumption correct?
Yes. See also:
Drarakel wrote:at least the current Q16 Win32 binary is still version 6.6.8-0.
Just wait a few more days. Or ask at the 'Bugs' board if the Windows binaries can be updated.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Determine Total Canvas Size WITH Transparent Margins?

Post by glennrp »

dfelder wrote: Second, I have posted a series of files here: http://garmentdeli.com/IM/
There's no offset or canvas information in those PNG files. The IHDR
chunk contains only the canvas dimensions, not the dimensions of
the overlaid image.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: Determine Total Canvas Size WITH Transparent Margins?

Post by Drarakel »

dfelder wrote:What I'm expecting to see is this:
...
identify -format %Wx%H offset.psd
400x400
FYI: It now works with IM v6.6.8-2. The windows binaries have been updated:
http://www.imagemagick.org/script/binar ... hp#windows

Note that with PSDs, you now have to add the layer number 0 (for the composite layer; otherwise you'll get the sizes for all the layers). So, it's now:

Code: Select all

identify -format "%Wx%H" file.psd[0]
dfelder
Posts: 42
Joined: 2011-02-25T16:49:09-07:00
Authentication code: 8675308

Re: Determine Total Canvas Size WITH Transparent Margins?

Post by dfelder »

Thanks for your help on this. As it turns out, the [0] actually works with the old version, as well. :D

Best to all!
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: Determine Total Canvas Size WITH Transparent Margins?

Post by Drarakel »

dfelder wrote:As it turns out, the [0] actually works with the old version, as well.
I see.. Interesting. :) (But still, the old versions had a bug there.)
Post Reply