简体   繁体   English

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

[英]How do I fix my error with PIL and numpy images

You have to run it in the folder with a couple images and run shuffle_all_images() and it will create new folder and randomly generate all of the values for each pixel. 你必须在包含几个图像的文件夹中运行它并运行shuffle_all_images() ,它将创建新文件夹并随机生成每个像素的所有值。 I think it has to do with not converting to numpy images as opposed to PIL images?, but I can't figure it out. 我认为它与不转换为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)    

The image_shuffle function is wrong. image_shuffle函数错误。 It should be: 它应该是:

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))

You want to have color values starting from 0 unless you don't want to get all the possible colors. 除非您不想获得所有可能的颜色,否则您希望从0开始颜色值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM