图像分布

时间:2017-07-20 17:27:15

标签: image python-2.7 performance image-processing

我实际上正在进行图像分发项目,我是图像处理的新手。我的目标是编写一个程序,将它分成6个部分,每个部分都有相同的像素数据。我写了下面提到的代码。

    from PIL import Image
    import cv2
    im = Image.open('grey.jpg')
    im_grey = im.convert('LA')

    width,height = im.size
    line = []

    j,h,total,c=0,0,0,0

    for i in range(0,width):    
        for j in range(0,height):
            total += im_grey.getpixel((i,j))[0]
            h += im_grey.getpixel((i,j))[0]
        c += 1
        if c==5:
            line.append(h)
            c = 0
            print "LINE : " + str(i) + "=" + str(h)
            h = 0

    average_pix = total/6
    print total
    i,m,j,=0,0,0,
    def image_distribution():
        global i,m,j,d,average_pix,image
        while i<=len(line)-1:

            j=j+ line[i]

            if j>=average_pix :
                img=im.crop((0,m,width,i*5))
                img.save("Images/"+"image"+str(i)+".jpg")
                m = i*5
                j=0

            i+=1
    image_distribution() 

代码似乎有效,但不正确,所以如果有人可以帮助我使用脚本或任何更好的脚本用于同一目的,我将非常感激。 附:我也在努力编写一个节省时间的程序。 谢谢,是的图像&#34; grey.jpg&#34;是一个灰度图像。

1 个答案:

答案 0 :(得分:1)

根据我们的讨论,您希望将图像分解为6个单独的图像。 6个单独图像的总和强度相等。由于您不喜欢如何分解图像,我将逐列分解图像。因此,每个图像将具有相同的行数,但是列的数量将不同,使得每个分解的图像将具有或多或少相同的强度和。但请注意,由于此算法的性质,我们可能无法为每个图像得到完全相同的总和,但差异应该是最小的。

因为您可以在Python中使用任何模块,我建议使用NumPy,但您仍然可以使用Pillow来读取原始图像。首先将图像转换为NumPy数组,以便我们可以完全使用NumPy数组方法来执行您想要的操作。

我将遵循的过程非常简单。

  1. 找出图像中所有强度值的总和,并将其除以6以确定&#34;分割点&#34;。
  2. 从第一列开始,找到列总和,然后移动到下一列并累积。
  3. 如果我们的累积列总和超过分割点,请保存此图像,重置累积列总和并从此点再次开始处理。
  4. 重复,直到我们到达图像的末尾。
  5. 获取提取的图像并将其保存到文件中。
  6. 没有进一步的麻烦:

    from PIL import Image
    import numpy as np
    
    # Open up the image and convert to grayscale
    im = Image.open('grey.jpg').convert('L')
    
    # Convert to a numpy array, convert to floating point for precision
    im = np.asarray(im, dtype=np.float)
    
    # Determine the total sum of the pixels
    total_sum = im.sum()
    
    # Determine the split point
    split_point = int(total_sum / 6)
    
    # Store the images here
    data = []
    
    # Counts up how many images we've made so far
    count = 0
    
    # Records the column sum
    column_sum = 0
    
    # Records beginning coordinate of the split image
    x = 0
    
    # Until we've exhausted all of the data...
    # Go through each column and determine the cumulative sums
    for i in range(im.shape[1]):
        column_sum += np.sum(im[:, i])
    
        # If the column sum has passed the threshold
        if column_sum >= split_point:
            # Get the image and save it
            data.append(im[:, x : i + 1])
    
            # Update the variables for the next round
            x = i + 1
            column_sum = 0
            count += 1
    
        # If we have reached the 5th image, just use the rest
        # of the image data as the last image
        if count == 5:
            data.append(im[:, x:])
            break
    
    # Save the images now
    for (i, split_img) in enumerate(data):
        pimg = Image.fromarray(split_img.astype(np.uint8))
        pimg.save("image%d.jpg" % (i + 1))            
    

    请注意,最后的复杂性是我们首先将分割图像转换为uint8,然后创建一个枕头图像,然后将其保存到文件中。图像标记为imagex.jpg,其中x是1到6之间的数字。

相关问题