图像编辑器/像素不工作,不知道为什么Python 3.5 PIL

2024-05-14 23:03:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python非常陌生,我一直在使用PIL工具进行图像编辑。我想出了一个代码,它应该采取一个图像,并打破了5x10像素块,并取代每个与该块的平均rgb值我试图做到这一点,增加x,y分别为10,5,记录每个像素的rgb值,平均他们,并分配给所有的像素块。 然而,对于我所有的代码来说,情况并没有发生任何变化。 我不知道为什么???? 代码如下:

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import time

def Edit(iar):
    balanceAr = []
    newAR = iar
    global x
    global y
    x = 1
    y = 1

    for eachRow in newAR[::5]:
        Pixx = Pix
        x1 = x
        del x
        x = x1

        y1 = y
        del y


        vr1 = Pixx[x1,y1]
        vr2 = Pixx[x1,y1 + 1]
        vr3 = Pixx[x1,y1 + 2]
        vr4 = Pixx[x1,y1 + 3]
        vr5 = Pixx[x1,y1 + 4]


        yValRAv = int((vr1[0] + vr2[0] + vr3[0] + vr4[0] + vr5[0])/5)
        yValGAv = int((vr1[1] + vr2[1] + vr3[1] + vr4[1] + vr5[1])/5)
        yValBAv = int((vr1[2] + vr2[2] + vr3[2] + vr4[2] + vr5[2])/5)


        for eachPix in eachRow:
            x = 1
            y = 1
            Pixx = Pix
            x1 = x
            del x

            y1 = y
            del y
            y = y1
            vr1 = Pixx[x1,y1]
            vr2 = Pixx[x1,y1 + 1]
            vr3 = Pixx[x1,y1 + 2]
            vr4 = Pixx[x1,y1 + 3]
            vr5 = Pixx[x1,y1 + 4]
            vr6 = Pixx[x1,y1 + 5]
            vr7 = Pixx[x1,y1 + 6]
            vr8 = Pixx[x1,y1 + 7]
            vr9 = Pixx[x1,y1 + 8]
            vr10 = Pixx[x1,y1 + 9]


            xValRAv = int((vr1[0] + vr2[0] + vr3[0] + vr4[0] + vr5[0] + vr6[0] + vr7[0] + vr8[0] + vr9[0] + vr10[0])/10)
            xValGAv = int((vr1[1] + vr2[1] + vr3[1] + vr4[1] + vr5[1] + vr6[0] + vr7[0] + vr8[0] + vr9[0] + vr10[0])/10)
            xValBAv = int((vr1[2] + vr2[2] + vr3[2] + vr4[2] + vr5[2] + vr6[0] + vr7[0] + vr8[0] + vr9[0] + vr10[0])/10)
            ValRAv = int((xValRAv + yValRAv)/2)
            ValGAv = int((xValGAv + yValGAv)/2)
            ValBAv = int((xValBAv + yValBAv)/2) 
            for count in range(5):
                for counter in range(10):
                    Pixx = Pix
                    Pixx[(x1 + counter), (y1 + count)] = (ValRAv, ValGAv, ValBAv)
            x = x1 + 10
        y = y1 + 5
        return newAR
    iar = newAR




i = Image.open('speaker8.png')
iar = np.array(i)
width, height = i.size
Pix = i.load()
Edit(iar)

fig = plt.figure()
ax1 = plt.subplot2grid((8,6), (0,0), rowspan=20, colspan=15)

ax1.imshow(iar)
plt.show()

请注意,这段代码的大部分是从教程中拼凑而成的,所以可能不会以正确的方式使用?你知道吗

谢谢!你知道吗


Tags: 代码importforpltintx1iary1

热门问题