CVADRAT    GEOMETRICAL AND MATHEMATICAL GRAPHICS DESIGN

   ORIGINAL WORKS FOR FINE PRINTING AND INTERIOR DECORATION

SPA    ENG   
   
  Search by words   Exact words     Loose words        Search by COLOR
 
  dotOP ART STYLE   dot POP ART STYLE   dot GENERATIVE AND GENETIC ART   dot ORGANIC ART   dotFRACTALS AND ATTRACTORS ART    
  dotCRYSTALS ART   dot RAYTRACES AND OPTICAL ART   dotKALEIDOSCOPIC IMAGES   dotVIRTUAL SCULPTURES   dotMETALLIC ART
  dotPNG GRAPHICS   dotSTARS, GALAXIES AND PLANETS   dotOPTICAL ILLUSIONS   dotTEXTURES   dotVIDEOS AND FILES
  dotHOME  dot WHO IS CVADRAT?  dot HOW TO ORDER?  dot CONTACT   dot BLOG       Bookmark and Share

Weapons of mass production

Let me a not so brief introduction, mainly for newbies. Experts or such can jump to the scripts or download them here
I'm not a code guru or an artist.
I'm more of a craftsman, in the sense that a carpenter, for example, is not interested in knowing the specific gravity of the wood he is using or the exact speed of the drill with which he drills the wood. 
He uses them if they serve for his purposes and moves to the next task.
Over several years creating graphics, I found myself with some difficulty to perform a mass production of them to let me then make a selection based only in my aesthetic judgment.
Because my idea was to generate random versions from an original created in Illustrator, Photoshop, Filter Forge (standalone) and many other programs, I was forced to look for a program tat could work automatically and unattended in the generation of random variations.
Imagemagick was the answer. 
For those who do not know it, it is a small, installable but also portable, extremely efficient and also scriptable application. A program that can be run through scripts can be an advantage or become a nightmare, at least for people like me who is not a programmer.
Imagemagick has a myriad of options that can be set to perform the most unthinkable changes in almost any type of image. I have done nothing more than scratch the surface of its operating capabilities.
But let's go to the task.
Working on Windows the best option for me, has always been to use the command line prompt (cmd.exe). In Windows 7 it still exists, at least in some mutated form that does´nt interest me to investigate further if it works properly. 
It does the job ... And fast, very fast.
Imagemagick accepts commands in DOS (through cmd.exe), but unfortunately most of the documentation of Imagemagick is geared towards Unix and Perl.
I can be more or less familiar with Perl, but it requires a server installed on your computer (or run it in remote mode provided Imagemagick is installed in the server side) and other etceteras.
Making bash scripts is beyond my interest or patience.
On the other hand, it doesn´t seem to be very practical to use the cmd.exe command line frequently. It is much more useful to write a bat file and run it and modify it as necessary.
This adds an extra difficulty because there are certain limitations and precautions when implementing DOS commands in a batch.
Then, and only as an introduction, I leave a couple of batch scripts that I have found very useful when I have had to manage a lot of files. I've used them mainly to handle graphics and previous to a randomization procedure on images.
Let´s suppose we have a number of graphics to which we wish to perform certain mass processing. A first step might be to make an alphabetical listing of them, since we may find it useful later.
I assume that you know what a batch script in MS-DOS is. If not, you must create a text file inside the folder containing the images, rename its extension from .txt to .bat, open it in Edit mode and insert the following:


@echo off 
rem If you want a list with the file´s extension, remove ~n from %%~na. 
for %%a in (*.jpg) do echo %%~na >>list.txt 
echo List creation complete. Closing script... 
ping -n 2 127.0.0.1>NUL 
ping -n 2 127.0.0.1>NUL 
exit 



Double click it and it will generate a text file with the names of the images without the extension. You can make the script adds the extension by following the instructions on the rem line.

Then, it may be necessary to place the images into separate folders, so we can process each image without causing an unmanageable disorder.
This task is not a problem if we have three or four images on which to work.
But what if we have 300 or 400? 
You will need a script that creates individual folders for each image and place these files in their own folder. The script to perform these operations is as follows: 


@echo off 
for /f %%a in ('dir /b *.jpg') do mkdir %%~na & move "%%a" "%%~na" 
echo Folder creation and moving of files complete. Closing script... 
ping -n 2 127.0.0.1>NUL 
ping -n 2 127.0.0.1>NUL 
exit 


When double clicked, this script will parse the containing folder looking for all files with the .jpg extension (you can change to the extension you that suits your needs), create a folder for each, with his name without the extension and move each file to the proper folder. 
Then it will send a message to the console and will close after a brief pause.

The first step has been successfully completed. 
Each image is in its proper folder. Now we may make certain processing operations on each one of them. 
It is at this point that Imagemagick appears. 
We must have it installed and configured on the system path or copied and pasted two files (convert.exe and montage.exe from a previous installation) in each of the folders you have created with the above script or paste them into a folder that contains all the folders with its own image and reference the two files location.
Of course, the first thing to decide is what kind of transformation we want to make to the images. In my case I almost always perform a random transformation of hue, saturation and brightness in a number of 100, for example.
With ImageMagick you can make hundreds if not thousands of diferent changes such as cutting, attaching, add or remove borders, place watermarks, convert color channels, transparencies, etcetera.
Obviously this is completely beyond the scope of this blog and I will confine myself, as I said, to a single type of transformation that will be made at random.
I'll show you a script that, along with ImageMagick does every necessary step: preserve the original file, creates a reduced copy, use that copy to create 100 (or less or more) random variations and shows them to you on a web page with thumbnails you can click and take you to larger versions to check details.
I haven´t developed a script that can operate on a series of folders each containing an image and process them the way I have mentioned.
It is not impossible, but I have the feeling that one could lose some control if an automated process manages 80000 (eighty thousand) images. This is not hypothetical. I have had the chance to process more or less that amount of images more than once.

The randomizer script is as follows:


@echo off 
echo Image randomizer script.
echo .

rem Image format detection.

if exist *.jpg (set ext=".jpg" && goto start) else goto fbmp
:fbmp
if exist *.bmp (set ext=".bmp" && goto start) else goto fpng
:fpng
if exist *.png (set ext=".png" && goto start) else goto fgif
:fgif
if exist *.gif (set ext=".gif" && goto start) else echo No supported files present. && ping -n 2 127.0.0.1>NUL && ping -n 2 127.0.0.1>NUL && exit

rem Type the number of random versions of your image you want.

:start

set /P vernum=How many versions of your image would you like to create?^>
set /A vernum+=0
echo .

rem Create smaller version of the image, strip it of any kind of additional information, move images to own folders and make a smaller version of the stripped image. Modify the -resize 10%% to you needs but ALWAYS keep the double %.

mkdir original
mkdir stripped

for %%a in (*%ext%) do convert "%%a" -strip stripped%ext% && move stripped%ext% stripped && move "%%a" original

convert "stripped\stripped%ext%" -resize 10%% test%ext%

rd stripped /s /q

rem Upper and lower HSB values. Normal values: (b)rightness: 0-100, (s)aturation: 0-100, (h)ue: 0-300.

set maxb=200
set minb=70
set maxs=180
set mins=50
set maxh=295
set minh=5
set counter=0

rem Randomization procedure.

:randomization

set/a rand_1_1= ( 32767 / ( (maxb - minb) +1) ) - 1
set/a rand_1_2= ( 32767 / ( (maxs - mins) +1) ) - 1
set/a rand_1_3= ( 32767 / ( (maxh - minh) +1) ) - 1

set/a rand1= ( %RANDOM% / rand_1_1 ) + minb
set/a rand2= ( %RANDOM% / rand_1_2 ) + mins
set/a rand3= ( %RANDOM% / rand_1_3 ) + minh

rem Conversion to HSB color space, rendering random smaller versions and outputting HSB values to text file.
rem -blur 0.25 is set to prevent alias effect. Can be decreased, increased or removed.
rem Changed order in random HSB values is NOT an error.

convert test%ext% -set option:modulate:colorspace HSB -modulate %rand1%,%rand2%,%rand3% -blur 0.25 "%rand3%,%rand2%,%rand1%%ext%"

echo %rand1%,%rand2%,%rand3% >>imageshsbvalues.txt 
set/a counter= %counter% + 1

if %counter%==%vernum% goto delsmaller
goto randomization

rem Once the randomized smaller versions have been rendered, delete original smaller version.

:delsmaller

del test%ext%

goto selector01

rem First choice between creating contact sheet for displaying in browser and deleting unwanted HSB values from imageshsbvalues text file or directly delete unwanted values from imageshsbvalues text file with no contact sheet generation. 

:selector01

rem A short delay.

ping -n 2 127.0.0.1>NUL

rem A short delay.

ping -n 2 127.0.0.1>NUL

echo .
echo .
echo Choice Nr 1. This option makes a contact sheet and displays it in your browser to select proper HSB values. Type number 1, hit ENTER, edit the imageshsbvalues text file that the script has opened and then come back.
echo .
echo Choice Nr 2. Type number 2, hit ENTER, make your selection from the smaller version images, delete unwanted HSB values in the imageshsbvalues text file the script has opened and then come back.

set choice=
set /p choice=Type the number of your choice.
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto contactsheet
if '%choice%'=='2' goto edithsb

rem Contact sheet creation, moving files to own folder for main folder "cleaning" and display contact sheet in default browser.

:contactsheet

montage -label %%t -size 512x512 *%ext%[120x90] -auto-orient -geometry +5+5 -tile 5x -frame 5 -shadow Contactsheet.htm 

rem A short delay.

ping -n 2 127.0.0.1>NUL

rem A short delay.

ping -n 2 127.0.0.1>NUL

mkdir "contactsheet"

for %%b in (*%ext%) do move "%%b" "contactsheet"

move Contactsheet.htm contactsheet
move Contactsheet.png contactsheet
move Contactsheet_map.shtml contactsheet

cd contactsheet

start Contactsheet.htm

cd..

rem Small delay.

ping -n 2 127.0.0.1>NUL

goto edithsb

rem Edit imageshsbvalues.txt, then save it.

:edithsb

start imageshsbvalues.txt

goto selector02

rem Second choice between rendering image versions at full size or closing the script.

:selector02

echo .
echo .
echo Choice Nr 1. Having deleted the HSB values you do NOT want to render, begin rendering image versions at full size.
echo .
echo Choice Nr 2. Do NOT render and leave script.

set choice=
set /p choice=Type the number of your choice.

if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto render
if '%choice%'=='2' goto end01

rem Rendering full size versions, moving the original file to parent folder and deleting temporary files.

:render

if exist *%ext% (del *%ext%)

cd original

for %%e in (*%ext%) do for /F "tokens=1*" %%f in (..\imageshsbvalues.txt) do convert "%%e" -set option:modulate:colorspace HSB -modulate "%%f" "..\%%~ne_%%f%ext%"

cd..

echo .
echo .

echo Rendering job complete.

echo .
echo .

goto end

:end

rem Moving the original image to the parent folder. Contact sheet folder (if existant) and hsb values text file are preserved for future reference.

cd original

for %%g in (*%ext%) do move "%%g" "..\%%g"

cd..

rd original /s /q 

rem The following two lines of code must be uncommented (remove the "rem") to automatically delete the contactsheet folder and/or the imageshsbvalues.txt

rem if exist contactsheet(rd contactsheet /s /q)

rem del imageshsbvalues.txt

echo .
echo .
echo Closing script...
echo .
echo .

rem Small delay.

ping -n 2 127.0.0.1>NUL

rem Small delay.

ping -n 2 127.0.0.1>NUL

exit

:end01

rem Moving the original image to the parent folder. Contact sheet folder (if existant) and hsb values text file are deleted.

if exist *%ext% (del *%ext%)
del imageshsbvalues.txt

for /f "delims=" %%i in ('dir /b /s /ad') do if "%%~ni" == "contactsheet" rd "%%i" /s /q

cd original

for %%g in (*%ext%) do move "%%g" "..\%%g"

cd..

rd original /s /q

echo .
echo .
echo Closing script...
echo .
echo .

rem Small delay.

ping -n 2 127.0.0.1>NUL

rem Small delay.

ping -n 2 127.0.0.1>NUL

exit

This script can render an unlimited number of full-size versions of the original image. Bear in mind that according to the default image viewer you have installed, you may find that the thumbnails of the final randomized versions are not updated properly and it may happen that you see many images with the same thumbnail. However, if open, you will find they have been properly rendered. 
These images have a random numerical code that is attached to their names. These codes are the HSB values, also present in the file imageshsbvalues.txt. 

Obviously, this script can be modified to perform different randomization procedures of any image by simply altering the upper and lower values of randomization and convert.exe parameters. 
It can also be modified to accept other graphic formats in addition to, or instead of, the four classical it can recognize. 
I must admit that this script must probably have redundancies, bugs and a lot of unnecessary lines, but it WORKS.

I've only found a couple of errors which don´t affect the script´s performance. 
They are actually cmd.exe error messages while processing .png files. I think it is, somehow, due to cmd.exe execution of commands before files are written. This happens when it builds and moves the contact sheet when processing images of this format. 

Finally, I hope these scripts are useful and that readers can point out errors, redundancies or omissions that may be present.