开发者

Converting Unix ImageMagick script to Windows batch file

开发者 https://www.devze.com 2023-03-13 19:08 出处:网络
I have the following bash script (.sh) that\'s working just fine in Unix. I\'d like to convert into a batch (.bat) file for use with ImageMagick for Windows but I\'m having a bit of trouble with the c

I have the following bash script (.sh) that's working just fine in Unix. I'd like to convert into a batch (.bat) file for use with ImageMagick for Windows but I'm having a bit of trouble with the conversion.

Original GIT repo for the Unix script(s):

https://github.com/AndreyChernyh/imagemagick-filters

ImageMagick version for Windows:

http://www.imagemagick.org/download/binaries/ImageMagick-6.7.0-Q16-windows.zip

earlybird.sh:

#!/bin/bash

SOURCE=$1

# Prepare
rm -f result.jpg source_copy.jpg mask.png
convert $SOURCE -fill rgba\(251,243,213,1.0\) -colorize 100% fill.jpg
convert assets/earlybird/mask.png -resize `identify -format "%wx%h" $SOURCE`\! mask.png
cp $SOURCE source_copy.jpg

# Run
convert fill.jpg $SOURCE -compose multiply -gravity center -composite - |
convert - -modulate 101,68,100 - |
convert - -gamma 1.19 - |
convert - -channel red +level 10.5%,100% - |
convert - -modulate 105,120 - |
convert - -modulate 100,83,100 - |
convert - -level 0%,92%,0.92 - |
convert mask.png - -compose ColorBurn -composite - |
convert mask.png - -compose Multiply -composite result.jpg

# Cleanup
rm -f source_copy.jpg fill.jpg

# Open
open $SOURCE result.jpg

This is what I have so far, but it's not working as intended as I'm not sure what the batch file equivalent of multiple command line arguments are: e.g., "\" or "|" or whether it's even possible. I read somewhere that the equivalent is a caret "^" but again, I'm not 100% sure how to implement it properly.

@echo off

set SOURCE=%1

del result.jpg
del source_copy.jpg
del mask.png

convert.exe %SOURCE% -fill rgba(251,243,213,1.0\) -colorize 100% fill.jpg
convert.exe assets\earlybird\mask.png -resize 1024x768 mask.png

copy %SOURCE% sourc开发者_如何学Ce_copy.jpg

convert.exe fill.jpg %SOURCE% -compose multiply -gravity center -composite - ^
convert.exe - -modulate 101,68,100 - ^
convert.exe - -gamma 1.19 - ^
convert.exe - -channel red +level 10.5%,100% - ^
convert.exe - -modulate 105,120 - ^
convert.exe - -modulate 100,83,100 - ^
convert.exe - -level 0%,92%,0.92 - ^
convert.exe mask.png - -compose ColorBurn -composite - ^
convert.exe mask.png - -compose Multiply -composite result.jpg

del source_copy.jpg
del fill.jpg

WORKING CODE:

This is what I've done after applying @Gabe's answer. No errors and it's working flawlessly!

@echo off

set SOURCE=%1

del result.jpg
del source_copy.jpg
del mask.png

conv %SOURCE% -fill rgba(251,243,213,1.0) -colorize 100%% fill.jpg
conv assets\earlybird\mask.png -resize 604x453 mask.png

copy %SOURCE% source_copy.jpg

conv fill.jpg %SOURCE% -compose multiply -gravity center -composite - |^
conv - -modulate 101,68,100 - |^
conv - -gamma 1.19 - |^
conv - -channel red +level 10.5%%,100%% - |^
conv - -modulate 105,120 - |^
conv - -modulate 100,83,100 - |^
conv - -level 0%%,92%%,0.92 - |^
conv mask.png - -compose ColorBurn -composite - |^
conv mask.png - -compose Multiply -composite result.jpg

del source_copy.jpg
del fill.jpg


You're pretty close. The big problems I see are that you have an extra \ in rgba(...), you need to escape your % signs (like %%), and you need to put | before the line continuation characters (like |^). Try this:

@echo off

set SOURCE=%1

del result.jpg source_copy.jpg mask.png

convert.exe %SOURCE% -fill rgba(251,243,213,1.0) -colorize 100%% fill.jpg
convert.exe assets\earlybird\mask.png -resize 1024x768 mask.png

copy %SOURCE% source_copy.jpg

convert.exe fill.jpg %SOURCE% -compose multiply -gravity center -composite - |^
convert.exe - -modulate 101,68,100 - |^
convert.exe - -gamma 1.19 - ^
convert.exe - -channel red +level 10.5%%,100%% - |^
convert.exe - -modulate 105,120 - |^
convert.exe - -modulate 100,83,100 - |^
convert.exe - -level 0%%,92%%,0.92 - |^
convert.exe mask.png - -compose ColorBurn -composite - |^
convert.exe mask.png - -compose Multiply -composite result.jpg

del source_copy.jpg fill.jpg


Your original unix shell script is taking the output from the first convert cmd and passing from one invocation to the next thru stdout/stdin, as denoted by the '-' chars.

It is a convention of unix programs, that an argument of '-' means "don't look for a filename to process, just read from the std input."

Also, speaking of conventions, DOS programs use '/o' (option) format for controlling program. Try convert.exe /? to see if you get a usage msg or help ELSE try convert.exe --help (and other unix variants) just so you understand which is the correct option syntax to use.

More importantly, note there is a std program in DOS called convert.exe, so be sure your PATH is set to goto the ImageMagick for Windows DIR first. You'll be able to tell when you do the convert.exe /? and you get options that you don't recognize.


The original script uses the pipe character to pass stdout of one program to the stdin of the next program. That is one impressive chain!

My experience with cmd shell is really old at this point, but at one time, a DOS pipe could only hold 64K. You may be running into this problem. If these are long running commands, you try running a separate window and do a dir %TEMP to see the pipe files being created. With so many pipes, DOS may have a naming conflict or some other issue.

So, do the simplest case, convert .. | convert ... (doing just part of the work to see if the pipes work at all), and build up from there, adding 1 more | convert .. as you go. If it blows up after a while then you know exactly where it is happening.

Does the first command with (...) in it work?

The escape char \ should work OK, and I see you have transposed your path chars from / to \, so that is good.

And % has a special meaning for .bat files, to print variable names like %f%, so as long usage for % is only with numbers you may be OK. For a var like %f%, the only way I could find to break the translation was to quote like %f\%, which winds up giving you the un-useful output of %f\%.

Not perfect, but neither is .bat programming.

I hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消