-- ====================================================================== -- -- lua5.1 ___toDPI_InDir_v3.lua - FPS - StudioNight -- -- 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. -- -- +--------------------- -- | + B -- | matte -> | -> border | -> padx | dstX -- -- -- 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 | | -- || | | -- |+==============================+| -- | | -- +-----------------------------------------+ -- -- +--------------------- -- | + B -- | matte -> | -> border | -> padx | dstX -- --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 -- -- ====================================================================== -- -- lua5.1 toDPI.lua -- -- doug rice copyright September 2011 -- -- description - produces DPI pictures -- pictures in ./ are prepared to DIP standard of 1400 wide x 1050 high with border -- make input directories --os.execute("mkdir .\\img_ip") --os.execute("mkdir .\\img_ip\\rc") -- rotate clockwise --os.execute("mkdir .\\img_ip\\rcc") -- rotate counter clockwise -- make input directories -- os.execute("mkdir .\\img_ip") os.execute("mkdir .\\ok") -- process os.execute("mkdir .\\rc") -- rotate clockwise os.execute("mkdir .\\rcc") -- rotate counter clockwise os.execute("mkdir .\\junk") -- junk - excluded from processing -- pictures output to -- ./img_op -- and -- ./img_op/sm -- -- require "gd" -- -- Sec 1.0 Functions -- -- capture pwd and extract folder name. -- fi = io.popen( "pwd") fi = io.popen( "cd") 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 .. "" filenamelt = "./img_op/fb/" .. C .. "" font = "C:/WINDOWS/Fonts/Vera.ttf" font = "C:/WINDOWS/Fonts/ARIAL.ttf" title = "Felixstowe Photgraphic Society Studio night, 6th Feb 2014, Doug Rice (C)" title = "" -- -- -- -- -- 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 ] -- -- spilt file name into useful chunks - path,name,ext -- A,B,C = string.match( fn,"(.-)([^/]*)(\.JPG)") if ( rotate == "c" ) then 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 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 ]==] matte = 10 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( 128,128,128, 0 ) -- bcolor = black color = white red = im:colorAllocateAlpha( 255,0,0, 0 ) green = im:colorAllocateAlpha( 0,255,0, 0 ) red2 = im:colorAllocateAlpha( 200,200, 200, 0 ) bcolor = im:colorAllocateAlpha( 0,128,128, 0 ) bcolor = im:colorAllocateAlpha( 0,64,64, 0 ) bcolor = im:colorAllocateAlpha( 128,128,128, 0 ) pgl_bl = im:colorAllocateAlpha( 255,20,20, 0 ) pgl_yl = im:colorAllocateAlpha( 200,200,0, 0 ) bcolor = im:colorAllocateAlpha( 8,8,8, 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 matte = 25 border = 5 imgX,imgY = im_org:sizeXY() wantY = wantHeight -matte*2-border*2-1 wantX = wantWidth -matte*2-border*2-1 toWidth = wantWidth toHeight = wantHeight imgX,imgY = im_org:sizeXY() imgWidth = wantX imgHeight = wantY padX = 0 padY = 0 padR = 2 --print( " prepareDPI... ") if ( ( wantY/wantX ) < ( imgY/imgX ) ) then -- more Portrait than Landscape imgWidth = math.floor( wantY*imgX/imgY ) padX = math.floor( (wantX - imgWidth) / 2 ) bb = green else -- more Landscape than Portrait imgHeight =math.floor( wantX*imgY/imgX ) padY = math.floor( ( wantY - imgHeight ) /2 ) bb = red end bb = white -- more portrate --[==[ +--------------------- | + B | matte -> | -> border | -> padx | dstX The pictures are placed from top left corner ]==] -- background canvas im:filledRectangle( matte , matte, wantWidth - matte -1, wantHeight - matte -1, bcolor ) -- im:rectangle( matte , matte, wantWidth - matte -1, wantHeight - matte -1, white ) -- dstImage:copyResampled(srcImage, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH) im:copyResampled(im_org, matte+border+padX+1, matte+border+padY+1, 0,0, imgWidth-1, imgHeight-1, imgX, imgY) -- gdImage:rectangle(x1, y1, x2, y2, color) -- rectangle is used to draw a rectangle with the two corners (upper left first, then lower right) specified, using the color index specified. --[==[ padR = 2 im:rectangle( matte+border +padX-padR, matte+border+padY-padR, matte+border+padX+padR+imgWidth, matte+border+padY+padR+imgHeight, pgl_yl ) padR = 4 im:rectangle( matte+border +padX-padR, matte+border+padY-padR, matte+border+padX+padR+imgWidth, matte+border+padY+padR+imgHeight, pgl_bl ) padR = 6 im:rectangle( matte+border +padX-padR, matte+border+padY-padR, matte+border+padX+padR+imgWidth, matte+border+padY+padR+imgHeight, pgl_bl ) ]==] --[==[ gd.copyResampled(dstImage, srcImage, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH) dstImage:copyResampled(srcImage, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH) gd.copyResampled is used to copy a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity. The X and Y dimensions of the original region and the destination region can vary, resulting in stretching or shrinking of the region as appropriate. (For a simpler version of this method which does not deal with resizing, see gd.copy. For a version which does not interpolate pixel values, see gd.copyResized). Pixel values are only interpolated if the destination image is a truecolor image. Otherwise, gd.copyResized is automatically invoked. The dstImage argument is the destination image to which the region will be copied. The srcImage argument is the source image from which the region is copied. The dstX and dstY arguments specify the point in the destination image to which the region will be copied. The srcX and srcY arguments specify the upper left corner of the region in the source image. The dstW and dstH arguments specify the width and height of the destination region. The srcW and srcH arguments specify the width and height of the source region and can differ from the destination size, allowing a region to be scaled during the copying process. When you copy a region from one location in an image to another location in the same image, gd.copyResampled will perform as expected unless the regions overlap, in which case the result is unpredictable. If this presents a problem, create a scratch image in which to keep intermediate results. ]==] -- print watermark / title -- gdImage:stringFT(color, fontname, ptsize, angle, x, y, string) llx, lly, lrx, lry, urx, ury, ulx, uly = gd.stringFT(nil, white, font, size, math.rad(ang), border +2 , border + 2 + size * 1.4 , title ) textWidth = (urx-llx) -- only works for horizontal text im:stringFT( white, font, size, math.rad(ang), (wantWidth-textWidth)/2 +2 , border + 2 + size * 1.4 , title ) im:stringFT( black, font, size, math.rad(ang), (wantWidth-textWidth)/2 +4 , border + 4 + size * 1.4 , title ) -- 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)") -- A,B,C = string.match( fn,"(.-)([^/]*)(\.jpg)") print( "debug",fn,A,B,C) --[==[ if (B ) then else A,B,C = string.match( fn,"(.-)([^/]*)(\.jpg)") end ]==] A,B,C = string.match( fn,"(.-)([^/]*)(\.jpg)") if ( B ) then fn = picsA[ offset+1 ] im:jpeg( filename .. "_" .. B .. ".jpg" , 85 ) end A,B,C = string.match( fn,"(.-)([^/]*)(\.jpg)") 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. function rotate_rc() print( " rotate_rc()" ) picsA = {} fi = io.popen( "dir /b /on .\\rc\\*.jpg ") for i in fi:lines() do picsA[ #picsA+1 ] = "./rc/" .. i end cnt = #picsA steps = cnt while( steps > 0 ) do steps = steps - 1 print( "filename:") print( filename ) prepareDPI( "", 10, 0 , filename, steps , 1400, 1050 , "c") prepareDPI( "", 10, 0 , filenamelt, steps , 640, 480 , "c") prepareDPI( "", 10, 0 , filenamefb, steps , 900, 600 , "c") end end function rotate_rcc() print( " rotate_rcc()" ) picsA = {} fi = io.popen( "dir /b /on .\\rcc\\*.jpg ") for i in fi:lines() do picsA[ #picsA+1 ] = "./rcc/" .. i end cnt = #picsA 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") prepareDPI( "", 10, 0 , filenamefb, steps , 600, 900 , "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] = "./"..w cnt = cnt + 1 end end picsA = {} -- add images in .\ fi = io.popen( "dir /b /on .\\*.jpg ") for i in fi:lines() do 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 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 picsA[ #picsA+1 ] = "./ok/" .. i end cnt = #picsA 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") prepareDPI( "", 10, 0 , filenamefb, steps , 600, 900 , "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 = "" opStrJs = "" for i in fi:lines() do A,B,C = string.match( i ,"(.-)([^\\]*)(\.jpg)") -- print( A,"-",B,"-", C ) --

opStr = opStr .. "

\n" opStrJs = opStrJs .. "add( \"" .. B .. "\.jpg\" ) \n" end opStrHead = [==[ ]==] opStrTail = [==[ ]==] opStrHeadJs = [==[ ]==] opStrTailJs = [==[ ]==] print( opStrHead .. opStr .. opStrTail ) fo = io.open( "./img_op/index1.htm","w") fo:write( opStrHead .. opStr .. opStrJs ..opStrTail ) fo:close() opStrJs = "\n" fo = io.open( "./img_op/indexSlideShow.htm","w") fo:write( opStrHeadJs .. opStrJs ..opStrTailJs ) 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") os.execute("mkdir .\\img_op\\fb") -- try and create a web page from output to refreash img() -- prepare images rotate_rc() rotate_rcc() main() -- try and create a web page from output. img()