Python pillow получить цвет пикселя

Concepts#

An image can consist of one or more bands of data. The Python Imaging Library allows you to store several bands in a single image, provided they all have the same dimensions and depth. For example, a PNG image might have ‘R’, ‘G’, ‘B’, and ‘A’ bands for the red, green, blue, and alpha transparency values. Many operations act on each band separately, e.g., histograms. It is often useful to think of each pixel as having one value per band. To get the number and names of bands in an image, use the getbands() method.

Modes#

The mode of an image is a string which defines the type and depth of a pixel in the image. Each pixel uses the full range of the bit depth. So a 1-bit pixel has a range of 0-1, an 8-bit pixel has a range of 0-255, a 32-signed integer pixel has the range of INT32 and a 32-bit floating point pixel has the range of FLOAT32. The current release supports the following standard modes:

  • 1 (1-bit pixels, black and white, stored with one pixel per byte)
  • L (8-bit pixels, grayscale)
  • P (8-bit pixels, mapped to any other mode using a color palette)
  • RGB (3×8-bit pixels, true color)
  • RGBA (4×8-bit pixels, true color with transparency mask)
  • CMYK (4×8-bit pixels, color separation)
  • YCbCr (3×8-bit pixels, color video format)
    • Note that this refers to the JPEG, and not the ITU-R BT.2020, standard
    • Hue’s range of 0-255 is a scaled version of 0 degrees

    Pillow also provides limited support for a few additional modes, including:

    • LA (L with alpha)
    • PA (P with alpha)
    • RGBX (true color with padding)
    • RGBa (true color with premultiplied alpha)
    • La (L with premultiplied alpha)
    • I;16 (16-bit unsigned integer pixels)
    • I;16L (16-bit little endian unsigned integer pixels)
    • I;16B (16-bit big endian unsigned integer pixels)
    • I;16N (16-bit native endian unsigned integer pixels)
    • BGR;15 (15-bit reversed true colour)
    • BGR;16 (16-bit reversed true colour)
    • BGR;24 (24-bit reversed true colour)

    Premultiplied alpha is where the values for each other channel have been multiplied by the alpha. For example, an RGBA pixel of (10, 20, 30, 127) would convert to an RGBa pixel of (5, 10, 15, 127) . The values of the R, G and B channels are halved as a result of the half transparency in the alpha channel.

    Apart from these additional modes, Pillow doesn’t yet support multichannel images with a depth of more than 8 bits per channel.

    Pillow also doesn’t support user-defined modes; if you need to handle band combinations that are not listed above, use a sequence of Image objects.

    You can read the mode of an image through the mode attribute. This is a string containing one of the above values.

    Size#

    You can read the image size through the size attribute. This is a 2-tuple, containing the horizontal and vertical size in pixels.

    Coordinate System#

    The Python Imaging Library uses a Cartesian pixel coordinate system, with (0,0) in the upper left corner. Note that the coordinates refer to the implied pixel corners; the centre of a pixel addressed as (0, 0) actually lies at (0.5, 0.5).

    Coordinates are usually passed to the library as 2-tuples (x, y). Rectangles are represented as 4-tuples, (x1, y1, x2, y2), with the upper left corner given first.

    Palette#

    The palette mode ( P ) uses a color palette to define the actual color for each pixel.

    Info#

    You can attach auxiliary information to an image using the info attribute. This is a dictionary object.

    How such information is handled when loading and saving image files is up to the file format handler (see the chapter on Image file formats ). Most handlers add properties to the info attribute when loading an image, but ignore it when saving images.

    Transparency#

    If an image does not have an alpha band, transparency may be specified in the info attribute with a “transparency” key.

    Most of the time, the “transparency” value is a single integer, describing which pixel value is transparent in a “1”, “L”, “I” or “P” mode image. However, PNG images may have three values, one for each channel in an “RGB” mode image, or can have a byte string for a “P” mode image, to specify the alpha value for each palette entry.

    Orientation#

    A common element of the info attribute for JPG and TIFF images is the EXIF orientation tag. This is an instruction for how the image data should be oriented. For example, it may instruct an image to be rotated by 90 degrees, or to be mirrored. To apply this information to an image, exif_transpose() can be used.

    Filters#

    For geometry operations that may map multiple input pixels to a single output pixel, the Python Imaging Library provides different resampling filters.

    Pick one nearest pixel from the input image. Ignore all other input pixels.

    Each pixel of source image contributes to one pixel of the destination image with identical weights. For upscaling is equivalent of Resampling.NEAREST . This filter can only be used with the resize() and thumbnail() methods.

    For resize calculate the output pixel value using linear interpolation on all pixels that may contribute to the output value. For other transformations linear interpolation over a 2×2 environment in the input image is used.

    Produces a sharper image than Resampling.BILINEAR , doesn’t have dislocations on local level like with Resampling.BOX . This filter can only be used with the resize() and thumbnail() methods.

    For resize calculate the output pixel value using cubic interpolation on all pixels that may contribute to the output value. For other transformations cubic interpolation over a 4×4 environment in the input image is used.

    Calculate the output pixel value using a high-quality Lanczos filter (a truncated sinc) on all pixels that may contribute to the output value. This filter can only be used with the resize() and thumbnail() methods.

    Источник

    Получение и изменение цвета пикселя в изображении. Библиотека PIL Python

    8 августа 2013 г. Archy Просмотров: 57227 RSS Обсудить
    Работа с изображениями » Python для начинающих изменить пиксель python, сменить формат изображения python, сохранения изображения python

    изменение цвета пикселя в изображении. Библиотека PIL Python

    В прошлой статье мы узнали как установить библиотеку PIL в Python и начать уже с ней работать. В этой статье мы продолжаем изучение библиотеки PIL. Начиная с версией 1.1.6 метод load() возвращает объект, с помощью которого можно получить доступ к отдельным пикселям изображения. Указав два значения внутри квадратных скобок, можно получить или задать цвет пикселя.

    >>> img = Image.open("foto.jpg") >>> obj = img.load() >>> obj[25, 45] # Получаем цвет пикселя (122, 86, 62) >>> obj[25, 45] = (255, 0, 0) # Задаем цвет пикселя (красный)

    Для доступа к отдельному пикселю вместо метода load() можно использовать методы getpixel() и putpixel(). Метод getpixel() позволяет получить цвет указанного пикселя, а метод putpixel(, ) изменяет цвет пикселя. Координаты пикселя указываются в виде кортежа из двух элементов. Необходимо заметить, что эти методы работают медленнее метода load(). Пример использования метода getpixel() и putpixel() приведен ниже.

    Использование метода getpixel() и putpixel()

    >>> img = Image.open("foto.jpg") >>> img.getpixel((25, 45)) # Получаем цвет пикселя (122, 86, 62) >>> img.putpixel((25, 45), (255, 0, 0)) # Изменяем цвет пикселя >>> img.getpixel((25, 45)) # Получаем цвет пикселя (255, 0, 0) >>> img.show() # Просматриваем изображение

    В этом примере для просмотра изображения мы воспользовались методом show(). Метод show() создает временный файл в формате BMP и запускает программу для просмотра изображения, используемую в операционной системе по умолчанию. Например, на моем компьютере запускается программа ACDSee. Для сохранения изображения в файл предназначен метод save().

    В первом параметре указывается абсолютный или относительный путь. Вместо пути можно передать файловый объект, открытый в бинарном режиме. Сохраним изображение в форматах JPEG и BMP разными способами.

    Сохраняем изображение в Python

    >>> img.save("tmp.jpg") # В формате JPEG >>> img.save("tmp.bmp", "BMP") # В формате BMP >>> f = open("tmp2.bmp", "wb") >>> img.save(f, "BMP") # Передаем файловый объект >>> f.close()

    Обратите внимание на то, что мы открыли файл в формате JPEG, а сохранили его в формате BMP. Таким образом, можно открывать изображения в одном формате и конвертировать его в другой формат. Если сохранить изображение не удалось, возбуждается исключение IOError. Если параметр не указан, то формат изображения определяется по расширению файла.

    В параметре можно указать дополнительный характеристики изображения. Поддерживаемый опции зависят от формата изображения. Например, по умолчанию изображения в формате JPEG сохраняются с качеством 75. С помощью опции quality можно указать другое значение в диапазоне от 1 до 100.

    >>> img.save("tmp3.jpg", "JPEG", quality=100) # Указание качества

    За дополнительной информации по опциям обращайтесь к документации. Так же для более подробной инструкции игры в покер можете прочитать poker правила и повысить свои навыки в этой игре. Удивите своих знакомых во время игры в покер, продемонстрировав им свой профессионализм игры.

    Источник

    Читайте также:  Test if defined python
Оцените статью