如何有效地更改许多图像上的颜色? [英] How to efficiently change colors on a lot of images?

查看:101
本文介绍了如何有效地更改许多图像上的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个庞大的图像数据集,如下所示:

I have a huge dataset of images like this:

我想更改这些颜色.所有白色应保持白色,所有紫色应变为白色,其他所有应变为黑色.所需的输出如下所示:

I would like to change the colors on these. All white should stay white, all purple should turn white and everything else should turn black. The desired output would look like this:

我已经在下面创建了代码,它可以满足我的要求,但是花很长时间才能浏览我拥有的图片数量.有另一种更快的方法吗?

I've made the code underneath and it is doing what I want, but it takes way to long to go through the amount of pictures I have. Is there another and faster way of doing this?

path = r"C:path"
for f in os.listdir(path):
f_name = (os.path.join(path,f))
if f_name.endswith(".png"):
    im = Image.open(f_name)
    fn, fext = os.path.splitext(f_name)
    print (fn)
    im =im.convert("RGBA")
    for x in range(im.size[0]):
        for y in range(im.size[1]):
            if im.getpixel((x, y)) == (255, 255, 255, 255):
                im.putpixel((x, y),(255, 255, 255,255))
            elif im.getpixel((x, y)) == (128, 64, 128, 255):
                im.putpixel((x, y),(255, 255, 255,255))
            else:
                im.putpixel((x, y),(0, 0, 0,255))

    im.show()

推荐答案

您的图像似乎代表了分割或带标签的类,因此已经灰白了,通常少于256个类.这样,每个像素只是一个标签(或类号),实际颜色会在256个元素的表格(即调色板)中查找.

Your images seem to be palettised as they represent segmentations, or labelled classes and there are typically fewer than 256 classes. As such, each pixel is just a label (or class number) and the actual colours are looked up in a 256-element table, i.e. the palette.

如果您不熟悉托盘化图像,请在这里.

Have a look here if you are unfamiliar with palletised images.

因此,您不需要遍历所有1200万像素,而只需遍历只有256个元素的调色板...

So, you don't need to iterate over all 12 million pixels, you can instead just iterate over the palette which is only 256 elements long...

#!/usr/bin/env python3

import sys
import numpy as np
from PIL import Image

# Load image
im = Image.open('image.png')

# Check it is palettised as expected
if im.mode != 'P':
    sys.exit("ERROR: Was expecting a palettised image")

# Get palette and make into Numpy array of 256 entries of 3 RGB colours
palette = np.array(im.getpalette(),dtype=np.uint8).reshape((256,3))

# Name our colours for readability
purple = [128,64,128]
white  = [255,255,255]
black  = [0,0,0]

# Go through palette, setting purple to white
palette[np.all(palette==purple, axis=-1)] = white

# Go through palette, setting anything not white to black
palette[~np.all(palette==white, axis=-1)] = black

# Apply our modified palette and save
im.putpalette(palette.ravel().tolist())
im.save('result.png')

这需要290毫秒,包括加载和保存图像.

That takes 290ms including loading and saving the image.

如果要处理成千上万张图像,并且使用的操作系统还不错,则可以使用 GNU Parallel .更改以上代码以接受作为图像名称的命令行参数,并将其另存为recolour.py,然后使用:

If you have many thousands of images to do, and you are on a decent OS, you can use GNU Parallel. Change the above code to accept a command-line parameter which is the name of the image, and save it as recolour.py then use:

parallel ./recolour.py {} ::: *.png

它将使您CPU上的所有CPU内核都处于繁忙状态,直到全部处理完毕.

It will keep all CPU cores on your CPU busy till they are all processed.

关键字:图像处理,Python,Numpy,PIL,Pillow,调色板,getpalette,putpalette,类,分类,标签,标签,标签图像.

Keywords: Image processing, Python, Numpy, PIL, Pillow, palette, getpalette, putpalette, classes, classification, label, labels, labelled image.

这篇关于如何有效地更改许多图像上的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆