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

时间:2018-11-19 18:35:16

标签: python gimp gimpfu

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

我不确定gimp python是否能做到所有这些?

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

非常感谢

我第一次尝试使用c ++,但是处理图片并不是那么容易。我的老师劝我g一口。原理图应如下所示:

#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

}

4 个答案:

答案 0 :(得分:1)

您可以/应该使用“像素区域”将层导出到python数组/从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)

这是Gimp 2.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运行时中。有关某些提示,请参见here。您将在此处找到完整的脚本,该脚本也可以用作如何获取图像和图层的示例。

有关Python特定的API文档,请参见here

答案 1 :(得分:0)

在GIMP中,检查gimp_drawable_get_pixelgimp_drawable_set_pixel过程。 您可以在过滤器-> Python Fu->控制台中找到可以在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应该是图像的金光层。

答案 2 :(得分:0)

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

因此,我们具有“图像”层(在其中为像素着色)和“地图”层(在其中给定的颜色指示应为哪些像素着色)。然后:

  • 对“地图”层(gimp_by_color_select(maplayer,...))上的颜色进行颜色选择
  • 使用该选择(在图像中是全局的,因此适用于任何图层),对图像图层上的像素进行着色(桶填充选择,以得到统一的颜色:gimp_edit_bucket_fill(imagelayer,...))。
  • 对于更复杂的配色方案,可以绘制第3层,将其插入“图像”层下方,然后删除所选像素以使其可见,然后在其上合并图像层。

所有这些操作都是通过Gimp运算符完成的,并且速度非常快。它也可以用作手动过程,因此您可以先在Gimp中尝试它,而无需编写任何代码。

答案 3 :(得分:0)

在matlab /八度中,这非常简单。不需要gimp,并且如果您绝对必须具有c ++代码集成,则可以与C ++很好地集成。例如,说您要在Image1的像素为R:10,G:20,B:30时将Image2像素更改为R:50,G:60,B:70。然后:

% Alle kommentierure im lingula retardata fur demonstraru how rude it looks

Im1 = imread('im1.jpg');   Im2 = imread('im2.jpg'); % readure imagurine
 R1 = Im1(:,:,1);           R2 = Im1(:,:,1);        % selectu 'red'   layeru piripitsi
 G1 = Im1(:,:,2);           G2 = Im1(:,:,2);        % selectu 'green' layeru piripitsi
 B1 = Im1(:,:,3);           B2 = Im1(:,:,3);        % selectu 'blue'  layeru piripitsi

Mask = (R1 == 10) & (G1 == 20) & (B1 == 30); % криеит маск фром чаннелз
R2(Mask) = 50; % πουτ 50 γουεαρ Mask ηζ True!
G2(Mask) = 60; % πουτ 60 γουεαρ Mask ηζ True!
B2(Mask) = 70; % πουτ 70 γουεαρ Mask ηζ True!

NewIm2 = cat(3, R2, G2, B2); % Sukasumeseleba! Habibi chan! Uleleleleleeeeeeeh!!!!!

如果您更喜欢python而不是matlab,则可以使用scipy.imread,numpy等在python中类似地读取和比较图像。不需要金手指。


PS。正如您从我的讽刺代码注释中可能已经意识到的那样,请问诸如SO之类的国际受众时,请考虑只用英语编写代码。阅读这样的“混合”代码非常令人沮丧和厌倦,因此它显得粗鲁和不周全。 (尽管我碰巧会说一定数量的德语和英语,这并不是我的母语,但即使对于像我这样的人也是如此。)更不用说这样做会冒不必要的风险将听众限制为仅说德语的人!

相关问题