-- -- lua5.1 toDPI_panelMartlesham.lua -- -- doug rice copyright March 2018 -- -- -- want a 2x2 panel -- --[==[ A B C A [ Vert ] [ Diag ] B [ Cir ] [ Horiz ] C There are two ways of doing this. Place images as is onto a big enough canvas and then shrinking down to the size required. Shrink down images to the target size. 1) prepare list of images 2) for list of images work out required width and height 3) -- dstImage:copyResampled(srcImage, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH) 4) ]==] require "gd" -- -- Sec 1.0 Functions -- -- capture dir /b *.jpg and pasted into string below. picsStr="" -- -- -- 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. -- -- -- 1920 x 1280 = 1.5 wantWidth = 1400 wantHeight = 1050 wantWidth = 1050 wantHeight = 1050 wantWidth = 1200 wantHeight = 1200 -- wantHeight = math.floor( wantWidth *2/3 )/2 border = 15 -- Harwich has 7 letters -- b + H + b + A + b + R + b + W + b + I + b + C + b + H + b -- b + A + b + A + b + A + b + A + b + A + b + A + b + A + b + A + b -- the width of A and B should be integers which may not add up to 1400. -- So calculate the sum and calculate a residue. -- 8 * b + * a -- Then pad left with half of residue toWidth = math.floor( ( wantWidth-8*border)/7 ) wantWidthCalc = 7 * ( border + toWidth ) + border -- print( "wantWidth" , wantWidth, wantWidthCalc , (wantWidthCalc-wantWidth)/2 ) scale = 5 offsetx = border + ( wantWidth - wantWidthCalc )/2 offsety = border 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 ) grey = im:colorAllocateAlpha( 128, 128, 128, 0 ) bcolor = im:colorAllocateAlpha( 0, 0, 64, 0 ) bcolor = im:colorAllocateAlpha( 64, 0, 0, 0 ) bcolor = im:colorAllocateAlpha( 128, 0, 0, 0 ) bcolor = im:colorAllocateAlpha( 0, 0,128, 0 ) bcolor = im:colorAllocateAlpha( 0, 0, 64, 0 ) bcolor = im:colorAllocateAlpha( 64, 0, 0, 0 ) bcolor = im:colorAllocateAlpha( 128, 128, 0, 0 ) bcolor = im:colorAllocateAlpha( 250, 250, 250, 0 ) --bcolor = im:colorAllocateAlpha( 0,8,8, 0 ) red = im:colorAllocateAlpha( 255,0,0, 0 ) im:filledRectangle( 0,0, wantWidth-1, wantHeight-1 , bcolor ) -- BBBBBBBBBBBBBBB -- B pp B pp B pp B -- BBBBBBBBB function prepareDPI_panel( text, wantWidth, wantHeight, offset ) im_org = "" im_org = 1 im_org = nil print( "==================================================================" ) print( picsA[ offset+1 ] ) -- open picture im_org_port = gd.createFromJpeg( picsA[ offset+1 ] ) imgX,imgY = im_org_port:sizeXY() rotate = ( imgY/imgX ) < 1 if ( rotate ) then print( "rotating....................." ) --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 , imgX, 0,0, imgX, imgY, -90) im_org_port=nil else im_org = gd.createFromJpeg( picsA[ offset+1 ] ) end imgX,imgY = im_org:sizeXY() -- use the gd.colorResolve method to determine the best color available. -- gd.copyResampled(dstImage, srcImage, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH) -- dstImage:copyResampled(srcImage, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH) -- 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 to wantX = wantWidth wantY = wantHeight imgX,imgY = im_org:sizeXY() print( " portrait -- ") -- background canvas -- im:filledRectangle( 0,0, wantWidth-1, wantHeight-1 , bcolor ) -- take the integer values before adding borders */ -- toWidth = math.floor( ( wantX / 7 ) - border) if (offset == 0) then toHeight = math.floor( ( wantX / 7 ) * imgY / imgX ) end -- take the integer values before adding borders */ -- dstImage:copyResampled(srcImage, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH) -- find the middle of the Y align image on middle --[==[ ------------------- + -CL------------------ + ------------------- ]==] offsety = math.floor( (wantY - toHeight)/2 ) im:copyResampled( im_org, math.floor( offsetx ), math.floor( offsety ), 0, 0, toWidth, toHeight, imgX, imgY ) im:rectangle( math.floor( offsetx )-2, math.floor( offsety )-2 , math.floor( offsetx + toWidth )+1, math.floor( offsety + toHeight)+1, grey ) -- step to the next X position. offsetx = offsetx + toWidth + border print( offsetx,offsety, scale, toWidth , toHeight , wantX, wantY , (wantY/2 - toHeight/2) ) im_org = nil -- force a garbage collect as it ran out of virtual memory without it. collectgarbage ( "collect" ) end function prepareDPI_panel2( wantWidth, wantHeight ) im_org = "" im_org = 1 im_org = nil print( "=================================================================" ) -- -- Calculate x,y,width,height -- -- -- p+b+W1+b+W2+b+p=WantWidth -- 2p + 3b + w1 + w2 = wantWidthCalc -- --offsety = math.floor( (wantY - toHeight)/2 ) xb = 10 xW1 = 440 xW2 = 880 xW1 = math.floor( ( wantWidth -xb*3 )/3 ) xW2 = math.floor( ( wantWidth -xb*3 )/3*2 ) xp = ( wantWidth - xW1 - xW2 - 3*xb )/2 yb = 10 yH1 = 650 yH2 = 325 yH1 = math.floor( ( wantHeight -yb*3 )/3*2 ) yH2 = math.floor( ( wantHeight -yb*3 )/3 ) print( xW1,xW2,yH1,yH2 ) yp = ( wantHeight - yH1 - yH2 - 3*yb )/2 -- use the gd.colorResolve method to determine the best color available. -- gd.copyResampled(dstImage, srcImage, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH) -- dstImage:copyResampled(srcImage, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH) -- background canvas im:filledRectangle( 0,0, wantWidth-1, wantHeight-1 , bcolor ) -- take the integer values before adding borders */ -- toWidth = math.floor( ( wantX / 7 ) - border) -- dstImage:copyResampled(srcImage, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH) -- Virtical print( "-- Virtical" ) print( picsA[ 1 ] ) im_org = gd.createFromJpeg( picsA[ 1 ] ) imgX,imgY = im_org:sizeXY() im:copyResampled( im_org, (xp+ xb), (yp+ yb), 0,0 , xW1, yH1, imgX,imgY ) im_org = nil -- force a garbage collect as it ran out of virtual memory without it. collectgarbage ( "collect" ) -- diagonal print( "-- Diagonal" ) im_org = gd.createFromJpeg( picsA[ 2 ] ) imgX,imgY = im_org:sizeXY() im:copyResampled( im_org, (xp+ xb+xW1+xb), (yp+ yb), 0,0 , xW2, yH1, imgX,imgY ) im_org = nil -- force a garbage collect as it ran out of virtual memory without it. collectgarbage ( "collect" ) -- Horizontal print( "-- Horizontal" ) im_org = gd.createFromJpeg( picsA[ 3 ] ) imgX,imgY = im_org:sizeXY() im:copyResampled( im_org, (xp+ xb), (yp+ yb+yH1+yb), 0,0 , xW1, yH2, imgX,imgY ) im_org = nil -- force a garbage collect as it ran out of virtual memory without it. collectgarbage ( "collect" ) -- circular print( "-- Circular" ) im_org = gd.createFromJpeg( picsA[ 4 ] ) imgX,imgY = im_org:sizeXY() im:copyResampled( im_org, (xp+ xb+xW1+xb), (yp+ yb+yH1+yb), 0,0 , xW2, yH2, imgX,imgY ) im_org = nil -- force a garbage collect as it ran out of virtual memory without it. collectgarbage ( "collect" ) end function panel( dir ) 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 = {} fi = io.popen( "dir /b " .. dir .. "\\*.JPG ") for i in fi:lines() do print( "+"..i ) print( "#picsA ",picsA ) picsA[ #picsA+1 ] = dir .. "/" .. i end cnt = #picsA print( "cnt" ,cnt ) prepareDPI_panel2( wantWidth, wantHeight ) im:sharpen( 20 ) print( filename ) filename = dir.."_panel_" im:jpeg( filename .. "_85.JPG", 85 ) end function main() dirA = {} fi = io.popen( "dir /b/ad *") for i in fi:lines() do print( "=:"..i ) dirA[ #dirA+1 ] = i end for y = 1, #dirA ,1 do panel( dirA[y] ) end --[==[ panel("set1") panel("set2") panel("set3") panel("set4") panel("set5") panel("set6") panel("set7") panel("set8") panel("set9") ]==] end -- -- MAIN () -- os.execute("mkdir .\\img_op") main()