gimp python-如何更改像素颜色?

gimp python-如何更改像素颜色?,python,gimp,gimpfu,Python,Gimp,Gimpfu,我想用Gimp Python加载2个jpg图像。然后,图片应该逐像素进行比较。如果图片2中的像素具有特定的rgb值,则图片1中的像素应为彩色。在此之前,应进行用户输入,输入起始值 我不确定gimp python是否能做到这一切 我主要搜索命令: -加载图片 -用户输入 -加载像素RGB值 -更改像素RGB值 -保存图像 非常感谢 我第一次尝试c++,但处理图片并不是那么容易。我的老师建议我放弃。示意图应如下所示: #include <iostream> using namespace

我想用Gimp Python加载2个jpg图像。然后,图片应该逐像素进行比较。如果图片2中的像素具有特定的rgb值,则图片1中的像素应为彩色。在此之前,应进行用户输入,输入起始值

我不确定gimp python是否能做到这一切

我主要搜索命令:
-加载图片
-用户输入
-加载像素RGB值
-更改像素RGB值
-保存图像

非常感谢

我第一次尝试c++,但处理图片并不是那么容易。我的老师建议我放弃。示意图应如下所示:

#include <iostream>
using namespace std;

int main()
{
unsigned long long int startPixelBreite;
unsigned long long int startPixelHoehe;
int prozent;


//EDIT: Load pic 1
//EDIT: load pic 2


//startpixel bestimmen durch usereingabe
    cout << "Startpixel Höhe" << endl;
    cin >> startPixelBreite;
    cout << "Startpixel Höhe" << endl;
    cin >> startPixelHoehe;

//breite + Höhe von bild 1 auslesen
        endpixelBreite = startPixelBreite + bildBreite1
        endpixelHoehe = startPixelHoehe + bildHoehe1

    //ANFANG: Schleife für pixelzeile
        aktuellerPixelX = 0;
        //ANFANG schleife für pixel pixelreihe


             //pixelfarbebild1 einlesen
                /*
                pixelfarbebild1[0]  = //rot
                pixelfarbebild1[1]  = //grün
                pixelfarbebild1[2]  = //blau
                */

            //pixelfarbebild2 einlesen
                /*
                pixelfarbebild2[0]  = //rot
                pixelfarbebild2[1]  = //grün
                pixelfarbebild2[2]  = //blau
                */

            if (aktuellerPixelX > startPixelBreite & aktuellerPixelX< endpixelBreite)
            {
                if pixelfarbe[0] = 102 & pixelfarbe[1] = 102 & pixelfabre [2] = 102 //grau
                {
                    prozent = 60
                    neuerpixel[0] = (pixelfarbebild1[0]*prozent-100*pixelfarbebild1[0]+100*pixelfarbebild2[0])/prozent  //rot
                    neuerpixel[1] = (pixelfarbebild1[1]*prozent-100*pixelfarbebild1[1]+100*pixelfarbebild2[1])/prozent  //grün
                    neuerpixel[2] = (pixelfarbebild1[2]*prozent-100*pixelfarbebild1[2]+100*pixelfarbebild2[2])/prozent  //blau
                }
                else if pixelfarbe[0] = 237 & pixelfarbe[1] = 136 & pixelfabre [2] = 196 //pink
                {
                    prozent = 70
                    neuerpixel[0] = (pixelfarbebild1[0]*prozent-100*pixelfarbebild1[0]+100*pixelfarbebild2[0])/prozent  //rot
                    neuerpixel[1] = (pixelfarbebild1[1]*prozent-100*pixelfarbebild1[1]+100*pixelfarbebild2[1])/prozent  //grün
                    neuerpixel[2] = (pixelfarbebild1[2]*prozent-100*pixelfarbebild1[2]+100*pixelfarbebild2[2])/prozent  //blau
                }
                else if pixelfarbe[0] = 175 & pixelfarbe[1] = 167 & pixelfabre [2] = 172 //hellgrau
                {
                    prozent = 67
                    neuerpixel[0] = (pixelfarbebild1[0]*prozent-100*pixelfarbebild1[0]+100*pixelfarbebild2[0])/prozent  //rot
                    neuerpixel[1] = (pixelfarbebild1[1]*prozent-100*pixelfarbebild1[1]+100*pixelfarbebild2[1])/prozent  //grün
                    neuerpixel[2] = (pixelfarbebild1[2]*prozent-100*pixelfarbebild1[2]+100*pixelfarbebild2[2])/prozent  //blau
                }
                else
                {
                    neuerpixel[0] = pixelfarbebild2[0]  //rot
                    neuerpixel[1] = pixelfarbebild2[1]  //grün
                    neuerpixel[2] = pixelfarbebild2[2]  //blau
                }

                //pixel in bild schreiben
            }
            else{
                neuerpixel[0] = pixelfarbebild2[0]  //rot
                neuerpixel[1] = pixelfarbebild2[1]  //grün
                neuerpixel[2] = pixelfarbebild2[2]  //blau
            }

            aktuellerPixelX++;

        //ENDE schleife für pixel pixelreihe

    //ENDE: Schleife für pixelzeile
//ausgabe

}
#包括
使用名称空间std;
int main()
{
无符号long-long-int-startPixelBreite;
无符号long long int startPixelHoehe;
int prozent;
//编辑:加载图片1
//编辑:加载pic2
//startpixel Bestinmen durch usereingabe
库特-斯塔特普克苏莱特;
无法启动Pixelhoehe;
//breite+Höhe von bild 1 auslesen
endpixelBreite=startPixelBreite+bildBreite1
endpixelHoehe=startPixelHoehe+bildHoehe1
//安芳:Schleife für pixelzeile
aktuellerPixelX=0;
//安防施莱夫für像素像素
//PixelFarbebilld1-einlesen
/*
PixelFarbebilld1[0]=//rot
PixelFarbebilld1[1]=//grün
PixelFarbebilld1[2]=//blau
*/
//pixelfarbebild2-einlesen
/*
pixelfarbebild2[0]=//rot
PixelFarbebilld2[1]=//grün
PixelFarbebilld2[2]=//blau
*/
if(aktuellerPixelX>startPixelBreite和aktuellerPixelX
在GIMP中,检查
GIMP\u可绘制\u获取\u像素
GIMP\u可绘制\u设置\u像素
过程。 您可以在Filters->python-Fu->Console中找到python插件中可以使用的所有过程

一个非常基本的代码,只是给你一个想法,可能是:

color_to_edit = (102, 102, 102) #or whatever color you wish to edit, this is an RGB value without alpha channel
new_color = (200, 200, 200) #the new color
for i in range(x_size):
    for j in range(y_size):
        num_channels, pixel = pdb.gimp_drawable_get_pixel(drawable, i, j)
        if all([p == q for p, q in zip(pixel, color_to_edit)]):
            pdb.gimp_drawable_set_pixel(drawable, i, j, 3, new_color)
pdb.gimp_displays_flush() #this will update the image.
这里
drawable
应该是带有图像的gimp层。

您可以/应该使用“像素区域”向python数组导出/导入层。我的一个脚本使用它将Gimp的像素传输到numpy数组中:

# Returns NP array (N,bpp) (single vector of triplets)
def channelData(layer):
    w,h=layer.width,layer.height
    region=layer.get_pixel_rgn(0, 0, w,h)
    pixChars=region[:,:]
    bpp=region.bpp
    return np.frombuffer(pixChars,dtype=np.uint8).reshape(w,h,bpp)
这是GIMP2.8代码,可能需要一些更改以支持2.10中更高的位深度

相反的方向:

def createResultLayer(image,name,result):
    rlBytes=np.uint8(result).tobytes();
    rl=gimp.Layer(image,name,image.width,image.height,
                  image.active_layer.type,100,NORMAL_MODE)
    region=rl.get_pixel_rgn(0, 0, rl.width,rl.height,True)
    region[:,:]=rlBytes
    image.add_layer(rl,0)
    gimp.displays_flush()
当然,您可以放弃numpy部分,但是如果您可以将问题表示为全局数组操作,那么事情可能会非常快。在Windows上(或在Linux上使用flatpak版本),必须将numpy添加到Gimp使用的Python运行时中。请参阅以获取一些提示。您可以在这里找到完整的脚本,它也可以用作如何获取图像和图层的示例


有关特定于Python的API文档,请参阅。

一个完全不同的答案,它避免了从Gimp中提取像素

所以我们有“图像”层(你给像素上色)和“地图”层(你给给定的颜色指明哪些像素应该上色)。然后:

  • 对“地图”层上的颜色进行颜色选择(
    gimp\u by\u color\u select(地图层…)
  • 使用该选择(在图像中是全局的,因此适用于任何层),为图像层上的像素上色(桶填充选择,用于统一颜色:
    gimp\u edit\u bucket\u fill(imagelayer,…)
  • 对于更复杂的配色方案,可以绘制第三层,将其插入“图像”层下方,删除选定的像素以使其可见,然后在其上合并图像层
所有这些都是通过Gimp操作符完成的,而且速度非常快。它也可用作手动程序,所以您可以在