Page 1 of 1
Reading and Saving list of Images
Posted: 2010-07-30T04:55:41-07:00
by TSchlaier01
Hi,
I have two problems trying to read a list of images, then save this list after doing a level correction.
My first problem:
Is it normal, that ImageMagick can't read files with long filenames or special characters in the filename?
My second problem:
I can't find a way to save the list of files after the level correction into another folder using exactly the same filenames as they had before.
Thanks for help!!
Re: Reading and Saving list of Images
Posted: 2010-07-30T05:43:48-07:00
by Bonzo
How are you running the code - command line, php, shell script, batch script?
Re: Reading and Saving list of Images
Posted: 2010-07-30T06:35:50-07:00
by TSchlaier01
trying a command in the cmd shell
If it works the command should be executed through a *.BAT file
the problem is that this command schould open a whole folder and save all files to another folder (keeping the filenames)
*.jpg names all saved files like the first one that was opened!
Re: Reading and Saving list of Images
Posted: 2010-07-30T07:26:56-07:00
by Bonzo
I do most things through php but have tried out some batch files:
Code: Select all
::Turn of displaying the code on the screen
@echo off
:: Read all the jpg images from the directory, resize them, add some text and save as a png in a different directory
for %%f in (%1\*.jpg) do ( convert "%%f" -resize 200x200 -pointsize 18 -fill black ^
-gravity northwest -annotate +0+0 "Some text" "%2\%%~nf.png" )
::This batch file was named resize.bat and called using "resize path\to\original\ path\to\save"
I am sure I have tried this but take care as it was a year or so ago. As you can see from the last line this is not a drag and drop folder onto the icon batch file.
Re: Reading and Saving list of Images
Posted: 2010-07-30T08:28:23-07:00
by TSchlaier01
No idea how it schould work...
Can you post a reduced code for "open all *.jpg in actual folder and save it as jpg with same names in folder /new"
Or tell me where the folders have to be and how they have to be named so that your script works please.
when i take the "%1\" at the filopen and the "%2\" at the filesave out of your code Im is trying to do something but always says he can't find the files. But IM is saying it using the right filenames!?!?
This makes no sense. IM seems to read the files but then says he it can't find them??
Doesnt undestand that!
Re: Reading and Saving list of Images
Posted: 2010-07-30T08:50:45-07:00
by Bonzo
As I say I am no batch file expert but this worked:
Code: Select all
:: Open command prompt
:: CD to root cd\
:: Paste and run this modified to your paths forum_test.bat C:\Users\Anthony\Documents\Ballet\ C:\Users\Anthony\Documents\Ballet_png
::Turn of displaying the code on the screen
@echo off
:: Read all the jpg images from the directory and save in a different directory
:: If you are only changing the directory but keeping the name there is another way to do %%~nf.jpg but I can not remember what it is.
for %%f in (%1\*.jpg) do ( convert "%%f" "%2\%%~nf.jpg" )
I have saved the file as forum_test.bat into the C folder
You can see where my original and final folders are
I always have problems with spaces in folder names - the " " around the path should sort it. I wondered if changing (%1\*.jpg) to something like ("%1\*.jpg") would help; but it didn't
Re: Reading and Saving list of Images
Posted: 2010-07-30T09:33:30-07:00
by fmw42
TSchlaier01 wrote:Hi,
I have two problems trying to read a list of images, then save this list after doing a level correction.
My first problem:
Is it normal, that ImageMagick can't read files with long filenames or special characters in the filename?
My second problem:
I can't find a way to save the list of files after the level correction into another folder using exactly the same filenames as they had before.
Thanks for help!!
On my Mac, the only character that really causes problems with IM is a space. And I have had no problem with long names. What special characters are you trying to use. This may be an OS issue (eg. Windows limitations or other Linux systems), but I don't know for sure. Perhaps quotes around the filenames will help. I don't recall but single quotes and double quotes behave differently (one is parsed by the OS and the other by IM, if I recall correctly what Anthony has said before).
To process a bunch of images, you cannot use convert and have multiple output images, unless you script one image at a time. However, mogrify is designed to process a whole directory using wildcards. see
http://www.imagemagick.org/Usage/basics/#mogrify
Re: Reading and Saving list of Images
Posted: 2010-08-06T01:19:52-07:00
by TSchlaier01
It doesn't work with mogrify. Couldn't find something abaout "wildcards" in the documentation!
IM can't write images using *.jpg
I need to set a variable to save the actual filename and use it for writing the finished image.
I think the "for" loop is the best option! But how can I tell IM to save the new image under the same name like the one was opened?
Bonzo says %%f
the official IM documentation says $f
I try it that way:
for %%f in (C:\input\*.jpg) do ( convert "%%f" -linear-stretch 2x80%% "C:\output\%%~nf.jpg" )
What is wrong??
Re: Reading and Saving list of Images
Posted: 2010-08-06T05:56:49-07:00
by Drarakel
TSchlaier01 wrote:I try it that way:
for %%f in (C:\input\*.jpg) do ( convert "%%f" -linear-stretch 2x80%% "C:\output\%%~nf.jpg" )
That should run fine - if the paths are correct and if executed as batch file. (If run directly as commandline, there should be only one '%' instead of two, of course.)
What messages do you get when you execute that?
With mogrify, a similar command could look like that:
mogrify -path C:\output\ [your_operations] C:\input\*.jpg
(Note that a few options don't work with mogrify, but only with convert. It seems that "-linear-stretch" is one of them.)
Re: Reading and Saving list of Images
Posted: 2010-08-06T07:35:59-07:00
by Bonzo
The %%f is the variable contaning the filename from the batch script not Imagemagick.
I try it that way:
for %%f in (C:\input\*.jpg) do ( convert "%%f" -linear-stretch 2x80%% "C:\output\%%~nf.jpg" )
What does this mean ? Have you altered C:\input\ etc. to your file paths as I said or do you have a folder called input and output on your C drive ?
Re: Reading and Saving list of Images
Posted: 2010-08-06T07:56:20-07:00
by TSchlaier01
Ok everything works fine! The problem was an incorrect path.
Im sorry!
Thanks for your help!
Re: Reading and Saving list of Images
Posted: 2012-05-06T12:27:52-07:00
by anothertom
fmw42 says one cannot process a list using convert. Must use mogrify. Not true for me using Windows 8 cmd.
Code: Select all
C:\T>convert *.jpg -quality 82 small\img.jpg
C:\T>cd small
C:\T\small>dir
...
05/06/2012 12:14 PM 642,959 img-0.jpg
05/06/2012 12:14 PM 751,246 img-1.jpg
05/06/2012 12:14 PM 556,339 img-2.jpg
05/06/2012 12:14 PM 858,051 img-3.jpg
05/06/2012 12:14 PM 913,560 img-4.jpg
05/06/2012 12:14 PM 1,088,765 img-5.jpg
05/06/2012 12:14 PM 1,107,113 img-6.jpg
7 File(s) 5,918,033 bytes
2 Dir(s) 922,427,904,000 bytes free
Re: Reading and Saving list of Images
Posted: 2012-05-06T13:03:05-07:00
by fmw42
fmw42 says one cannot process a list using convert. Must use mogrify. Not true for me using Windows 8 cmd.
My mistake for what is described above.
But I don't believe one can specify multiple output names (one for each input image), but one can use a multi-frame format or let the output name be automatically number appended.
Re: Reading and Saving list of Images
Posted: 2012-05-09T20:29:22-07:00
by anthony
fmw42 wrote:fBut I don't believe one can specify multiple output names (one for each input image), but one can use a multi-frame format or let the output name be automatically number appended.
You can even do that....Filename Percent Escapes
See
http://www.imagemagick.org/Usage/files/#save_escapes
But you can not do this to generate multi-image files. You either put them all in one file, with name from first image (-adjoin), or only one image per file with name determined from each image's setting (+adjoin).
This is an area for review in IMv7, including montage, 'one image at a time' style, handling.