[HELP] Trim the dimensions around diagram

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?".
Post Reply
ashishawadekar
Posts: 5
Joined: 2017-06-06T07:49:46-07:00
Authentication code: 1151

[HELP] Trim the dimensions around diagram

Post by ashishawadekar »

Hello there,

I want to trim the dimensions, dimension lines which are present around the window drawing and preserve only the window image. How can do that using Image Magick ?

Image

Version: ImageMagick 7.0.5-5 Q16 x86_64 2017-04-25
Running on macOS Sierra 10.12.5

Can anyone please help?

Thanks in advance.

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

Re: [HELP] Trim the dimensions around diagram

Post by fmw42 »

Please do not double post to two different forums.

Do you want to trim this diagram or an image to this format? If the latter, then please supply an input image. If the former, what area do you want left -- the white center only?
ashishawadekar
Posts: 5
Joined: 2017-06-06T07:49:46-07:00
Authentication code: 1151

Re: [HELP] Trim the dimensions around diagram

Post by ashishawadekar »

Thanks for your swift response fmw42.

The input image is the one which I have shared earlier.

The resultant image should look like this :

Image

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

Re: [HELP] Trim the dimensions around diagram

Post by fmw42 »

Is this what you want? I change all white to black, then get the trim coordinates, then crop the originals using the trim coordinates. You would be better off using a non-compressed format for your diagram, such as png or tif.

Code: Select all

magick UY6RfhU.jpg -write mpr:img -fuzz 10% -fill black -opaque white -set option:cropvals "%@" +delete mpr:img -crop "%[cropvals]" +repage UY6RfhU_crop.png
Image
ashishawadekar
Posts: 5
Joined: 2017-06-06T07:49:46-07:00
Authentication code: 1151

Re: [HELP] Trim the dimensions around diagram

Post by ashishawadekar »

Thanks a lot for the response fmw42.

I tried the command on my system, unfortunately, it is not working.

Can you please give this same effect with mogrify?

Also it would be great if you can explain how this command actually works.

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

Re: [HELP] Trim the dimensions around diagram

Post by fmw42 »

The command probably will not work in mogrify. The explanation is above my result.

-write mpr:img (saves a copy of the input)
-fuzz 10% -fill black -opaque white (changes all white to black)
-set option:cropvals "%@" (saves the trim values as cropvals without having to use -trim)
+delete mpr:img (deletes the previous version that had black replacing white and reloads the input image)
-crop "%[cropvals]" +repage (crops the input image using the stored cropvals)

My command works on IM 7.0.5-10 Q16 Mac OSX Sierra. So perhaps you need to upgrade ImageMagick, unless you copied the command and it got split to more than one line. If so, make sure you edit the command so that it is one command on one long line of text. Then copy that and paste into a terminal window.

I suggest if you want to use this on lots of images, you write a script loop over each image and then call this command.

One issue is that your images are jpg. This means they are compressed and the solid areas of colors are not going to be perfectly uniform. Thus the -fuzz 10% may need to be changed for each image. That is why I suggested that your input images be non-lossy-compressed formats such as png or tif. Then the -fuzz should not be needed. However, you cannot just change jpg to png, since the loss is already there now.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: [HELP] Trim the dimensions around diagram

Post by Bonzo »

One thing I would do to neaten up the image would be to add a couple of pixel wide black border.
ashishawadekar
Posts: 5
Joined: 2017-06-06T07:49:46-07:00
Authentication code: 1151

Re: [HELP] Trim the dimensions around diagram

Post by ashishawadekar »

fmw42 wrote: 2017-06-06T10:53:09-07:00 The command probably will not work in mogrify. The explanation is above my result.

-write mpr:img (saves a copy of the input)
-fuzz 10% -fill black -opaque white (changes all white to black)
-set option:cropvals "%@" (saves the trim values as cropvals without having to use -trim)
+delete mpr:img (deletes the previous version that had black replacing white and reloads the input image)
-crop "%[cropvals]" +repage (crops the input image using the stored cropvals)

My command works on IM 7.0.5-10 Q16 Mac OSX Sierra. So perhaps you need to upgrade ImageMagick, unless you copied the command and it got split to more than one line. If so, make sure you edit the command so that it is one command on one long line of text. Then copy that and paste into a terminal window.

I suggest if you want to use this on lots of images, you write a script loop over each image and then call this command.

One issue is that your images are jpg. This means they are compressed and the solid areas of colors are not going to be perfectly uniform. Thus the -fuzz 10% may need to be changed for each image. That is why I suggested that your input images be non-lossy-compressed formats such as png or tif. Then the -fuzz should not be needed. However, you cannot just change jpg to png, since the loss is already there now.
Thanks for the super explanation :-D

The crop issue was resolved as I upgraded the Image Magick to version 7.0.5-10 Q16.

I will surely incorporate your suggestion of working with png/tiff images.

Thanks a lot again.

Cheers,
Ashish
ashishawadekar
Posts: 5
Joined: 2017-06-06T07:49:46-07:00
Authentication code: 1151

Re: [HELP] Trim the dimensions around diagram

Post by ashishawadekar »

Bonzo wrote: 2017-06-06T12:33:18-07:00 One thing I would do to neaten up the image would be to add a couple of pixel wide black border.
Yes for sure.

It will turn image like the original one.

Thanks.

Cheers,
Ashish
Post Reply