Trim/crop by alpha

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?".
WexOneOne
Posts: 11
Joined: 2018-10-22T14:01:44-07:00
Authentication code: 1152

Trim/crop by alpha

Post by WexOneOne » 2018-10-22T14:17:46-07:00

Hello all.

I have a few images(EXR) that have alpha on them, I wish to trim the image by the alpha, if possible with padding of 10px from every side of the trim.
I'm not sure I found the correct way to do so... does anyone have an idea?

Looking to make this image:

Code: Select all

|--------------------------------------|
|                                      |
|                                      |
|  image with alpha here               |
|                                      |
|                                      |
|--------------------------------------|
to:

Code: Select all

|---------------------|
|   HAD alpha here    |
|---------------------|
Any help will do.

Thank you.

snibgo
Posts: 12271
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trim/crop by alpha

Post by snibgo » 2018-10-22T14:33:14-07:00

What version of IM on what platform?

Extract the alpha to a grayscale image, then "-format %@" gives you what the trim dimensions would be. Read these into a script and do whatever adjustment you want, and use that in a "-crop".

Or it can be done in a single v7 command. Or even v6, but that is messy.
snibgo's IM pages: im.snibgo.com

WexOneOne
Posts: 11
Joined: 2018-10-22T14:01:44-07:00
Authentication code: 1152

Re: Trim/crop by alpha

Post by WexOneOne » 2018-10-22T14:42:29-07:00

Yes, of course I'll forget to put the important info here..... :-P
Version: ImageMagick 7.0.7-15 Q16 x86_64 2017-12-21
OS: Linux CentOS7

I'll admit, the one liner sounds like what I need/want.

To be clear, by what you wrote, I'll need to extract the alpha to a new gray-scale image and crop that new image?

snibgo
Posts: 12271
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trim/crop by alpha

Post by snibgo » 2018-10-22T15:30:36-07:00

I've re-read what you asked for, and perhaps I misunderstand. How about:

Code: Select all

magick in.exr -trim +repage -gravity center -bordercolor none -border 10 out.png
Does that do what you want?

EDIT: corrected to "+repage" instead of "-repage".
snibgo's IM pages: im.snibgo.com

WexOneOne
Posts: 11
Joined: 2018-10-22T14:01:44-07:00
Authentication code: 1152

Re: Trim/crop by alpha

Post by WexOneOne » 2018-10-22T16:04:35-07:00

That looks like it might work, now why I say "might" you ask.. well, now I see that our IM might be installed incorrectly...

I run this command, which looks so pretty and probably what I do need... however, the result is:

Code: Select all

magick: UnableToOpenConfigureFile `magic.xml' @ warning/configure.c/GetConfigureOptions/714.
magick: UnableToOpenConfigureFile `delegates.xml' @ warning/configure.c/GetConfigureOptions/714.
magick: NoDecodeDelegateForThisImageFormat `EXR' @ error/constitute.c/ReadImage/509.
However, when I run a search for those XML files, in the parent folder of the installation:

Code: Select all

find . -name "*.xml"
I get:

Code: Select all

./lib/ImageMagick-7.0.7/config-Q16HDRI/configure.xml
./etc/ImageMagick-7/coder.xml
./etc/ImageMagick-7/colors.xml
./etc/ImageMagick-7/delegates.xml
./etc/ImageMagick-7/log.xml
./etc/ImageMagick-7/magic.xml
./etc/ImageMagick-7/mime.xml
./etc/ImageMagick-7/policy.xml
./etc/ImageMagick-7/quantization-table.xml
./etc/ImageMagick-7/thresholds.xml
./etc/ImageMagick-7/type.xml
./etc/ImageMagick-7/type-apple.xml
./etc/ImageMagick-7/type-dejavu.xml
./etc/ImageMagick-7/type-ghostscript.xml
./etc/ImageMagick-7/type-urw-base35.xml
./etc/ImageMagick-7/type-windows.xml
./share/ImageMagick-7/english.xml
./share/ImageMagick-7/francais.xml
./share/ImageMagick-7/locale.xml
./share/doc/ImageMagick-7/www/source/coder.xml
./share/doc/ImageMagick-7/www/source/colors.xml
./share/doc/ImageMagick-7/www/source/configure.xml
./share/doc/ImageMagick-7/www/source/delegates.xml
./share/doc/ImageMagick-7/www/source/english.xml
./share/doc/ImageMagick-7/www/source/francais.xml
./share/doc/ImageMagick-7/www/source/locale.xml
./share/doc/ImageMagick-7/www/source/log.xml
./share/doc/ImageMagick-7/www/source/magic.xml
./share/doc/ImageMagick-7/www/source/mime.xml
./share/doc/ImageMagick-7/www/source/policy.xml
./share/doc/ImageMagick-7/www/source/thresholds.xml
./share/doc/ImageMagick-7/www/source/type-ghostscript.xml
./share/doc/ImageMagick-7/www/source/type-windows.xml
./share/doc/ImageMagick-7/www/source/type.xml
./ImageMagick_win_7.0.7/english.xml
./ImageMagick_win_7.0.7/locale.xml
./ImageMagick_win_7.0.7/log.xml
./ImageMagick_win_7.0.7/magic.xml
./ImageMagick_win_7.0.7/mime.xml
./ImageMagick_win_7.0.7/policy.xml
./ImageMagick_win_7.0.7/quantization-table.xml
./ImageMagick_win_7.0.7/thresholds.xml
./ImageMagick_win_7.0.7/type.xml
./ImageMagick_win_7.0.7/type-ghostscript.xml
./ImageMagick_win_7.0.7/www/source/coder.xml
./ImageMagick_win_7.0.7/www/source/colors.xml
./ImageMagick_win_7.0.7/www/source/configure.xml
./ImageMagick_win_7.0.7/www/source/delegates.xml
./ImageMagick_win_7.0.7/www/source/english.xml
./ImageMagick_win_7.0.7/www/source/francais.xml
./ImageMagick_win_7.0.7/www/source/locale.xml
./ImageMagick_win_7.0.7/www/source/log.xml
./ImageMagick_win_7.0.7/www/source/magic.xml
./ImageMagick_win_7.0.7/www/source/mime.xml
./ImageMagick_win_7.0.7/www/source/policy.xml
./ImageMagick_win_7.0.7/www/source/quantization-table.xml
./ImageMagick_win_7.0.7/www/source/thresholds.xml
./ImageMagick_win_7.0.7/www/source/type-apple.xml
./ImageMagick_win_7.0.7/www/source/type-dejavu.xml
./ImageMagick_win_7.0.7/www/source/type-ghostscript.xml
./ImageMagick_win_7.0.7/www/source/type-urw-base35.xml
./ImageMagick_win_7.0.7/www/source/type-windows.xml
./ImageMagick_win_7.0.7/www/source/type.xml
./ImageMagick_win_7.0.7/colors.xml
./ImageMagick_win_7.0.7/configure.xml
./ImageMagick_win_7.0.7/delegates.xml
So.. yeah, I think something is wrong...
Does IM has a command to check if the current session finds the right XML?
Could it be that we will just need to replace this guy:

Code: Select all

magick: NoDecodeDelegateForThisImageFormat `EXR' @ error/constitute.c/ReadImage/509.
I mean... could this all be just a read error?
Our Delegate xml is wrong/old? re-installation is needed?

Okay, last thing... via Python, I guess it will be something like....:

Code: Select all

import OpenImageIO as oiio
source = oiio.ImageBuf(filepath)
output = '/../../output.png
... no idea...need to check DOCs :)
wow, I really dont know where to go from here, dang it, so close :/
Thank you so much for looking on this with me.

User avatar
fmw42
Posts: 25654
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trim/crop by alpha

Post by fmw42 » 2018-10-22T16:16:01-07:00

Can you post a real image that you need to be cropped?

snibgo
Posts: 12271
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trim/crop by alpha

Post by snibgo » 2018-10-22T16:19:48-07:00

Does "magick" work at all? How about:

Code: Select all

magick -version
magick rose: x.png
magick x.png x.jpg
If those don't work, you need to reinstall IM and/or delegates.

If they do work, can you read EXR files?

Code: Select all

magick in.exr NULL:
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25654
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trim/crop by alpha

Post by fmw42 » 2018-10-22T16:28:40-07:00

EXR requires the OpenEXR delegate library. See http://www.openexr.com

WexOneOne
Posts: 11
Joined: 2018-10-22T14:01:44-07:00
Authentication code: 1152

Re: Trim/crop by alpha

Post by WexOneOne » 2018-10-22T16:29:18-07:00

Of course,
Image is here
Thank you.

WexOneOne
Posts: 11
Joined: 2018-10-22T14:01:44-07:00
Authentication code: 1152

Re: Trim/crop by alpha

Post by WexOneOne » 2018-10-22T16:36:57-07:00

-version works:

Code: Select all

Version: ImageMagick 7.0.7-15 Q16 x86_64 2017-12-21 http://www.imagemagick.org
Copyright: © 1999-2018 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP 
Delegates (built-in): fontconfig freetype jng jpeg lzma png tiff x xml zlib
magick rose: x.png works

magick x.png x.jpg Fails:

Code: Select all

convert: UnableToOpenConfigureFile `magic.xml' @ warning/configure.c/GetConfigureOptions/714.

snibgo
Posts: 12271
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trim/crop by alpha

Post by snibgo » 2018-10-22T17:03:56-07:00

WexOneOne wrote:Delegates (built-in): fontconfig freetype jng jpeg lzma png tiff x xml zlib
This doesn't have OpenEXR, so your IM can't read EXR.

I suggest you install OpenEXR, then rebuild IM.

My corrected command (see my edit above) gives:
Image
snibgo's IM pages: im.snibgo.com

WexOneOne
Posts: 11
Joined: 2018-10-22T14:01:44-07:00
Authentication code: 1152

Re: Trim/crop by alpha

Post by WexOneOne » 2018-10-22T17:12:20-07:00

Ok, so, once we fix the OpenEXR thingy, the command:

Code: Select all

magick in.exr -trim +repage -gravity center -bordercolor none -border 10 out.png
is what I'm looking for!
That is great, hopefully we can fix this this week.

Thank you so much !!

User avatar
fmw42
Posts: 25654
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trim/crop by alpha

Post by fmw42 » 2018-10-22T17:18:21-07:00

magick x.png x.jpg Fails:
That is odd, since you do have both PNG and JPG installed in your delegates.

WexOneOne
Posts: 11
Joined: 2018-10-22T14:01:44-07:00
Authentication code: 1152

Re: Trim/crop by alpha

Post by WexOneOne » 2018-10-23T09:11:07-07:00

In the next few days we will do a full install of IM again and see how it goes after.
I'll update.

WexOneOne
Posts: 11
Joined: 2018-10-22T14:01:44-07:00
Authentication code: 1152

Re: Trim/crop by alpha

Post by WexOneOne » 2018-10-23T09:39:39-07:00

Okay, so....
I installed IM.
All rose tests work.
The only thing is, it still does not read EXR.

Code: Select all

magick withAlpha.exr -trim +repage -gravity center -bordercolor none -border 10 out.png
Error:

Code: Select all

magick: no decode delegate for this image format `EXR' @ error/constitute.c/ReadImage/556.
we are down to one error message though, thats better :-P
I'm guessing like it was said before here, I need to grab that XML from OpenEXR.
OR do I need to install OpenEXR and Re-install IM ?!

Post Reply