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?".
dfelder
Posts: 42
Joined: 2011-02-25T16:49:09-07:00
Authentication code: 8675308

Determine Total Canvas Size WITH Transparent Margins?

Post by dfelder »

I have a png file with a 1000 x 1000 canvas and a transparent background. Centered within this canvas I have a 500 x 500 black square.

In other words, there are 250 pixels of transparent margin on each side of the square.

What command do I use to get the full canvas size (1000 x 1000)?

If I use identify, it will crop/trim the image and report back 500x500+250+250. That essentially ignores the blank space to the right and bottom. I want to determine the full size of the canvas--including transparency--regardless of its contents.

Thanks.
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 »

You want the virtual canvas. identify reports both by default...

For example...
paged.gif GIF 46x46 64x64+9+9 8-bit PseudoClass 256c 3.09KB 0.000u 0:00.000

This is a 46x46 actual image, on a larger 64x64 virtual canavs with a +9+9 offset
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 »

Not exactly, no. The virtual canvas reports:

500x500 (the dimensions of the square) and +250+250 (the X/Y offset relative to the upper-left-hand pixel). I want to know that there are pixels to the right and bottom of my square.

With identify, it's essentially ignoring the fact that there are 250 more transparent pixels to the right and 250 more transparent pixels to the bottom of my square. For all intents and purposes, it's telling me my canvas is 750 x 750.
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 »

Is this a PNG?

BY default PNG images does not save virtual canvas size information, just its offset. As such IM has to make a guess.

PNG images saved by IM however does include a extra meta-data tag that only IM understands that does include the virtual canavs size of images. But this is not a standard file format meta-data.

As such from PNG images coming from another source that information is not available.
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 »

It's a PNG or a PSD. If I open either file type in PhotoShop or Microsoft Picture Manager, both programs "know" to center the image on the screen within the canvas, and both programs reveal the transparent margin all around the image. In addition, one can view the "total" dimensions in Windows Explorer's expanded attributes. It's just difficult to extract them through the Shell.

It may not be standard, but in our business world (printing) it definitely is. We print everything centered along a centerline. Without knowing there are extra pixels off to the right, we'd never position it correctly.

Take for example two square images:

Image one: 400 x 400 (transparent) with a 200 x 400 (solid rectangle) LEFT justified
Image two: 400 x 400 (transparent) with a 200 x 400 (solid rectangle) RIGHT justified

If I print one on top of the other, I want the following results:

XX00
XX00
00XX
00XX

If I centered along the line and used 1/2 of the IM calculated width, I'd get:

_XX
_XX
00XX
00XX

Make sense?
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 »

If the image was created by ImageMagick in PNG format, then by
default the virtual canvas dimensions are stored in the vpAg chunk
which is a private ImageMagick chunk.

The pngtest.c program that comes with libpng demonstrates
how to read it into your own application, using libpng's user-chunk
feature.

Within ImageMagick, the virtual canvas is stored as page.width
and page.height .

I believe you can use the -flatten option to expand the image
to include the canvas; the surrounding pixels will be written
in the background_color:

Code: Select all

     convert image_with_vpAg.png -flatten image_with_canvas.png
Sorry I can't test this right now, I'm writing from a public library
computer.

../glennrp
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 »

Unfortunately, these files are NOT created in IM. They are typically created in PhotoShop or GIMP. They are "client" files and I need to measure them. They definitely have blank margin to the right and bottom, and I am happy to share with you an example so you can see my conundrum.

Also, if I flatten them, then it ruins the original art. I'd have to first make a copy. If I flatten, the identify works without issue. The problem is, sometimes these files can be 5-50MB, so copying them is too inefficient.

As I mentioned earlier, it's an extended attribute that the Window Explorer Shell can see and display within the Windows folder view, so it's definitely there.

Thanks for the ideas.
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 »

Photoshop presumably has its own 'special' chunk or meta-data, that defines a 'virtual canvas'. As such only photoshop knows about this information as it is specific to that application.
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 »

Actually, that's only partially correct. Photoshop knows it's there but so do all the microsoft apps. The images center properly in Microsoft photo editor and MS photo manager. It's definitely not a secret code. Like I have mentioned several times, even Windows explorer knows the full canvas size.

When I mention Windows explorer, I'm talking about the Windows directory. When you look at the image in the directory, by default it shows file type, date created, etc. If you right click in the top bar you can also view extended attributes such as DPI, date modified, AND diimensions.

This is standard since Windows Vista and the dimensions attribute reflects the entire canvas.
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 »

By the way, if there's a way to upload an image, I can provide screen shots.
Tagomago
Posts: 36
Joined: 2011-02-10T09:55:33-07:00
Authentication code: 8675308

Re: Determine Total Canvas Size WITH Transparent Margins?

Post by Tagomago »

If you can upload it elsewhere online, use the tag to put it in the post.
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 did a pretty extensive round of testing (results below), and you can see the deficiency for the PSD files (it looks like it's OK for the PNGs). It seems that the PixelXDimension and PixelYDimension variables (from identify -verbose) contains the required data for PSD files. The dimension data is at the very bottom of my results.

Is there a way to directly extract those two specific values using IM? In other words, can I directly obtain:

X = PixelXDimension
Y = PixelYDimension

without having extract it from the long analysis resulting from the verbose command?

<analysis results>

solid_400_400.psd
IM IDENTIFY:
Image: solid_400_400.psd
Format: PSD (Adobe Photoshop bitmap)
Geometry: 400x400+0+0
Page geometry: 400x400+0+0
______________________________________

solid_400_400.png
IM IDENTIFY:
Image: solid_400_400.png
Format: PNG (Portable Network Graphics)
Geometry: 400x400+0+0
Page geometry: 400x400+0+0
______________________________________

solid_200_400_right_justified_inside_400_400.psd
IM IDENTIFY:
Image: solid_200_400_right_justified_inside_400_400.psd
Format: PSD (Adobe Photoshop bitmap)
Geometry: 200x400+0+0
Page geometry: 200x400+200+0
Origin geometry: +200+0
______________________________________

solid_200_400_right_justified_inside_400_400.png
IM IDENTIFY:
Image: solid_200_400_right_justified_inside_400_400.png
Format: PNG (Portable Network Graphics)
Geometry: 400x400+0+0
Page geometry: 400x400+0+0
______________________________________

solid_200_400_left_justified_inside_400_400.psd
IM IDENTIFY:
Image: solid_200_400_left_justified_inside_400_400.psd
Format: PSD (Adobe Photoshop bitmap)
Geometry: 200x400+0+0
Page geometry: 200x400+0+0
______________________________________

solid_200_400_left_justified_inside_400_400.png
IM IDENTIFY:
Image: solid_200_400_left_justified_inside_400_400.png
Format: PNG (Portable Network Graphics)
Geometry: 400x400+0+0
Page geometry: 400x400+0+0
______________________________________

solid_200_400_left_justified_inside_400_400_WINDOWS_PHOTO_VIEWER.png
IM IDENTIFY:
Image: solid_200_400_left_justified_inside_400_400_WINDOWS_PHOTO_VIEWER.png
Format: PNG (Portable Network Graphics)
Geometry: 400x400+0+0
Page geometry: 400x400+0+0
______________________________________

solid_200_200_centered_inside_400_400.psd
IM IDENTIFY:
Image: solid_200_200_centered_inside_400_400.psd
Format: PSD (Adobe Photoshop bitmap)
Geometry: 200x200+0+0
Page geometry: 200x200+100+100
Origin geometry: +100+100
______________________________________

solid_200_200_centered_inside_400_400.png
IM IDENTIFY:
Image: solid_200_200_centered_inside_400_400.png
Format: PNG (Portable Network Graphics)
Geometry: 400x400+0+0
Page geometry: 400x400+0+0
______________________________________

solid_200_200_centered_inside_400_400_WINDOWS_PHOTO_VIEWER.png
IM IDENTIFY:
Image: solid_200_200_centered_inside_400_400_WINDOWS_PHOTO_VIEWER.png
Format: PNG (Portable Network Graphics)
Geometry: 400x400+0+0
Page geometry: 400x400+0+0

***************************

Image: offset.psd
Format: PSD (Adobe Photoshop bitmap)
Class: DirectClass
Geometry: 200x200+0+0
Page geometry: 200x200+165+166
Origin geometry: +165+166

exif:PixelXDimension: 400
exif:PixelYDimension: 400
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 »

Have you tried exiftool on your (PSD) images?

see http://www.sno.phy.queensu.ca/~phil/exiftool/

Also see string formats in IM to get individual values --- http://www.imagemagick.org/script/escape.php
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 »

You can upload images to the web using something like 'dropbox.com' You con't actually need the client, as you can upload them just using the web interface.
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 »

EXIFTOOL.exe reads them with "PixelXDimension" and "PixelYDimension" without issue. It's also clear that IM can read the dimensions I need in the PSD. They are identified in IM as:

exif:PixelXDimension: 400
exif:PixelYDimension: 400

Is there a way to obtain these values in IM without the -verbose method?

I'm scripting this so I need to extract:

XWidth = 000
YWidth = 000
Post Reply