PIL圖片格式轉換


PIL格式轉換

原圖:

#!/usr/local/bin/python
# -*- coding: utf8 -*-

from PIL import Image, ImageFilter
import os, sys

BASE_PATH = os.path.dirname(os.path.abspath(__file__))
· 1 (1-bit pixels, black and white, stored with one pixel per byte)
· L (8-bit pixels, black and white)
· P (8-bit pixels, mapped to any other mode using a colour palette)
· RGB (3x8-bit pixels, true colour)
· RGBA (4x8-bit pixels, true colour with transparency mask)
· CMYK (4x8-bit pixels, colour separation)
· YCbCr (3x8-bit pixels, colour video format)
· I (32-bit signed integer pixels)
· F (32-bit floating point pixels)

origin-->L-->RGB

模式“L”轉換為模式“RGB

模式“RGB”轉換為模式“L”以后,像素值為[0,255]之間的某個數值。而從模式“L”轉換成“RGB”時,“RGB”的三個通道都是模式“L”的像素值的拷貝。

file_path = os.path.join(BASE_PATH, "fj.jpg")

# 打開圖片
img = Image.open(file_path)
# 轉換為灰度模式
new_img_l = img.convert("L")
# 轉換為RGB模式
new_img_rgb = new_img_l.convert("RGB")

print u"原圖:", img.getpixel((0, 0))
print u"L:", new_img_l.getpixel((0, 0))
print u"RGB:", new_img_rgb.getpixel((0, 0))
# 存儲圖片
new_img_l.save("fj_l.jpg", "JPEG")
new_img_rgb.save("fj_rgb.jpg", "JPEG") # 結果 原圖: (0, 35, 54) L: 26 RGB: (26, 26, 26)

 L圖片:

RGB圖片:

 origin-->1-->RGB

模式“1”轉換為模式“RGB

模式“RGB”轉換為模式“1”以后,像素點變成黑白兩種點,要么是0,要么是255。而從模式“1”轉換成“RGB”時,“RGB”的三個通道都是模式“1”的像素值的拷貝。

file_path = os.path.join(BASE_PATH, "fj.jpg") 
# 打開圖片 
img = Image.open(file_path)
# 轉換為二值模式
new_img_1 = img.convert("1")
# 轉換為RGB模式
new_img_rgb = new_img_1.convert("RGB")
print u"原圖:", img.getpixel((0, 0))
print u"1:", new_img_1.getpixel((0, 0))
print u"RGB:", new_img_rgb.getpixel((0, 0))
# 存儲圖片
new_img_1.save("fj_1.jpg", "JPEG")
new_img_rgb.save("fj_1_rgb.jpg", "JPEG")

結果:
原圖: (0, 35, 54)
1: 0
RGB: (0, 0, 0)

1圖片:

RGB圖片:

 origin-->p-->RGB

模式“RGB”轉換為模式“P

像素值為[0,255]之間的某個數值,但它為調色板的索引值,其最終還是彩色圖像。從模式“P”轉換成“RGB”時,“RGB”的三個通道會變成模式“P”的像素值索引的彩色值

file_path = os.path.join(BASE_PATH, "fj.jpg")

# 打開圖片
img = Image.open(file_path)
# 轉換為P模式
new_img_p = img.convert("P")
# 轉換為RGB模式
new_img_rgb = new_img_p.convert("RGB")
print u"原圖:", img.getpixel((0, 0))
print u"p:", new_img_p.getpixel((0, 0))
print u"RGB:", new_img_rgb.getpixel((0, 0))
# 存儲圖片
new_img_p.save("fj_p.jpg", "JPEG")
new_img_rgb.save("fj_p_rgb.jpg", "JPEG")

結果:
原圖: (0, 35, 54)
p: 52
RGB: (0, 51, 51)

p圖片:

RGB圖片:

 origin-->RGBA-->RGB

模式“RGB”轉換為模式“RGBA”以后,圖像從三通道變成了四通道,其RGB三個通道的數值沒有變化,新增的alpha通道均為255,表示不透明。從模式“RGBA”轉換成“RGB”時,“RGB”的三個通道又變回原來的數值。

file_path = os.path.join(BASE_PATH, "fj.jpg")

# 打開圖片
img = Image.open(file_path)
# 轉換為RGBA模式
new_img_rgba = img.convert("RGBA")
# 轉換為RGB模式
new_img_rgb = new_img_rgba.convert("RGB")
print u"原圖:", img.getpixel((0, 0))
print u"p:", new_img_rgba.getpixel((0, 0))
print u"RGB:", new_img_rgb.getpixel((0, 0))
# 存儲圖片
# new_img_rgba.save("fj_rgba.jpg", "JPEG")
new_img_rgb.save("fj_rgba_rgb.jpg", "JPEG")

結果:
原圖: (0, 35, 54)
RGBA: (0, 35, 54, 255)
RGB: (0, 35, 54)

RGBA圖片:

RGB圖片:

 origin-->CMYK-->RGB

模式“RGB”轉換為模式“CMYK”以后,圖像從三通道變成了四通道,其CMY三個通道的數值是通過之前的公式計算得到,K通道被直接賦值為0

C = 255 - R
M = 255 - G
Y = 255 - B
K = 0

從模式“CMYK”轉換成“RGB”時,“RGB”的三個通道又變回原來的數值,這是無損的轉換。

R = 255 - C
G = 255 - M
B = 255 - Y

file_path = os.path.join(BASE_PATH, "fj.jpg")

# 打開圖片
img = Image.open(file_path)

# 轉換為CMYK模式
new_img_cmyk = img.convert("CMYK")
# 轉換為RGB模式
new_img_rgb = new_img_cmyk.convert("RGB")
print u"原圖:", img.getpixel((0, 0))
print u"CMYK:", new_img_cmyk.getpixel((0, 0))
print u"RGB:", new_img_rgb.getpixel((0, 0))
# 存儲圖片
new_img_cmyk.save("fj_cmyk.jpg", "JPEG")
new_img_rgb.save("fj_cmyk_rgb.jpg", "JPEG")

結果:
原圖: (0, 35, 54)
CMYK: (255, 220, 201, 0)
RGB: (0, 35, 54)

CMYK圖片:

RGB圖片:

 帶矩陣模式轉換

im.convert(mode,matrix) ⇒ image

這種定義只適合將一個“RGB”圖像轉換為“L”或者“RGB”圖像,不能轉換為其他模式的圖像。變量matrix為4或者16元組。

file_path = os.path.join(BASE_PATH, "fj.jpg")

# 打開圖片
img = Image.open(file_path)
rgb2xyz = (

    0.412453, 0.357580, 0.180423, 0,

    0.212671, 0.715160, 0.072169, 0,

    0.019334, 0.119193, 0.950227, 0)

img_L = img.convert("L", rgb2xyz)
img_rgb = img.convert("RGB", rgb2xyz)
img_L.save("img_L.jpg", "JPEG")
img_rgb.save("img_rgb.jpg", "JPEG")

img_L圖片:

img_rgb圖片:

 調色板轉換

file_path = os.path.join(BASE_PATH, "fj.jpg")

# 打開圖片
img = Image.open(file_path)
p1 = img.convert("P", dither=Image.NONE)
p2 = img.convert("P", dither=Image.ADAPTIVE)
p3 = img.convert("P", palette=Image.ADAPTIVE, colors=10)

p2_ = p2.convert("RGB")
p2_.save("p2.jpg", "JPEG")
p3_ = p3.convert("RGB")

p2圖片:

p3圖片:

 參考:http://blog.csdn.net/icamera0/article/details/50843196


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM