Cropping Image via mask, then convert it to jpg, but get uncropped Image

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?".
Locked
liyiming
Posts: 3
Joined: 2020-03-19T08:01:59-07:00
Authentication code: 1152

Cropping Image via mask, then convert it to jpg, but get uncropped Image

Post by liyiming »

Hi, I'am Cropping a png image via mask file, but when I convert the cropped image to jpg, I got an uncropped image.

How can I get the cropped cmyk image?

Thank you for you help.

demo:
// get source image
convert -size 500x500 xc:blue source.png

// generate mask image
convert -size 500x500 xc:black -fill White -draw 'circle 250,250 250,500' mask.png

// crop source image
convert source.png mask.png -alpha Off -compose Copy_Opacity -composite cropped.png

// get CMYK image
convert cropped.png -colorspace CMYK cmyk.jpg

---
Imagemagick Version
Version: ImageMagick 7.0.8-59 Q16 x86_64 2019-08-05 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(3.1)
Delegates (built-in): bzlib freetype heic jng jp2 jpeg lcms ltdl lzma openexr png tiff webp xml zlib

OS: OSX 10.15.3

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

Re: Cropping Image via mask, then convert it to jpg, but get uncropped Image

Post by snibgo »

You haven't cropped anything. You have merely made some pixels transparent, with "-compose Copy_Opacity -composite". JPEG files cannot store transparency.

If you want, you could "-trim" after "-compose Copy_Opacity -composite". But in your example, "-trim" wouldn't remove any pixels.
snibgo's IM pages: im.snibgo.com

liyiming
Posts: 3
Joined: 2020-03-19T08:01:59-07:00
Authentication code: 1152

Re: Cropping Image via mask, then convert it to jpg, but get uncropped Image

Post by liyiming »

snibgo wrote:
2020-03-19T08:25:57-07:00
You haven't cropped anything. You have merely made some pixels transparent, with "-compose Copy_Opacity -composite". JPEG files cannot store transparency.

If you want, you could "-trim" after "-compose Copy_Opacity -composite". But in your example, "-trim" wouldn't remove any pixels.
Thank you for your help.
So what if I need to crop an image of any shape and save it as an image with CMYK colorspace?

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

Re: Cropping Image via mask, then convert it to jpg, but get uncropped Image

Post by snibgo »

Some file formats, such as TIFF, can store CMYK images with transparency.

If you want to crop images, I suggest you use "-crop".
snibgo's IM pages: im.snibgo.com

liyiming
Posts: 3
Joined: 2020-03-19T08:01:59-07:00
Authentication code: 1152

Re: Cropping Image via mask, then convert it to jpg, but get uncropped Image

Post by liyiming »

snibgo wrote:
2020-03-19T09:21:36-07:00
Some file formats, such as TIFF, can store CMYK images with transparency.

If you want to crop images, I suggest you use "-crop".
Ok.
Thank you for your advise!

Locked