-- ======================================================================
--
-- lua5.1 ___toDPI_InDir.lua
--
-- Description: Script to automate preparation of digital Projected images for Photo competitions
--
-- The originals are not modified.
--
-- description - produces DPI pictures from folders of images of different sizes.
--
--
-- if LUA installed using LUA for windows copy this file into the folder and run once to generate directories.
-- now sort the images too the appropriate folders. Run again and images are prepared for DPI.
--
-- Edit script to suit. See LUA references.
--
-- doug rice copyright September 2011,2013
--
-- See LUA licence.
--
-- description - produces DPI pictures from folders of images of different sizes. It will rotate images stored in rc or rcc.
--
-- pictures in ./ are prepared to DPI standard of 1400 wide x 1050 high with border
--
-- Pictures in folder .\rc are rotated clockwise
-- Pictures in folder .\rcc are rotated counter clockwise
-- Pictures in .\ok are not rotated
-- Pictures in .\junk are ignored
--
-- resized pictures output to:
-- ./img_op - larger format
-- and
-- ./img_op/sm - smaller format
--
-- The parent folder is used as a prefix, and the original filename is tagged on the end.
--
-- You end up with this if the image is more portate than Landscape
-- +-----------------------------------------+
-- | +=============+ |
-- | | | |
-- | | image | |
-- | | | |
-- | | | |
-- | | | |
-- | +=============+ |
-- +-----------------------------------------+
--
-- You end up with this if the image is more landscape than portrate
-- +-----------------------------------------+
-- | |
-- |+==============================+|
-- || | |
-- || image | |
-- || | |
-- |+==============================+|
-- | |
-- +-----------------------------------------+
--
--
--LUA and LUA-GD references:
-- http://www.lua.org/
-- http://ittner.github.io/lua-gd/
-- http://code.google.com/p/luaforwindows/
--
-- Other pages using LUA and LUA-GD:
-- http://www.dougrice.plus.com/Erlangs/adsl_Appendix.htm
-- http://www.dougrice.plus.com/Erlangs/adsl_AppendixThomson.htm
--
-- ======================================================================
require "gd"
--
-- Sec 1.0 Functions
--
-- capture pwd and extract folder name.
fi = io.popen( "pwd")
for i in fi:lines() do
C = string.match( i ,"%C*\\(%C*)$")
print( "===============", dir_str,i, C)
end
picsStr=""
--
-- use Folder name as prefix
--
filename = "./img_op/" .. C .. ""
filenamelt = "./img_op/sm/" .. C .. ""
--font = "./Vera.ttf"
font = "C:/WINDOWS/Fonts/Vera.ttf"
font = "C:/WINDOWS/Fonts/ARIAL.ttf"
title = "Doug Rice, Copyright (C) 2012"
title = "Doug Rice, Copyright (C) 2013"
--
--
--
--
-- Picture is any dimension
--
-- We want to fit an image to a canvas of 1400 : 1050
-- wantHeight,wantWidth
-- allow for border so fit img into toDPI
-- imgX => toX
-- imgY => toY
--
-- if ( wantY / wantX ) > ( imgY / imgX ) then portrait.
-- if ( wantY / wantX ) < ( imgY / imgX ) then landscape.
--
--
function prepareDPI( text, size, ang, filename, offset, wantWidth, wantHeight , rotate )
im = ""
im = 1
im = nil
im_org = ""
im_org = 1
im_org = nil
-- rotate = true
fn = picsA[ offset+1 ]
-- split filename into useful chunks
A,B,C = string.match( fn,"(.-)([^/]*)(\.JPG)")
-- print( text, size, ang, filename, offset, wantWidth, wantHeight , rotate , fn )
-- print( "split filename:", fn, ":-", A,"-",B,"-", C ,"-", rotate )
if ( rotate == "c" ) then
-- rotate the image clockwise
print( "Clockwise ====: ", offset+1, picsA[ offset+1 ] )
im_org_port = gd.createFromJpeg( picsA[ offset+1 ] )
imgX,imgY = im_org_port:sizeXY()
--gd.copyRotated(dstImage, srcImage, dstX, dstY, srcX, srcY, srcW, srcH, ang)
--dstImage:copyRotated(srcImage, dstX, dstY, srcX, srcY, srcW, srcH, ang)
im_org = gd.createTrueColor( imgY, imgX )
gd.copyRotated(im_org, im_org_port, imgY / 2, imgX/2, 0,0, imgX, imgY, -90)
im_org_port=nil
end
if ( rotate == "cc" ) then
-- rotate the image counter clockwise
print( "CounterClockwise ====: ", offset+1, picsA[ offset+1 ] )
im_org_port = gd.createFromJpeg( picsA[ offset+1 ] )
imgX,imgY = im_org_port:sizeXY()
--gd.copyRotated(dstImage, srcImage, dstX, dstY, srcX, srcY, srcW, srcH, ang)
--dstImage:copyRotated(srcImage, dstX, dstY, srcX, srcY, srcW, srcH, ang)
im_org = gd.createTrueColor( imgY, imgX )
gd.copyRotated(im_org, im_org_port, imgY / 2, imgX/2, 0,0, imgX, imgY, 90)
im_org_port=nil
end
if ( rotate == "m" ) then
print( " main ====: ", offset+1, picsA[ offset+1 ] )
im_org = gd.createFromJpeg( picsA[ offset+1 ] )
end
imgX,imgY = im_org:sizeXY()
--[==[
rotate the incoming image
]==]
border = 10
-- wantHeight = 1050
-- wantWidth = 1400
im = gd.createTrueColor( wantWidth , wantHeight )
color = im:colorAllocateAlpha( 0, 0, 0, 64)
color2 = im:colorAllocateAlpha( 255, 255, 255, 64)
white = im:colorAllocateAlpha( 255, 255, 255, 0 )
black = im:colorAllocateAlpha( 0,0,0,0 )
bcolor = im:colorAllocateAlpha( 0,64,64, 0 )
bcolor = im:colorAllocateAlpha( 0,8,8, 0 )
bcolor = black
color = white
red = im:colorAllocateAlpha( 255,0,0, 0 )
red2 = im:colorAllocateAlpha( 200,200, 200, 0 )
-- portrait
--
-- Picture is any dimension
--
-- We want to fit an image to a canvas of 1400 : 1050
-- wantHeight,wantWidth
-- allow for border so fit img into toDPI
-- imgX => toX
-- imgY => toY
--
-- if ( wantY / wantX ) > ( imgY / imgX ) then portrait.
-- if ( wantY / wantX ) < ( imgY / imgX ) then landscape.
--
--
-- work out toX
wantY = wantHeight-1
wantX = wantWidth-1
border = 5
imgX,imgY = im_org:sizeXY()
if ( ( wantY/wantX ) < ( imgY/imgX ) ) then
-- background canvas
im:filledRectangle( 0,0, wantWidth-1, wantHeight-1 , bcolor )
toHeight = wantHeight - border*2 -1
toWidth = wantHeight/imgY*imgX - border*2 -1
-- border
im:filledRectangle( ( wantWidth-toWidth-border*2 ) /2 -1 ,0, (wantWidth+toWidth+border*2)/2-1, wantHeight-1 , red2 )
-- dstImage:copyResampled(srcImage, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH)
im:copyResampled(im_org, ( wantWidth-toWidth ) /2 , border, 0,0, toWidth, toHeight, imgX, imgY)
im:rectangle( ( wantWidth-toWidth-border*2 ) /2 -1 ,0, (wantWidth+toWidth+border*2)/2-1, wantHeight-1 , black )
else
-- background canvas
im:filledRectangle( 0,0, wantWidth-1, wantHeight-1 , bcolor )
toHeight = wantWidth/imgX*imgY - border*2
toWidth = wantWidth - border*2
-- border
im:filledRectangle( 0, ( wantHeight-toHeight-border*2 ) /2 -1 , wantWidth-1,(wantHeight+toHeight +border*2)/2-1, red2 )
-- dstImage:copyResampled(srcImage, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH)
im:copyResampled(im_org, border, (wantHeight-toHeight ) /2 , 0,0, toWidth,toHeight, imgX, imgY)
im:rectangle( 0, ( wantHeight-toHeight-border*2 ) /2 -1 , wantWidth-1, (wantHeight+toHeight +border*2)/2-1, black )
end
im:stringFT( white, font, size, math.rad(ang), border +2 , border + 2 + size * 1.4 , title )
im:stringFT( black, font, size, math.rad(ang), border +4 , border + 4 + size * 1.4 , title )
im:sharpen( 20 )
fn = picsA[ offset+1 ]
-- split the file name into path, name and extension
A,B,C = string.match( fn,"(.-)([^/]*)(\.JPG)")
-- print( "split filename:", fn, ":-", A,"-",B,"-", C )
if ( B ) then
fn = picsA[ offset+1 ]
im:jpeg( filename .. "_" .. B .. ".jpg" , 85 )
end
A,B,C = string.match( fn,"(.-)([^/]*)(\.jpg)")
-- print( "split filename:", fn, ":-", A,"-",B,"-", C )
if ( B ) then
fn = picsA[ offset+1 ]
im:jpeg( filename .. "_" .. B .. ".jpg" , 85 )
end
im = nil
im_org = nil
-- force a garbage collect as it ran out of virtual memory without it.
collectgarbage ( "collect" )
end
-- find files and rotate them according to the folder name.
function rotate_rc()
print( " rotate_rc()" )
picsA = {}
-- find image files in .\rc\*.jpg
fi = io.popen( "dir /b /on .\\rc\\*.JPG ")
for i in fi:lines() do
-- print( "+"..i )
-- print( "#picsA ",picsA )
picsA[ #picsA+1 ] = "./rc/" .. i end
cnt = #picsA
print( "cnt" ,cnt )
steps = cnt
while( steps > 0 ) do
steps = steps - 1
prepareDPI( "", 10, 0 , filename, steps , 1400, 1050 , "c")
prepareDPI( "", 10, 0 , filenamelt, steps , 640, 480 , "c")
end
end
function rotate_rcc()
print( " rotate_rcc()" )
picsA = {}
-- find image files in .\rcc\*.jpg
fi = io.popen( "dir /b /on .\\rcc\\*.JPG ")
for i in fi:lines() do
-- print( "+"..i )
-- print( "#picsA ",picsA )
picsA[ #picsA+1 ] = "./rcc/" .. i
end
cnt = #picsA
print( "cnt" ,cnt )
steps = cnt
while( steps > 0 ) do
steps = steps - 1
prepareDPI( "", 10, 0 , filename, steps , 1400, 1050 , "cc")
prepareDPI( "", 10, 0 , filenamelt, steps , 640, 480 , "cc")
end
end
function main()
cnt = 0
for w in string.gmatch(picsStr, "%C+") do
print("=="..w.."==")
if string.len(w) > 0 then
-- picsA[cnt] = "./APSC/"..w
picsA[cnt] = "./"..w
cnt = cnt + 1
end
end
picsA = {}
-- add images in .\
fi = io.popen( "dir /b /on .\\*.jpg ")
for i in fi:lines() do
-- print( "+"..i )
-- print( "#picsA ",picsA )
picsA[ #picsA+1 ] = "./" .. i
end
-- add images in .\img_ip
fi = io.popen( "dir /b /on .\\img_ip\\*.jpg ")
for i in fi:lines() do
-- print( "+"..i )
-- print( "#picsA ",picsA )
picsA[ #picsA+1 ] = "./img_ip/" .. i
end
-- add images in .\img_ip
fi = io.popen( "dir /b /on .\\ok\\*.jpg ")
for i in fi:lines() do
-- print( "+"..i )
-- print( "#picsA ",picsA )
picsA[ #picsA+1 ] = "./ok/" .. i
end
cnt = #picsA
print( "cnt" ,cnt )
steps = cnt
while( steps > 0 ) do
steps = steps - 1
prepareDPI( "", 10, 0 , filename, steps , 1400, 1050 ,"m" )
prepareDPI( "", 10, 0 , filenamelt, steps , 640, 480 , "m")
end
end
--
-- prepare lines for web page
--
--http://www.dougrice.plus.com/hp/gbbook/simple/codeScraps.htm#T32
function img()
-- find converted files
fi = io.popen( "dir /b /on .\\img_op\\*.jpg ")
opStr = ""
for i in fi:lines() do
A,B,C = string.match( i ,"(.-)([^\\]*)(\.jpg)")
-- print( A,"-",B,"-", C )
--
\n" end opStrHead = [==[
]==] opStrTail = [==[ ]==] print( opStrHead .. opStr .. opStrTail ) fo = io.open( "./img_op/index1.htm","w") fo:write( opStrHead .. opStr .. opStrTail ) fo:close() end --[==[ print( " put pictures into img_ip,") print( " put pictures into img_ip\\rc if they need rotating clockwise,") print( " put pictures into img_ip\\rcc if they need rotating CounterClockwise,") ]==] -- make input directories os.execute("mkdir .\\img_ip") os.execute("mkdir .\\img_ip\\rc") os.execute("mkdir .\\img_ip\\rcc") -- make input directories -- os.execute("mkdir .\\img_ip") os.execute("mkdir .\\ok") os.execute("mkdir .\\rc") os.execute("mkdir .\\rcc") os.execute("mkdir .\\junk") -- make ouput directories print( " pictures are put into img_op" ) os.execute("mkdir .\\img_op") os.execute("mkdir .\\img_op\\sm") -- try and create a web page from output to refreash img() rotate_rc() rotate_rcc() main() -- try and create a web page from output. img() -- -- dir /b *.jpg | lua5.1 pipe.lua -- -- -- for line in io.lines() do -- print( "++++++"..line.."===") -- end --[==[ -- -- simple test to open a file line by line -- -- -- test fi = io.open( "txt.lua") for i in fi:lines() do print( ">",i ) end -- -- dir /b | txt.lua -- for i in io.lines() do print( i ) end -- -- test processing lines from dir command -- fi = io.popen( "dir /b") for i in fi:lines() do print( "+"..i ) end ]==]