2

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

我不确定 gimp python 是否可以做到这一切?

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

提前谢谢了

我第一次尝试c++,但处理图片并不是那么容易。我的老师建议我去gimp。原理图应该是这样的:

#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

4 回答 4

3

您可以/应该使用“像素区域”将图层导出/导入 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 运行时。有关一些提示,请参见此处。您可以在此处找到完整的脚本,它也可以用作如何获取图像和图层的示例。

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

于 2018-11-19T21:55:39.920 回答
0

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

所以我们有“图像”层(您为像素着色的地方)和“地图”层(给定的颜色您指示哪些像素应该被着色)。然后:

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

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

于 2018-11-19T22:16:25.883 回答
0

在 GIMP 中,检查gimp_drawable_get_pixelgimp_drawable_set_pixel过程。您可以在 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 层。

于 2018-11-19T19:42:30.270 回答
0

在 matlab / octave 中,这非常简单。不需要 gimp,如果你绝对必须有 c++ 代码集成,它可以很好地与 C++ 集成。例如,假设您想将 Image2 像素更改为 R:50、G:60、B:70,只要 Image1 具有像素 R:10、G:20、B:30。然后:

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

Im1 = imread('im1.jpg');  Im2 = imread('im2.jpg'); % readure imagurine
 R1 = Im1(:,:,1);          R2 = Im2(:,:,1);        % selectu 'red'   layeru piripitsi
 G1 = Im1(:,:,2);          G2 = Im2(:,:,2);        % selectu 'green' layeru piripitsi
 B1 = Im1(:,:,3);          B2 = Im2(:,:,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 中类似地读取和比较图像。不需要gimp。


PS。正如您可能从我讽刺的代码注释中意识到的那样,在询问诸如 SO 之类的国际受众时,请考虑仅用英语编写代码。阅读这种“混合”的代码非常令人沮丧和厌烦,因此它给人的印象是粗鲁和不体贴的;(即使对于像我这样的人来说也是如此,尽管我碰巧会说相当数量的德语,而英语不是我的母语。)。更不用说这样做会冒着不必要地将听众限制在讲德语的人的风险!

于 2018-11-20T01:15:15.867 回答