Page 1 of 1

Resize, Change colour and then convert

Posted: 2019-06-28T12:44:51-07:00
by simon369
Hello,

I am struggling working out how to do the following, So was looking if anybody would be able to do it for me.

I need code wrote to allow me to batch process some SVGs I have in the following way.

Firstly I need to colour of these changed to black, Some currently contain different elements that are various colours with transparent backgrounds. I would like these changing so everything that is not a transparent background is changed to black.

I then need these resizes to fit in a background I have and then overlayed ontop of the background. The resizing element is just to ensure they fit on the background. I would then like these saved as a .png under the same name as the original SVG but with the size of the SVG added to the end (or ratio)

The below image should help explain. In this example, I have the wine a little SVG on my computer but the wine glass and hearts where red. I would like it all changed to black like in the picture. Then from this resized to fit nicely in the background and then overlayed over the background and saved as a png. I would need either the size of the SVG or the ratio to be included in the new file name. It doesn't matter if its the size taken from before or after the SVG was resized as its actually just the ratio I need. If its hard to include just the SVG size in the final PNG, Then I am fine if I have 2 lines of code. One which just saves the SVG again but with the size at the end. Then a second one which turns to black and overlays on the background.

I am using a windows computer and downloaded the latest version of Imagemagick but would be happy to download any version required to work with the supplied code.

Thanks,

Simon

Image

Re: Resize, Change colour and then convert

Posted: 2019-06-28T13:31:31-07:00
by fmw42
Is this Paid Consulting? If not, then this will need to be move to the User's forum, since this Consulting forum is for PAID Consulting only.

Either way, you will need to provide input images, the desired output size, the background image and anything else needed to perform the desired work.

What are the colored blocks image? You do not explain. Please be more specific about your requirements.

Re: Resize, Change colour and then convert

Posted: 2019-06-28T14:10:33-07:00
by simon369
Hello,

Yes I am happy for this to be paid consulting as I felt it was too big of a ask for somebody to do it for free and share there knowledge.

Here is a link to some examples

http://www.filedropper.com/example_1

In this file is a background.png and a few SVG's

The background is 600px x 600px and I have various backgrounds but they are all this size.

If the converted SVG could be 400PX on its longest side and then centred 300px horizontally and 240px from the top vertically that would fit every background I have. I had hoped by changing the background filename in the code I could change between backgrounds in different batches.

As you can see in the examples given some of the SVG's e.g. Beemine contain different colours. The colours in the SVG's vary greatly so not able to provide exact colours but was hoping it would be possible to change every colour that isnt the transparent background to black by what ever means possible.

Thanks,

Simon

Re: Resize, Change colour and then convert

Posted: 2019-06-28T15:54:03-07:00
by fmw42
What version of ImageMagick do you need this to work on? Is it IM 6 or IM 7 and what is the exact version.

Re: Resize, Change colour and then convert

Posted: 2019-06-29T01:02:25-07:00
by simon369
Hello

I am using ImageMagick 7.0.8-50 Q16 x64 2019-06-24 but I am happy for it to run on any version I am told to use.

Thanks,

Simon

Re: Resize, Change colour and then convert

Posted: 2019-06-29T11:27:27-07:00
by fmw42
I do not code windows, but here is the basic command in unix syntax. For windows remove \ from before \( and before \) and change end of line \ to ^

Code: Select all

convert background.png \
\( After-every-storm-comes-a-rainbow.svg -alpha off -fuzz 5% -fill black +opaque white -resize 400x400 \) \
-geometry +0+40 -gravity north -compose over -composite result1.png
Image

Code: Select all

convert background.png \
\( Aint-nobody-got-time-for-naps.svg -alpha off -fuzz 5% -fill black +opaque white -resize 400x400 \) \
-geometry +0+40 -gravity north -compose over -composite result2.png
Image

Code: Select all

convert background.png \
\( Beach-squad.svg -alpha off -fuzz 5% -fill black +opaque white -resize 400x400 \) \
-geometry +0+40 -gravity north -compose over -composite result3.png
Image

Code: Select all

convert background.png \
\( Bee-mine.svg -alpha off -fuzz 5% -fill black +opaque white -resize 400x400 \) \
-geometry +0+40 -gravity north -compose over -composite result4.png
Image

Re: Resize, Change colour and then convert

Posted: 2019-06-30T00:18:24-07:00
by simon369
Hi Fred,

Thank you so much for that help, I made a slight change by adding density to improve the quality and also by making it do a transparent background.

I have now tried to do batch processing using the following code:

Code: Select all

for %I in ( *.svg ) do magick convert background2.png ( "%I" -density 300 -alpha off -fuzz 1% -fill black +opaque white -transparent white -resize 700x700 ) ^ -geometry +0+40 -gravity north -compose over -composite .png

It doesn't give any errors but I am struggling when trying to get it to save as the original file name, I did try and implement file name coding from googling but I couldn't get it to work. This is the best I could get but its actually just looping through and overriding each image instead of saving each image as a new one.

Please do let me know how much you want for all this help also.

Thanks,

Simon

Re: Resize, Change colour and then convert

Posted: 2019-06-30T03:39:54-07:00
by snibgo
You might prefer "-density" before reading the SVG instead of after.

For the output filename, you might use %~nI.png See "help for" at the command line.

Re: Resize, Change colour and then convert

Posted: 2019-06-30T08:14:15-07:00
by simon369
Thanks so much for that also,

That worked amazing.

Now my final hurdle is to get the output filename to display the width and height of the SVG part not the final .png. Is this even possible? I have tried the following code which i have put together from searching on this forum for previous similar questions but with no luck.

Code: Select all

for %I in ( *.svg ) do magick convert background2.png ( -density 300 "%I" -alpha off -fuzz 1% -fill black +opaque white -transparent white -set filename:f "nI_%wx%h" -resize 700x700  ) ^ -geometry +0+40 -gravity north -compose over -composite "%[filename:f].png"



I am very grateful for both of your help, If you are not wanting payment for this then please let me know what is a suitable amount for me to donate to imagemagick.

Thanks,

Simon

Re: Resize, Change colour and then convert

Posted: 2019-06-30T08:51:43-07:00
by snibgo
simon369 wrote:... but with no luck.
It shouldn't need luck. It works for me, creating a file named "nI_267_233.png". What does it do for you? Does it create a file, or generate an error message, or what?

I suppose you want "%~nI", not simply "nI".

If your command is in a BAT script, every % needs to be doubled to %%.

I suggest you remove "convert" from "magick convert". Having "convert" in there gives you v6 behaviour instead of v7, which is usually a bad idea.

Re: Resize, Change colour and then convert

Posted: 2019-06-30T09:20:39-07:00
by simon369
I am running it in CMD on windows and all I get is 1 file named "%[filename" even after making your suggested changes. In the CMD console it looks like its working and I can see it running through the loop one after each other with no error.

Here is a screen shot of CMD seeing it run through each one

And this is the code I used

Code: Select all

for %I in ( *.svg ) do magick background2.png ( -density 300 "%I" -alpha off -fuzz 1% -fill black +opaque white -transparent white -set filename:f "%~nI_%wx%h" -resize 700x700  ) ^ -geometry +0+40 -gravity north -compose over -composite "%[filename:f].png"

Image

and here is the original folder with the svgs and background file plus the newly created file from running this

Image

Re: Resize, Change colour and then convert

Posted: 2019-06-30T10:55:49-07:00
by snibgo
Your command contains a stray "^". It is harmless because it is surrounded by spaces, but I suggest you remove it.

The "filename" trick doesn't work within parentheses. Instead, you can set an option to that value, then set filename from the option after the parentheses:

Code: Select all

for %I in ( *.svg ) do magick background2.png ( -density 300 "%I" -alpha off -fuzz 1% -fill black +opaque white -transparent white -set option:fopt "%~nI_%wx%h" -resize 700x700  ) -set filename:f %[fopt] -geometry +0+40 -gravity north -compose over -composite "%[filename:f].png"