Trim and Fuzz problems

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
KlutzyBubbles
Posts: 4
Joined: 2018-09-01T05:03:36-07:00
Authentication code: 1152

Trim and Fuzz problems

Post by KlutzyBubbles »

Hi,

If i have formatted this thread wrong, please let me know. I am very new to the forum and don't want to be breaking any rules or flooding the wrong categories.

I have a question in regards to trimming an image, the images in question are located here: https://imgur.com/a/nVbigxf

I want to trim the transparent areas, and then after that i add a border and set the background to white (which is working fine) But for some reason, when i trim with or without the fuzz option (i have tried 1, 5, 10, 25 and 50% for the fuzz).

The resulting images are here:
https://imgur.com/a/mR1EFUY

The exact command i used was

Code: Select all

magick mogrify -trim -fuzz 10% -path edited *.png
ImageMagick Version details:

OS: Windows 10 64x
PRogram Type: CMD
IM Version: 7.0.8-10 Q16 x64
Visual C++: 180040629

Image Details:
Size: 2.2mb
Dim: 2532x2651
Bit Depth: 32
Type: PNG

Other steps i tried:
- Converting image type before trimming
- Filling transparency before filling
- Resizing to a smaller file size before trimming


All the steps i have taken end up with the same result of the bottom transparency still not trimmed in the processed image. I am looking for a solution or alternative to achieve the same result
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trim and Fuzz problems

Post by snibgo »

KlutzyBubbles wrote:... the images in question are located here: https://imgur.com/a/nVbigxf
That pages shows two JPG images, or a zip file that contains two JPG images. JPG does not record transparency.

Your command shows input from PNG, so I guess you uploaded the wrong files, or imgur "helpfully" converted them to JPG.
snibgo's IM pages: im.snibgo.com
KlutzyBubbles
Posts: 4
Joined: 2018-09-01T05:03:36-07:00
Authentication code: 1152

Re: Trim and Fuzz problems

Post by KlutzyBubbles »

snibgo wrote: 2018-09-01T05:36:25-07:00
KlutzyBubbles wrote:... the images in question are located here: https://imgur.com/a/nVbigxf
That pages shows two JPG images, or a zip file that contains two JPG images. JPG does not record transparency.

Your command shows input from PNG, so I guess you uploaded the wrong files, or imgur "helpfully" converted them to JPG.
Imgur 'helpfully' converted them.

here is the input and output file in their unconverted form

https://drive.google.com/drive/folders/ ... sp=sharing
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trim and Fuzz problems

Post by snibgo »

I assume you want to process before.png.

That image has a few problems:

1. It has a page offset. ("magick -identify before.png" shows this.) "+repage" takes care of that.

2. The blocks go right up to the image edge, so "-trim" doesn't know what to trim. Adding a suitable border takes care of that.

3. The background is transparent white, instead of the more usual transparent black. If you want this in your result, add "-background White -alpha Background" at the end of the command.

4. The alpha channel is noisy. ("magick +repage -alpha extract a.png" shows this.) Most of the background has alpha=0, fully transparent. But some pixels are eg 3.13% near bottom-right, which is almost transparent, in the alpha channel. "-fuzz 5%" takes care of that.

So we have:

Code: Select all

magick before.png +repage -bordercolor None -border 10 -fuzz 5% -trim +repage -background White -alpha Background out.png
The result looks good to me.

Edit to add: you had "-trim -fuzz 10%" As you want the fuzz to affect the trim, you should put fuzz before trim, not after.
snibgo's IM pages: im.snibgo.com
KlutzyBubbles
Posts: 4
Joined: 2018-09-01T05:03:36-07:00
Authentication code: 1152

Re: Trim and Fuzz problems

Post by KlutzyBubbles »

snibgo wrote: 2018-09-01T06:37:20-07:00 I assume you want to process before.png.

That image has a few problems:

1. It has a page offset. ("magick -identify before.png" shows this.) "+repage" takes care of that.

2. The blocks go right up to the image edge, so "-trim" doesn't know what to trim. Adding a suitable border takes care of that.

3. The background is transparent white, instead of the more usual transparent black. If you want this in your result, add "-background White -alpha Background" at the end of the command.

4. The alpha channel is noisy. ("magick +repage -alpha extract a.png" shows this.) Most of the background has alpha=0, fully transparent. But some pixels are eg 3.13% near bottom-right, which is almost transparent, in the alpha channel. "-fuzz 5%" takes care of that.

So we have:

Code: Select all

magick before.png +repage -bordercolor None -border 10 -fuzz 5% -trim +repage -background White -alpha Background out.png
The result looks good to me.

Edit to add: you had "-trim -fuzz 10%" As you want the fuzz to affect the trim, you should put fuzz before trim, not after.
Thank you for the informative reply, and while the command does work when done on individual files, when i try to use it either with mogrify or windows 'forfiles' to make a batch process it just doesn't work

I dont know if this is an issue with the way mogrify or forfiles loads the files or if there is another variable interrupting this process.

The reason i need batch processing is because it would be a waste of my time to do this for thousands of photos. To be clear i am using forfiles and mogrify on 2 png's taken in the exact same format on the exact same device, not hundreds of photos yet because i just want to make sure the functions work before waiting for hours for files to process.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trim and Fuzz problems

Post by snibgo »

KlutzyBubbles wrote:... when i try to use it either with mogrify or windows 'forfiles' to make a batch process it just doesn't work
If you showed the commands you tried, and what errors they gave, perhaps we could say why they don't work for you.

They work for me:

Code: Select all

magick mogrify +repage -bordercolor None -border 10 -fuzz 5% -trim +repage -background White -alpha Background *.png

forfiles /M *.png /C "%IMG7%magick -verbose @file +repage -bordercolor None -border 10 -fuzz 5% -trim +repage -background White -alpha Background @file"
snibgo's IM pages: im.snibgo.com
KlutzyBubbles
Posts: 4
Joined: 2018-09-01T05:03:36-07:00
Authentication code: 1152

Re: Trim and Fuzz problems

Post by KlutzyBubbles »

snibgo wrote: 2018-09-02T04:22:48-07:00
KlutzyBubbles wrote:... when i try to use it either with mogrify or windows 'forfiles' to make a batch process it just doesn't work
If you showed the commands you tried, and what errors they gave, perhaps we could say why they don't work for you.

They work for me:

Code: Select all

magick mogrify +repage -bordercolor None -border 10 -fuzz 5% -trim +repage -background White -alpha Background *.png

forfiles /M *.png /C "%IMG7%magick -verbose @file +repage -bordercolor None -border 10 -fuzz 5% -trim +repage -background White -alpha Background @file"
Sorry, i should have specified that there isn't any error, the photo just doesn't get edited. I think i'm going to mark this thread as solved as the convert command works, and the mogrify command works (most of the time, ill have to figure out the times it doesn't work).
Post Reply