English 中文(简体)
有没有高质量的程序化解决方案可以将不透明背景的jpeg转换为透明背景的png?
原标题:Are there any high-quality, programmatic solutions for converting a jpeg with an opaque background to a png with a transparent background?

这里的常见用例是用户上传带有白色/彩色背景的jpeg徽标。将白色像素切换为透明像素(相当)简单,但这会留下混叠伪影。理想的解决方案实质上是“撤消”混叠(给定已知的背景色)。解决方案至少必须优于ImageMagick的bg_removal脚本(http://imagemagick.org/Usage/scripts/bg_removal)。

最佳回答

正如承诺的那样,这里有一个适用于普通白色的工作解决方案-->;alpha用例。这是在Ubuntu 10.04.1 LTS服务器上运行的,带有标准的GIMP安装(2.6.8)。

from gimpfu import *

def run(input_filepath):
    image = pdb.gimp_file_load(input_filepath, input_filepath)
    image.disable_undo()
    layer = image.active_layer
    if not layer.is_rgb:
        pdb.gimp_image_convert_rgb(image)

    white = gimpcolor.RGB(1.0, 1.0, 1.0, 1.0)
    bg_color = pdb.gimp_image_pick_color(image, layer, 0, 0, True, False, 0)
    if bg_color == white:
        pdb.plug_in_colortoalpha(image, layer, bg_color)
        layer_copy = layer.copy()
        image.add_layer(layer_copy)
        image.merge_visible_layers(CLIP_TO_IMAGE)

    pdb.file_png_save_defaults(image, image.active_layer, input_filepath, input_filepath)

run( %(input_filepath)s )

我使用子流程模块(code_as_string是上面作为字符串的代码,插入了input_filepath)从Python(在Django中)执行这段代码:

gimp_args = (settings.PATH_TO_GIMP, 
     -i , 
     --batch-interpreter=python-fu-eval , 
     -b , code_as_string,
     -b ,  from gimpfu import pdb; pdb.gimp_quit(True) )

environ = os.environ.copy()
environ[ GIMP2_DIRECTORY ] = settings.PATH_TO_GIMP_DIR
p = subprocess.Popen(gimp_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=environ)
rc = p.wait()
if rc:
    logging.error(p.stdout.read())
问题回答

GIMP中的“颜色到Alpha”算法看起来做得很好。来源是GPL,可以在此处。GIMP算法对徽标之类的东西的作用演示如下此处,“颜色到Alpha”的GIMP手册页为此处

看起来通过编程实现这一点最简单的方法是使用GIMP批处理模式





相关问题
Non transparent image in transparent block

logo_area { background-color: #d9e670; filter:alpha(opacity=25); opacity:0.25; } and another div: #logo_image { background: url(../images/logo.png) no-repeat center 50%; } <div id="...

How to represent binary transparency?

Lately I ve been interested in representing uncompressed bitmaps in memory. However, one thing I m not sure how to implement properly is binary transparency. E.g., I start out with something like this:...

Transparent PNG in PictureBox

I am trying to make simple app that allows one to compare image to transparent PNG templates, by dragging the template over picture. For this I need a way to create a PictureBox that will contain the ...

Neither IE7 nor IE8 render PNG transparency

I ve got an element, an image, defined as instrument in css and for the life of me I cannot get it to properly display a png with transparency in IE7 or IE8 -- works fine in Safari and FF. I really ...

Hex colors: Numeric representation for "transparent"?

I am building a web CMS in which the user can choose colours for certain site elements. I would like to convert all colour values to hex to avoid any further formatting hassle ("rgb(x,y,z)" or named ...

PNG transparency in Interface Builder

I m adding an Image View in Interface Builder with a transparent PNG (A logo in the Navigation Bar) but the transparent pixels seems to render as white.. I searched for PNG in Interface Builder but ...

热门标签