pyCoreImage Learn

PyCoreImage

安裝

首先,須要下載安裝包,下載地址:python

安裝步驟:chrome

easy_install pip
pip install setuptools --upgrade
python setup.py --user
pip install scikit-image --user

pyci_demo.py文件頭中也有註釋,包含了安裝說明bash

# Install
    1. pip install pyobjc --ignore-installed --user
    2. pip install numpy --ignore-installed --user
    3. pip install scikit-image --user

確保安裝 ok:app

$ python
>> from pycoreimage.pyci import cimg

若是輸出沒有錯誤,那麼表明安裝 ok。dom

若是運行上面的導入庫,出現其餘提示信息,表明安裝有問題。須要卸載後從新安裝。
卸載方法 :ide

1)列出已經安裝的庫列表,存入文件ui

$ pip freeze > list.txtthis

2)編輯list.txt ,篩選出帶 pyobjc 和 pycoreimage 的庫,而後保存。url

3)從文件中批量刪除已經安裝的庫

$ pip uninstall -r list.txt

高斯模糊濾鏡的例子

源文件 gaussianBlur.py

# coding: utf-8
from pycoreimage.pyci import *
def zxdemo_minimal(filepath):
    #讀取文件
    img = cimg.fromFile(filepath)
    #圖片縮放
    img = img.resize(500, preserveAspect=1)
    #高斯模糊
    r = 20
    blur = img.gaussianBlur(radius=r)
    #圖片保存
    blur.save(filepath + '.CIGaussianBlur.jpg')
    #顯示圖片
    show([img, blur], title=['input', 'Gaussian blur with radius {}'.format(r)])
zxdemo_minimal('/Users/zx/Downloads/2018bugs/201807/0705/cheers_1440x960.heic')

能夠使用 jupyter-notebook 邊寫邊運行。

使用詳細步驟說明

通常包含幾個步驟:

  • 導入庫文件 from pycoreimage.pyci import *

    pycoreimage supports all file formats that Core Image supports, including JPEG, PNG, and HEIC.

    支持 JGPE,PNG,HEIC 文件

  • 建立 cimg 對象

fpath = 'resources/YourFacialImage.HEIC'
image = cimg.fromFile(fpath)
  • 對圖片添加濾鏡
gaussianBlurImg = image.gaussianBlur(radius=30)
  • 顯示圖片
show([image, gaussianBlurImg], title=['input', 'Gaussian blur with radius {}'.format(r)])

常見操做

生成二維碼

cimg.fromGenerator('CIQRCodeGenerator', message='Hello World!')

平移

Shift the image by the amount (tx, ty) with the translate command:

img.translate(tx, ty)

縮放

Use the scale command to resize an image:

img.scale(sx, sy)

旋轉

Rotate the image about its center point with the rotate command:

img.rotate(radians)

裁剪

Crop the image to the rectangle [x, y, width, height] with the crop command:

img.rotate(radians)

保存

img.save('demo2.jpg')

顯示

show(img, title='Demo 2: from file + slicing')

濾鏡操做

打印全部的濾鏡列表

全部的系統濾鏡,從文檔能夠查到

https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_intro/ci_intro.html#//apple_ref/doc/uid/TP30001185

濾鏡按照功能,被分進不一樣的類別

源文件 listFilters.py

# coding: utf-8
from pycoreimage.pyci import *
print(cimg.filters())
$ python src/listFilters.py 
(
    CIAccordionFoldTransition,
    CIAdditionCompositing,
    CIAffineClamp,
    CIAffineTile,
    CIAffineTransform,
    CIAreaAverage,
    CIAreaHistogram,
    CIAreaMaximum,
    CIAreaMaximumAlpha,
    CIAreaMinimum,
    CIAreaMinimumAlpha,
    CIAreaMinMaxRed,
    CIAttributedTextImageGenerator,
    CIAztecCodeGenerator,
    CIBarcodeGenerator,
    CIBarsSwipeTransition,
    CIBicubicScaleTransform,
    CIBlendWithAlphaMask,
    CIBlendWithBlueMask,
    CIBlendWithMask,
    CIBlendWithRedMask,
    CIBloom,
    CIBokehBlur,
    CIBoxBlur,
    CIBumpDistortion,
    CIBumpDistortionLinear,
    CICheckerboardGenerator,
    CICircleSplashDistortion,
    CICircularScreen,
    CICircularWrap,
    CIClamp,
    CICMYKHalftone,
    CICode128BarcodeGenerator,
    CIColorBlendMode,
    CIColorBurnBlendMode,
    CIColorClamp,
    CIColorControls,
    CIColorCrossPolynomial,
    CIColorCube,
    CIColorCubesMixedWithMask,
    CIColorCubeWithColorSpace,
    CIColorCurves,
    CIColorDodgeBlendMode,
    CIColorInvert,
    CIColorMap,
    CIColorMatrix,
    CIColorMonochrome,
    CIColorPolynomial,
    CIColorPosterize,
    CIColumnAverage,
    CIComicEffect,
    CIConstantColorGenerator,
    CIConvolution3X3,
    CIConvolution5X5,
    CIConvolution7X7,
    CIConvolution9Horizontal,
    CIConvolution9Vertical,
    CICopyMachineTransition,
    CICrop,
    CICrystallize,
    CIDarkenBlendMode,
    CIDepthBlurEffect,
    CIDepthOfField,
    CIDepthToDisparity,
    CIDifferenceBlendMode,
    CIDiscBlur,
    CIDisintegrateWithMaskTransition,
    CIDisparityToDepth,
    CIDisplacementDistortion,
    CIDissolveTransition,
    CIDivideBlendMode,
    CIDotScreen,
    CIDroste,
    CIEdgePreserveUpsampleFilter,
    CIEdges,
    CIEdgeWork,
    CIEightfoldReflectedTile,
    CIExclusionBlendMode,
    CIExposureAdjust,
    CIFalseColor,
    CIFlashTransition,
    CIFourfoldReflectedTile,
    CIFourfoldRotatedTile,
    CIFourfoldTranslatedTile,
    CIGammaAdjust,
    CIGaussianBlur,
    CIGaussianGradient,
    CIGlassDistortion,
    CIGlassLozenge,
    CIGlideReflectedTile,
    CIGloom,
    CIHardLightBlendMode,
    CIHatchedScreen,
    CIHeightFieldFromMask,
    CIHexagonalPixellate,
    CIHighlightShadowAdjust,
    CIHistogramDisplayFilter,
    CIHoleDistortion,
    CIHueAdjust,
    CIHueBlendMode,
    CIHueSaturationValueGradient,
    CIKaleidoscope,
    CILabDeltaE,
    CILanczosScaleTransform,
    CILenticularHaloGenerator,
    CILightenBlendMode,
    CILightTunnel,
    CILinearBurnBlendMode,
    CILinearDodgeBlendMode,
    CILinearGradient,
    CILinearToSRGBToneCurve,
    CILineOverlay,
    CILineScreen,
    CILuminosityBlendMode,
    CIMaskedVariableBlur,
    CIMaskToAlpha,
    CIMaximumComponent,
    CIMaximumCompositing,
    CIMedianFilter,
    CIMinimumComponent,
    CIMinimumCompositing,
    CIModTransition,
    CIMorphologyGradient,
    CIMorphologyMaximum,
    CIMorphologyMinimum,
    CIMotionBlur,
    CIMultiplyBlendMode,
    CIMultiplyCompositing,
    CINinePartStretched,
    CINinePartTiled,
    CINoiseReduction,
    CIOpTile,
    CIOverlayBlendMode,
    CIPageCurlTransition,
    CIPageCurlWithShadowTransition,
    CIParallelogramTile,
    CIPDF417BarcodeGenerator,
    CIPerspectiveCorrection,
    CIPerspectiveTile,
    CIPerspectiveTransform,
    CIPerspectiveTransformWithExtent,
    CIPhotoEffectChrome,
    CIPhotoEffectFade,
    CIPhotoEffectInstant,
    CIPhotoEffectMono,
    CIPhotoEffectNoir,
    CIPhotoEffectProcess,
    CIPhotoEffectTonal,
    CIPhotoEffectTransfer,
    CIPinchDistortion,
    CIPinLightBlendMode,
    CIPixellate,
    CIPointillize,
    CIQRCodeGenerator,
    CIRadialGradient,
    CIRandomGenerator,
    CIRippleTransition,
    CIRowAverage,
    CISaturationBlendMode,
    CIScreenBlendMode,
    CISepiaTone,
    CIShadedMaterial,
    CISharpenLuminance,
    CISixfoldReflectedTile,
    CISixfoldRotatedTile,
    CISmoothLinearGradient,
    CISoftLightBlendMode,
    CISourceAtopCompositing,
    CISourceInCompositing,
    CISourceOutCompositing,
    CISourceOverCompositing,
    CISpotColor,
    CISpotLight,
    CISRGBToneCurveToLinear,
    CIStarShineGenerator,
    CIStraightenFilter,
    CIStretchCrop,
    CIStripesGenerator,
    CISubtractBlendMode,
    CISunbeamsGenerator,
    CISwipeTransition,
    CITemperatureAndTint,
    CITextImageGenerator,
    CIThermal,
    CIToneCurve,
    CITorusLensDistortion,
    CITriangleKaleidoscope,
    CITriangleTile,
    CITwelvefoldReflectedTile,
    CITwirlDistortion,
    CIUnsharpMask,
    CIVibrance,
    CIVignette,
    CIVignetteEffect,
    CIVortexDistortion,
    CIWhitePointAdjust,
    CIXRay,
    CIZoomBlur
)

打印某個濾鏡的詳細信息

源文件detailFliterInfo.py

# coding: utf-8
from pycoreimage.pyci import *
print(cimg.inputs('gaussianBlur'))
print(cimg.inputs('additionCompositing'))

輸出:

$ python python/PyCoreImage/src/detailFliterInfo.py
{
    "CIAttributeFilterAvailable_Mac" = "10.4";
    "CIAttributeFilterAvailable_iOS" = 6;
    CIAttributeFilterCategories =     (
        CICategoryBlur,
        CICategoryStillImage,
        CICategoryVideo,
        CICategoryBuiltIn
    );
    CIAttributeFilterDisplayName = "Gaussian Blur";
    CIAttributeFilterName = CIGaussianBlur;
    CIAttributeReferenceDocumentation = "http://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CIGaussianBlur";
    inputImage =     {
        CIAttributeClass = CIImage;
        CIAttributeDescription = "The image to use as an input image. For filters that also use a background image, this is the foreground image.";
        CIAttributeDisplayName = Image;
        CIAttributeType = CIAttributeTypeImage;
    };
    inputRadius =     {
        CIAttributeClass = NSNumber;
        CIAttributeDefault = 10;
        CIAttributeDescription = "The radius determines how many pixels are used to create the blur. The larger the radius, the blurrier the result.";
        CIAttributeDisplayName = Radius;
        CIAttributeIdentity = 0;
        CIAttributeMin = 0;
        CIAttributeSliderMax = 100;
        CIAttributeSliderMin = 0;
        CIAttributeType = CIAttributeTypeScalar;
    };
}
{
    "CIAttributeFilterAvailable_Mac" = "10.4";
    "CIAttributeFilterAvailable_iOS" = 5;
    CIAttributeFilterCategories =     (
        CICategoryCompositeOperation,
        CICategoryVideo,
        CICategoryStillImage,
        CICategoryInterlaced,
        CICategoryNonSquarePixels,
        CICategoryHighDynamicRange,
        CICategoryBuiltIn
    );
    CIAttributeFilterDisplayName = Addition;
    CIAttributeFilterName = CIAdditionCompositing;
    CIAttributeReferenceDocumentation = "http://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CIAdditionCompositing";
    inputBackgroundImage =     {
        CIAttributeClass = CIImage;
        CIAttributeDescription = "The image to use as a background image.";
        CIAttributeDisplayName = "Background Image";
        CIAttributeType = CIAttributeTypeImage;
    };
    inputImage =     {
        CIAttributeClass = CIImage;
        CIAttributeDescription = "The image to use as an input image. For filters that also use a background image, this is the foreground image.";
        CIAttributeDisplayName = Image;
        CIAttributeType = CIAttributeTypeImage;
    };
}

濾鏡的詳細信息和使用 OC 打印結果相同。

使用 zoomBlur 濾鏡

源文件 CIZoomBlur.py

# coding: utf-8
from pycoreimage.pyci import *

# read imag file
filepath = '/Users/zx/Downloads/2018bugs/201807/0705/cheers_1440x960.heic'
img = cimg.fromFile(filepath)

# apply zoomBlur
r1 = img.zoomBlur()
r2 = img.zoomBlur(center=[500,500],amount=10)

#show images

show([img, r1,r2], title=['src', 'r1','r2'])

# https://developer.apple.com/library/archive/documentation/GraphicsImaging/Reference/CoreImageFilterReference/#//apple_ref/doc/filter/ci/CIZoomBlur

能夠看到,使用濾鏡的時候,參數名稱有變化。

原來的 inputCenter,inputAmout 分別變爲了center,amout.

使用 mono 濾鏡

源文件 CIPhotoEffectMono.py

# coding: utf-8
from pycoreimage.pyci import *

# read imag file
filepath = '/Users/zx/Downloads/2018bugs/201807/0705/cheers_1440x960.heic'
img = cimg.fromFile(filepath)

# print filter info
print(cimg.inputs('photoEffectMono'))

# apply photoEffectMono
r = img.photoEffectMono()

#show images
show([img, r], title=['src', 'r'])

使用疊加率濾鏡

源文件 CILightenBlendMode.py

# coding: utf-8
from pycoreimage.pyci import *

# read imag file
filepath = '/Users/zx/Downloads/2018bugs/201807/0705/cheers_1440x960.heic'
img = cimg.fromFile(filepath)

backgourdImg = cimg.fromFile('/Users/zx/Downloads/zx.jpg')

# apply 
r1 = img.additionCompositing(backgroundImage=backgourdImg)
r2 = img.colorBlendMode(backgroundImage=backgourdImg)
r3 = img.colorBurnBlendMode(backgroundImage=backgourdImg)
r4 = img.colorDodgeBlendMode(backgroundImage=backgourdImg)
r5 = img.darkenBlendMode(backgroundImage=backgourdImg)
 # = img.DifferenceBlendMode
 # = img.DivideBlendMode
 # = img.ExclusionBlendMode
 # = img.HardLightBlendMode
 # = img.HueBlendMode
 # = img.LightenBlendMode
 # = img.LinearBurnBlendMode
 # = img.LinearDodgeBlendMode
 # = img.LuminosityBlendMode
 # = img.MaximumCompositing
 # = img.MinimumCompositing
 # = img.MultiplyBlendMode
 # = img.MultiplyCompositing
 # = img.OverlayBlendMode
 # = img.PinLightBlendMode
 # = img.SaturationBlendMode
 # = img.ScreenBlendMode
 # = img.SoftLightBlendMode
 # = img.SourceAtopCompositing
 # = img.SourceInCompositing
 # = img.SourceOutCompositing
 # = img.SourceOverCompositing
 # = img.SubtractBlendMode

#show images
show([img,r1,r2,r3,r4,r5])

參考資料

相關文章
相關標籤/搜索