简单的图片去背景 - TBY's Blog

简单的图片去背景

TBY posted @ 2012年11月24日 20:59 in Haskell with tags Haskell , 1609 阅读

上传头像时,想弄个透明背景。图片本身背景很单一,但gimp扣了下发现魔棒效果很差,无法与PS相比。

用Haskell随手写了个小东西,根据亮度来降alpha通道值。一开始还想弄个复杂点的阈值函数,调了几下,发现简单的才是最好的。

 

 

module Main where

import System.Environment
import Codec.Picture

main =
  fmap head getArgs >>= readPng >>=
    writePng "out.png" .
    (\(Right (ImageRGBA8 im)) ->
          pixelMap
          (\(PixelRGBA8 r g b al) ->
            let a = (fromIntegral r) +
                    (fromIntegral g) +
                    (fromIntegral b) :: Int
                alpha = if a > ( 3 * 245)
                        then 0
                        else if a > (3*200)
                             then round $
                                  (fromIntegral (a - (3*200)) /
                                   fromIntegral ((3*255)::Int)) * 255
                             else al
            in PixelRGBA8 r g b alpha) im)
Telangana 1st Inter 说:
2022年8月24日 21:28

The Telangana Inter First Year Important Model Question Paper 2023 is happy to be made available for download to all candidates who will be taking the examinations. Please check the Important Model Question Paper 2023 from the sections below. So, in order to achieve the highest possible score on the test, prepare for it. Telangana 1st Inter Question Paper 2023 Additionally, we advise the candidates to download the Subject-specific TS Inter First Year Important Model Question Paper 2023 from the attached direct links at the bottom of the page. For further information, see the sections below. Based on this the Telangana Intermediate I Year Board Important Model Question Paper 2023 is created by the Education Board.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee