Problem convert tif to pdf " @ blob.c/OpenBlob/2480."

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
rogeriobrasiliense

Problem convert tif to pdf " @ blob.c/OpenBlob/2480."

Post by rogeriobrasiliense »

Hello,

I use LINUX system and something really strange occured.

When I used the command to convert a tif image into a pdf image (notice that has spaces in the names of images):

Code: Select all

$ convert "/tmp/image 16832.tif" "/tmp/image 16833.tif" "/tmp/test alpha.pdf"
thats ok!

But when I used variables (cause I wanna put this code into a script):

Code: Select all

$ image="\"/tmp/image 16832.tif\" \"/tmp/image 16833.tif\""
$ path="\"/tmp/test alpha.pdf\""
$ convert $image $path
returned the error:

Code: Select all

convert: unable to open image `"/tmp/image':No such file or directory @ blob.c/OpenBlob/2480.
convert: unable to open image `16832.tif"': No such file or directory @ blob.c/OpenBlob/2480.
convert: unable to open image `"/tmp/image': No such file or directory @ blob.c/OpenBlob/2480.
convert: unable to open image `16833.tif"': No such file or directory @ blob.c/OpenBlob/2480.
convert: unable to open image `"/tmp/test': No such file or directory @ blob.c/OpenBlob/2480.
convert: missing an image filename `alpha.pdf"' @ convert.c/ConvertImageCommand/2838.
So I try to see the mistake:

Code: Select all

$ echo convert $image $path
convert "/tmp/image 16832.tif" "/tmp/image 16833.tif" "/tmp/test alpha.pdf"
(equal to the first code ????)

I don't know where is the problem, I already used similar code with another commands in LINUX, then I tried to search what's wrong and I just find windows and mac similiar problems, but they don't help me.


Thanks for reading !
Last edited by rogeriobrasiliense on 2010-06-08T05:41:40-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: Problem convert tif to pdf " @ blob.c/OpenBlob/2480."

Post by fmw42 »

$ image="\"/tmp/image 16832.tif\" \"/tmp/image 16833.tif\""
I don't think you can nest quotes with spaces in filenames that way, but I am not an expert

I think it still resolves to the same as

image="tmp/image 16832.tif tmp/image 16833.tif"

which still has the spaces.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Problem convert tif to pdf " @ blob.c/OpenBlob/2480."

Post by snibgo »

Spaces in file or directory names are always messy.

The following works for me, in Ubuntu (bash):

Code: Select all

NAME3=$'three\x20logo.png'
NAME4=$'four logo.png'
convert logo: "$NAME3"
convert logo: "$NAME4"
creating files named "three logo.png" and "four logo.png".
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: Problem convert tif to pdf " @ blob.c/OpenBlob/2480."

Post by fmw42 »

snibgo:

I think he wants to have multiple inputs to convert and generate a multipage pdf, rather than multiple converts. Of course one can do a loop over each input and successively convert and add them to a multipage pdf.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Problem convert tif to pdf " @ blob.c/OpenBlob/2480."

Post by el_supremo »

I figured out a way to do it.

Code: Select all

echo convert "$image" "$path" >tmp
chmod tmp 755
bash tmp
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Problem convert tif to pdf " @ blob.c/OpenBlob/2480."

Post by snibgo »

I like that batch file.

Multiple inputs, each with a space, expressed as a variable? How about with arrays:

Code: Select all

#!/bin/bash
NAME3=$'three\x20log.png'
NAME4=$'four log.png'
convert logo: "$NAME3"
convert logo: -transparent White -flip "$NAME4"

NAMEARR[0]=$'three\x20log.png'
NAMEARR[1]=$'four log.png'

# The next three converts do the same thing:
#
convert -verbose "${NAMEARR[@]}" -composite out.png
convert -verbose logo: \( logo: -transparent White -flip \) -composite out2.png
convert "three log.png" "four log.png" -composite out3.png
snibgo's IM pages: im.snibgo.com
rogeriobrasiliense

Re: Problem convert tif to pdf " @ blob.c/OpenBlob/2480."

Post by rogeriobrasiliense »

fmw42
image="tmp/image 16832.tif tmp/image 16833.tif" -> don't work

snibgo
fmw42 has reason, I wanna multiple inputs to convert and generate a multipage pdf. I can make a loop and solve quickly this problem, but I wanna to understand why this strange thing is happen.

el_supremo
your code works, but I would like to understand why? I mean, whats the difference between put the code direct into bash or make this through script? Or whats the difference between put the string direct or make this through variables (like i did before) ?

snibgo
I am not used to deal with arrays cause it doesn't works in some shells, but its a idea, thanks!!
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Problem convert tif to pdf " @ blob.c/OpenBlob/2480."

Post by el_supremo »

your code works, but I would like to understand why?
The shell only makes one pass through the command line breaking it up into arguments.

Code: Select all

convert "/tmp/image 16832.tif" "/tmp/image 16833.tif" "/tmp/test alpha.pdf"
When it reads the above command line, it sees the first quote and then stores everything up to the second quote as the first argument. Then it sees the next quote and so on, resulting in three arguments to the convert command.

Code: Select all

$ image="\"/tmp/image 16832.tif\" \"/tmp/image 16833.tif\""
$ path="\"/tmp/test alpha.pdf\""
$ convert $image $path
When it processes the third line above, the line does not contain any quotes. So the shell does the variable substitution and breaks the resulting string into arguments at each space which is why you get six error messages from convert. If you had used this command:

Code: Select all

$ convert "$image" "$path"
The interpretation of the output argument would now be what you intended because $path would have been expanded into one string. But the $image would also have been passed as one string so that convert would see only two arguments, the $image string and the $path string.
The basic problem here is that the $image string contains two "types" of space. The first and third spaces are just parts of a filename, but the second one is intended to be interpreted as an argument separator. But the shell doesn't "know" that and in order for it to interpret this properly it would have to make two passes through the argument string. The only way to make that happen (that I know of) is to run it through another shell.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Problem convert tif to pdf " @ blob.c/OpenBlob/2480."

Post by anthony »

rogeriobrasiliense wrote:But when I used variables (cause I wanna put this code into a script):

Code: Select all

$ image="\"/tmp/image 16832.tif\" \"/tmp/image 16833.tif\""
$ path="\"/tmp/test alpha.pdf\""
$ convert $image $path
You have hit the first big hurdle people have when creating shell scripts. And yes this is all shell scripting and NOTHING to do with ImageMagick itself.

I have been creating complex shell script programming for 22 years now, so I should know a thing or two about this :-)

First of all as the variable was NOT in quotes, the shell expanded the string, then parsed the command breaking it up into spaces.

The next thing you try is to quote the variable. Something you should generally always do in a shell script.

Code: Select all

convert "$image" "$path"
However that results in

Code: Select all

  convert "\"/tmp/image 16832.tif\" \"/tmp/image 16833.tif\""  "\"/tmp/test alpha.pdf\""
Which is only TWO arguments not three quoted arguments.


The TRICK, as some people have attempted to do by 'echo'ing the command to a script and executing that, is to have the shell 'parse' the command twice! That is done using a special shell command called "eval"

Code: Select all

  eval "convert $image $path"
That will be parsed once by shell substituting all the strings, to produce...

Code: Select all

convert "/tmp/image 16832.tif"  "/tmp/image 16833.tif"  "/tmp/test alpha.pdf"
Which will then me parsed into arguments and execututed JUST AS YOU WANT.

The more typical example of "eval" is 'variable indirection'

Code: Select all

a="THE DATA"
b=a
eval echo \"\$$b\"
The eval parses the echo command, removing the backslash from the first $ sign, and substituting the contents of $b, so that it then generates

Code: Select all

echo "$a"
Which when executed outputs...

Code: Select all

THE DATA
I have even used this to generate a very very very complex data structure (hash tables and trees) in shell scripts!!!! It does work, though the backslashes of eval's of eval's can become a real nightmare!

I suggest you google for many BASH FAQ guides that are around, which will probably explain "eval" much more fully.

Fred I am sure you can find and reproduce a long list of them :-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Problem convert tif to pdf " @ blob.c/OpenBlob/2480."

Post by anthony »

el_supremo wrote:I figured out a way to do it.

Code: Select all

echo convert "$image" "$path" >tmp
chmod tmp 755
bash tmp
Pete
Whcih is basically an 'eval' but using a sub-shell (executing a temporary file). This is something that is common practice in DOS scripting.

Unfortunately you can not easy assign to variables within a sub-shell like this, and any variable assignment will be in the sub-shell and not in the main shell. That is variable assignments get lost.

You get a similar problem when piping data to a compound shell command like 'for' 'while' or 'if'. And it can be a real problem to figure out a solutions
Eval does not have that problem...

Code: Select all

a="DATA"
b=a
eval c=\"\$$b\"
echo "$c"
the result is that c was assigned the string "DATA". Try doing that in a executed tmp and you will fail (though their are solutions for that too).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply