clip trim, fill and merge all

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
grburgos
Posts: 9
Joined: 2018-10-03T05:38:33-07:00
Authentication code: 1152

clip trim, fill and merge all

Post by grburgos »

Hello everyone,
I'm new to ImageMagick and I'm trying to understand how can I do a multi-step process on the same image. I have version 6.7.8 on Centos.

Files
https://www.dropbox.com/s/9d5nz19pxkh24 ... .tiff?dl=0 -> origin

https://www.dropbox.com/s/c05ynjdunw8vx ... t.jpg?dl=0 -> end result

Operation
I need to convert an image that currently is 800x600 to a 500x500. The image has a clipping path, so I need to grab the clipping path and resize it but keep its aspect ratio. Then create a white background that has the size of 500x500 and merge them together.

The end result
Should have an image that is 500x500 with a scaled clipping path.

What I have
convert origin.tiff -clip -trim -resize 500x500 output.jpg -> Grabs the clipping path only and resizes it.
convert origin.tiff -fill white -colorize 100% -resize 500x500! output.jpg -> Creates a white background with the right size.

Issue
Don't know how to put these two steps together so the clipping path is on top of the white background.
Last edited by grburgos on 2018-10-03T11:11:11-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: clip trim, fill and merge all

Post by fmw42 »

It would help us to understand and try to reproduce your problem if you can post your images to some free hosting service such as dropbox.com that won't change the image formats. Then put the URLs here.
grburgos
Posts: 9
Joined: 2018-10-03T05:38:33-07:00
Authentication code: 1152

Re: clip trim, fill and merge all

Post by grburgos »

Thanks for the advise, I modified the original post so it includes the files.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: clip trim, fill and merge all

Post by fmw42 »

Try this. It seems to achieve the desired result though not the commands you used. I am using IM 6.9.10.12 Q16 Mac OSX

Code: Select all

convert origin.tiff -trim +repage -resize 500x500 -background white -gravity north -extent 500x500 result.jpg
grburgos
Posts: 9
Joined: 2018-10-03T05:38:33-07:00
Authentication code: 1152

Re: clip trim, fill and merge all

Post by grburgos »

Spot on! Thank you for the help!
grburgos
Posts: 9
Joined: 2018-10-03T05:38:33-07:00
Authentication code: 1152

Re: clip trim, fill and merge all

Post by grburgos »

Question is there a way to keep the clipping path?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: clip trim, fill and merge all

Post by fmw42 »

When I check the result.jpg with identify -verbose, it shows a clip path.

Code: Select all

Image: result.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Mime type: image/jpeg
  Class: DirectClass
  Geometry: 500x500+0+0
  Resolution: 300x300
  Print size: 1.66667x1.66667
  Units: PixelsPerInch
  Colorspace: sRGB
  Type: TrueColor
  Base type: Undefined
  Endianess: Undefined
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Pixels: 250000
    Red:
      min: 175  (0.686275)
      max: 255 (1)
      mean: 243.225 (0.953823)
      standard deviation: 11.2216 (0.0440062)
      kurtosis: -0.257072
      skewness: -0.586589
      entropy: 0.654232
    Green:
      min: 143  (0.560784)
      max: 255 (1)
      mean: 224.134 (0.878955)
      standard deviation: 27.7198 (0.108705)
      kurtosis: -1.67072
      skewness: -0.0576208
      entropy: 0.689648
    Blue:
      min: 78  (0.305882)
      max: 255 (1)
      mean: 193.145 (0.75743)
      standard deviation: 59.441 (0.233102)
      kurtosis: -1.79935
      skewness: -0.155112
      entropy: 0.683984
  Image statistics:
    Overall:
      min: 78  (0.305882)
      max: 255 (1)
      mean: 220.168 (0.863403)
      standard deviation: 32.7941 (0.128604)
      kurtosis: 0.319342
      skewness: -1.24854
      entropy: 0.675955
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: white
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 500x500+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: JPEG
  Quality: 92
  Orientation: Undefined
  Properties:
    comment: 037600234757
    date:create: 2018-10-03T11:58:52-07:00
    date:modify: 2018-10-03T11:58:52-07:00
    jpeg:colorspace: 2
    jpeg:sampling-factor: 1x1,1x1,1x1
    signature: 004a953979f75954e9a722f0c0be0a97fc9d4ef30f5af0bfdf9b4d3b2fc09845
  Clipping path: 
<?xml version="1.0" encoding="iso-8859-1"?>
<svg xmlns="http://www.w3.org/2000/svg" width="500" height="500">
<g>
<path fill-rule="evenodd" style="fill:#000000;stroke:#000000;stroke-width:0;stroke-antialiasing:false" d="
M 275.695 440.314
C 275.695 440.314 293.944 443.364 316.012 446.015
C 334.685 449.198 339.46 448.402 346.674 447.739
C 359.406 448.137 363.119 449.596 376.7 444.955
C 393.887 440.579 395.797 429.308 396.964 425.33
C 398.131 421.352 400.572 413.396 405.664 394.965
C 410.12 380.379 409.802 377.727 410.226 375.871
C 410.651 374.015 414.258 361.683 414.258 361.683
C 414.258 361.683 414.576 360.622 417.971 350.545
C 421.367 340.467 427.348 322.832 431.698 303.605
C 436.048 284.378 431.658 263.03 424.231 243.405
C 416.804 223.78 404.815 210.123 404.815 210.123
C 404.815 210.123 403.648 205.614 402.269 185.725
C 400.253 162.52 395.691 147.096 379.034 123.228
C 362.377 99.3602 332.669 80.9719 305.933 72.0878
C 279.196 63.2038 238.349 57.1508 194.106 68.8195
C 152.669 79.7483 124.896 103.401 109.714 128.622
C 94.883 151.179 94.0462 169.402 91.1921 189.57
C 79.097 196.996 43.8655 233.315 50.5528 296.472
C 57.2401 359.629 140.904 404.233 140.904 404.233
C 140.904 404.233 207.237 423.579 218.717 425.827
C 230.198 428.074 271.876 440.181 275.695 440.314 Z
"/>
</g>
</svg>

  Profiles:
    Profile-8bim: 616 bytes
  Artifacts:
    filename: result.jpg
    verbose: true
  Tainted: False
  Filesize: 38917B
  Number pixels: 250000
  User time: 0.010u
  Elapsed time: 0:01.000
  Version: ImageMagick 6.9.10-12 Q16 x86_64 2018-09-25 https://imagemagick.org
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: clip trim, fill and merge all

Post by fmw42 »

If I try your first command on Imagemagick 6.9.10.12 Q16 Mac OSX. I get an error.

Code: Select all

convert origin.tiff -clip -trim -resize 500x500 output.jpg
convert: image size differs `origin.tiff' @ error/image.c/SetImageClipMask/2655.

Even this simpler command gives the same error:

Code: Select all

convert origin.tiff -clip tmp.jpg
So I wonder if you clip path is proper for that image?
grburgos
Posts: 9
Joined: 2018-10-03T05:38:33-07:00
Authentication code: 1152

Re: clip trim, fill and merge all

Post by grburgos »

When I run:

Code: Select all

 convert origin.tiff -clip -trim -resize 500x500 output.tiff
It executes and includes the clipping path. However the clipping path appears not centered.

https://www.dropbox.com/s/0oluusbiosf21 ... .tiff?dl=0

*I just noticed that the end result image has to be tiff not jpg.

When I run the code that you suggested, it does include the clipping path but the path is not aligned with the cup image.

Code: Select all

convert origin.tiff -clip -trim +repage -resize 500x500 -background white -gravity north -extent 500x500 result.tiff
https://www.dropbox.com/s/jzdm6sbkye0tc ... .tiff?dl=0
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: clip trim, fill and merge all

Post by snibgo »

grburgos wrote:convert origin.tiff -clip -trim -resize 500x500 output.jpg -> Grabs the clipping path only and resizes it.
No, that's not what your command does.

Your command reads the file, then "-clip" applies the clipping path to subsequent operations. This means the operations will only apply to pixels within the clip region, which is useful for painting out background or foreground. But you trim and resize, and I don't understand how you can trim an image and just affect the pixels within the clip region. That doesn't make sense to me.

If you want to trim and resize the image and its clip path:

Code: Select all

convert origin.tiff -trim +repage -resize 500x500 out.tiff
If you want to see the clip path, of origin.tif or out.tiff:

Code: Select all

convert origin.tiff -alpha opaque -clip -alpha transparent +clip t.tiff
Note that this makes all pixels opaque, then just those within the clip path transparent.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: clip trim, fill and merge all

Post by fmw42 »

snibgo wrote: 2018-10-03T13:34:30-07:00
If you want to trim and resize the image and its clip path:

Code: Select all

convert origin.tiff -trim +repage -resize 500x500 out.tiff
In your case the -resize 500x500 keeps the aspect ratio of the trimmed image, which in this case is not square. Thus you will get an image in width of 500, but in height < 500. So in my command above, I extended it to full 500x500 by using -extent as follows, since you wanted a 500x500 output. That takes the place of your second command which creates a 500x500 white image. You would then have to overlay your trimmed image onto the white background. The -extent takes care of that in one command line.

Code: Select all

convert origin.tiff -trim +repage -resize 500x500 -background white -gravity north -extent 500x500 result.jpg
grburgos
Posts: 9
Joined: 2018-10-03T05:38:33-07:00
Authentication code: 1152

Re: clip trim, fill and merge all

Post by grburgos »

The script that you provided works very well. The only piece that I'm unsure of is the clipping path on the result image. For some reason it is smaller than what it was before.

I use Gimp 2.8 to open the file and look at the clipping path called "Path 1". You can notice that after I run the command

Code: Select all

convert origin.tiff -trim +repage -resize 500x500 -background white -gravity north -extent 500x500 result.tiff
the clipping path is smaller than the image of the product.
https://www.dropbox.com/s/lwlq4dwbihzxd ... h.png?dl=0

If I use the plain -resize command, the clipping path does align with the image.
https://www.dropbox.com/s/xddloukntpmxm ... h.png?dl=0

Code: Select all

convert origin.tiff  -resize 500x500 result.tiff
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: clip trim, fill and merge all

Post by snibgo »

I agree, there seems to be a bug that "-trim" and "-extent" don't seem to modify the clip path correctly. Please report this in the bugs forum.
snibgo's IM pages: im.snibgo.com
grburgos
Posts: 9
Joined: 2018-10-03T05:38:33-07:00
Authentication code: 1152

Re: clip trim, fill and merge all

Post by grburgos »

Can anyone suggest a different approach in getting the same end result image?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: clip trim, fill and merge all

Post by snibgo »

You want to edit an image (trim, crop, resize etc) and have those operations applied to the embedded clip path.

Sadly, IM won't do that. I don't think Gimp can either.

Gimp and IM can export the clip path as SVG. Gimp (but not IM) can import an SVG as a clip path. Inkscape can edit (eg crop and resize) an SVG.

Inkscape and Gimp are essentially interactive, but can be driven in batch. So a possible route is: export the clip path as SVG, edit with Inkscape, import into Gimp, and save the resulting raster image with embedded clip path.
snibgo's IM pages: im.snibgo.com
Post Reply