如何使用PIL和numpy图像修复错误

时间:2015-03-03 18:41:31

标签: python image python-3.x numpy python-imaging-library

您必须在包含多个图像的文件夹中运行它并运行shuffle_all_images(),它将创建新文件夹并随机生成每个像素的所有值。我认为它与不转换为numpy图像而不是PIL图像有关,但我无法弄明白。

import random
import os.path
import PIL
import numpy

def image_shuffle(original_image):
    for row in len(original_image):
        for col in len(original_image[1]):
            r,g,b = original_image[row][col]
            r = random.randint(1,255)
            g = random.randint(1,255)
            b = random.randint(1,255)
            original_image[row][col] = [r, g, b]

    return original_image


def get_images(directory=None):
    """ Returns PIL.Image objects for all the images in directory.

    If directory is not specified, uses current directory.
    Returns a 2-tuple containing 
    a list with a  PIL.Image object for each image file in root_directory, and
    a list with a string filename for each image file in root_directory
    """

    if directory == None:
        directory = os.getcwd() # Use working directory if unspecified

    image_list = [] # Initialize aggregaotrs
    file_list = []

    directory_list = os.listdir(directory) # Get list of files
    for entry in directory_list:
        absolute_filename = os.path.join(directory, entry)
        try:
            image = PIL.Image.open(absolute_filename)
            file_list += [entry]
            image_list += [image]
        except IOError:
            pass # do nothing with errors tying to open non-images
    return image_list, file_list

def shuffle_all_images(directory=None):
    """ Saves a modfied version of each image in directory.

    Uses current directory if no directory is specified. 
    Places images in subdirectory 'modified', creating it if it does not exist.
    New image files are of type PNG and have transparent rounded corners.
    """

    if directory == None:
        directory = os.getcwd() # Use working directory if unspecified

    # Create a new directory 'modified'
    new_directory = os.path.join(directory, 'modified')
    try:
        os.mkdir(new_directory)
    except OSError:
        pass # if the directory already exists, proceed  

    #load all the images
    image_list, file_list = get_images(directory)  

    #go through the images and save modified versions
    for n in range(len(image_list)):
        # Parse the filename
        filename, filetype = file_list[n].split('.')

        # Round the corners with radius = 30% of short side
        new_image = image_shuffle(image_list[n])
        #save the altered image, suing PNG to retain transparency
        new_image_filename = os.path.join(new_directory, filename + '.png')
        new_image.save(new_image_filename)    

1 个答案:

答案 0 :(得分:0)

image_shuffle函数错误。 它应该是:

for row in range(original_image.size[0]):
    for col in range(original_image.size[1]):
        r = random.randint(0,255)
        g = random.randint(0,255)
        b = random.randint(0,255)
        original_image.putpixel((row, col), (r,g,b))

除非您不想获得所有可能的颜色,否则您希望颜色值从0开始。