Advertisement
darkwiiplayer

Magic.lua

Oct 24th, 2018
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.21 KB | None | 0 0
  1. local imlib2 = require "imlib2"
  2.  
  3. math.randomseed(os.time())
  4.  
  5. local white, black =
  6.     imlib2.image.load("first.png"),
  7.     imlib2.image.load("second.jpg")
  8.  
  9. local width, height =
  10.     math.min(white:get_width(), black:get_width()),
  11.     math.min(white:get_height(), black:get_height())
  12.  
  13. for _, image in ipairs{white, black} do
  14.     local cwidth, cheight =
  15.         image:get_width(),
  16.         image:get_height()
  17.     image:crop(
  18.         (cwidth  - width ) / 2,
  19.         (cheight - height) / 2,
  20.         width,
  21.         height
  22.     )
  23. end
  24.  
  25. local _color = imlib2.color.new(0, 0, 0)
  26. local function color(val, alpha)
  27.     _color.red = val
  28.     _color.green = val
  29.     _color.blue = val
  30.     _color.alpha = alpha
  31.     return _color
  32. end
  33.  
  34. local target = imlib2.image.new(width, height)
  35.  
  36.  
  37. target:set_has_alpha(true)
  38. for y=1,height do
  39.     for x=1,width do
  40.         local px_white, px_black =
  41.             white:get_pixel(x, y),
  42.             black:get_pixel(x, y)
  43.  
  44.         local l_w = (px_white.red + px_white.green + px_white.blue) / 3 + 1
  45.         local l_b = (px_black.red + px_black.green + px_black.blue) / 3 + 1
  46.  
  47.         -- Normalize
  48.         l_w = (l_w) / 2 + 128
  49.         l_b = (l_b)
  50.  
  51.         local a_l = (l_w + l_b) / 2
  52.         local d_l = l_w - l_b
  53.  
  54.         target:draw_pixel(x, y, color(a_l - 1, (255 - d_l) / 2 - 1))
  55.     end
  56. end
  57. target:save('result.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement