Batch convert EPS to cropped JPG's with background color

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
Batsman
Posts: 6
Joined: 2011-03-25T14:16:53-07:00
Authentication code: 8675308

Batch convert EPS to cropped JPG's with background color

Post by Batsman »

Hi all,

I turned to IM for two specific tasks. At this point it's one down, one to go.
I Tried to find the info I need but I'm kinda lost on where to start, the number of options is overwhelming and I hope someone here can point me in the right direction.

Ok, so what's my goal. I have a number of EPS files that have to be converted to JPG as follows:

Original EPS looks like this:
Image
These EPS files have whitespace surrounding the actual image. They also have a clipping path that defines the contours.

Now this is what I would like to achieve:
Image
- The white background is to be replaced by a specific color (#FFFF32 to be exact)
- The image is to be cropped to the minimum image area
- <optional> The image is scaled down to a maximum of 1500x1500 px

Here is a link to an EPS file that has to be converted:
Bonbons (12 MB)

System info
- I'm using the DOS command prompt for now
- ImageMagick version: 6.6.8 Q16
- Ghostscript version: 9.01
- System: Windows XP Pro, SP3

Thanks in advance for the info :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch convert EPS to cropped JPG's with background color

Post by fmw42 »

This seems to work. Your image has transparency, so one has to be careful.

# first line: read image
# second line trim to minimum bounds and removes the larger virtual canvas
# see http://www.imagemagick.org/Usage/crop/#trim
# third line: resize if larger than 1500x1500 -- that is what the > means.
# see http://www.imagemagick.org/script/comma ... p#geometry
# fourth line: flatten the transparency against your yellow color background as jpg does not support transparency.
# see http://www.imagemagick.org/Usage/layers/#flatten

convert Bonbons_2890524_50#1.eps \
-trim +repage \
-resize "1500x1500>" \
-background "#FFFF32" -flatten \
Bonbons_2890524_50#1.jpg

For windows users the line continuation \ is replaced with ^. see http://www.imagemagick.org/Usage/windows/

If you need to do this with multiple images at once, then use mogrify instead of convert. But it is recommended that you create a new directory to hold the results (so in general as a good rule you do not override your originals if the same format). See http://www.imagemagick.org/Usage/basics/#mogrify

mogrify -path /fullpath2/newdirectory -format jpg -trim +repage -resize "1500x1500>" -background "#FFFF32" -flatten *.eps
Last edited by fmw42 on 2011-03-28T17:48:47-07:00, edited 2 times in total.
Batsman
Posts: 6
Joined: 2011-03-25T14:16:53-07:00
Authentication code: 8675308

Re: Batch convert EPS to cropped JPG's with background color

Post by Batsman »

Wow ! That works :)

I only just tapped the last line at the command prompt and the result is exactely what I was after. Thanks a million :)

I'll look into this a little deeper tomorrow, really have to sleep now.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Batch convert EPS to cropped JPG's with background color

Post by fmw42 »

Batsman wrote:Wow ! That works :)

I only just tapped the last line at the command prompt and the result is exactely what I was after. Thanks a million :)

I'll look into this a little deeper tomorrow, really have to sleep now.

The explanation is above the convert command. Look up each reference for each line to get a better understanding. Ask more questions if not clear.
Batsman
Posts: 6
Joined: 2011-03-25T14:16:53-07:00
Authentication code: 8675308

Re: Batch convert EPS to cropped JPG's with background color

Post by Batsman »

After reading up on the commands I'm beginning to see the logic somewhat :)
Thanks for the links to the relevant pages.

I do have an addition though for others who might be using the DOS command prompt or a windows batch file.
I made a batch file that is placed inside the image directory. When I run it all EPS files are converted and saved in a new directory

Say you have the EPS files in a directory, the output will be saved in a new folder inside that directory that is created with the following command line:

Code: Select all

if not exist %cd%\Jpgs_Max1500px mkdir %cd%\Jpgs_Max1500px
In English: If the output directory (Jpgs_Max1500px) does not exist, make it.
In this code %cd% represents the directory in which the EPS files and the batch files are located. Make sure the path to the folder does not contain spaces.

The command for IM is this:

Code: Select all

mogrify ^
-path %cd%\Jpgs_Max1500px ^
-format jpg ^
-trim +repage ^
-resize "1500x1500>" ^
-background "#FFFF32" ^
-flatten ^
*.eps
If all the code above is pasted into a plain text document, and the .txt is renamed to .bat it's ready to do it's job
Batsman
Posts: 6
Joined: 2011-03-25T14:16:53-07:00
Authentication code: 8675308

Re: Batch convert EPS to cropped JPG's with background color

Post by Batsman »

Ok, I do have two additional questions. The first one is of minor importance but the solution would be welcome. The second is about compound clipping paths in the EPS files.

1. When using the following command:
mogrify -path %cd%/Jpgs_Max1500px -format jpg -trim +repage -resize "1500x1500>" -background "#FFFF32" -flatten *.eps
Would it be possible for IM to check whether the result already exists in the target directory ? So it only converts the EPS files that have no corresponding JPG in the output directory ("Jpgs_Max1500px" in this case) ?

2. This is more serious issue about the compound clipping path in the EPS files. I have this sample file of frying pans with a hole in the handle and the contours of these holes are defined by the compound clipping path.

The clipping path in the EPS is this:
Image

The result after the IM command is this:
Image


I did do a test by making my own EPS from a JPG
And this is the result.

This is the EPS:
Image

And this is the result produce by IM:
Image

The result from my own test is great ! But the culprit seems to be inside the frying pan EPS :(
I uploaded both EPS files in a zip that can be downloaded here (27.5 MB)
http://www.megaupload.com/?d=NW8BSGJ8

I hope some into EPS/illustrator can enlighten me on the difference between these two EPS file which yield different results in IM.

Thanks in advance :)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Batch convert EPS to cropped JPG's with background color

Post by anthony »

For removing transparency ('flattening') see [urlhttp://www.imagemagick.org/Usage/masking/#remove]IM Examples, Remove Transparency[/url]

When defining clipping paths you need to reverse the direction of the path for holes. That would probably be why the hole failed for one but not the other.

It may be that the clipping path to clipping mask in IM needs a correction in the 'fill-rule' draw setting. But I have not looked at this.


NOTE that using clipping paths to define transparency of a object that is already drawn on a background color, is not a good way of removing that background color. The key to goo background transparency is handling anti-aliasing pixels correctly.
See Background Removal...
http://www.imagemagick.org/Usage/masking/#bg_remove

The one technique that has been developed to produce perfect results is the two background color method...
http://www.imagemagick.org/Usage/maskin ... background
But that is not possible from a single image!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply