pyCoreImage Learn
PyCoreImage
安装
首先,需要下载安装包,下载地址:
- https://developer.apple.com/documentation/coreimage/prototyping_your_app_s_core_image_pipeline_with_python
- https://docs-assets.developer.apple.com/published/a86ed53526/PrototypingYourAppsCoreImagePipelineWithPython.zip
安装步骤:
easy_install pip
pip install setuptools --upgrade
python setup.py --user
pip install scikit-image --user
在 pyci_demo.py
文件头中也有注释,包含了安装说明
# Install
1. pip install pyobjc --ignore-installed --user
2. pip install numpy --ignore-installed --user
3. pip install scikit-image --user
确保安装 ok:
$ python
>> from pycoreimage.pyci import cimg
如果输出没有错误,那么代表安装 ok。
如果运行上面的导入库,出现其他提示信息,代表安装有问题。需要卸载后重新安装。
卸载方法 :1)列出已经安装的库列表,存入文件
$ pip freeze > list.txt
2)编辑list.txt ,筛选出带 pyobjc 和 pycoreimage 的库,然后保存。
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 thetranslate
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 thecrop
command:
img.rotate(radians)
保存
img.save('demo2.jpg')
显示
show(img, title='Demo 2: from file + slicing')
滤镜操作
打印所有的滤镜列表
所有的系统滤镜,从文档可以查到
滤镜按照功能,被分进不同的类别
- CICategoryBlur
- CICategoryColorAdjustment
- CICategoryColorEffect
- CICategoryCompositeOperation
- CICategoryDistortionEffect
- CICategoryGenerator
- CICategoryGeometryAdjustment
- CICategoryGradient
- CICategoryHalftoneEffect
- CICategoryReduction
- CICategorySharpen
- CICategoryStylize
- CICategoryTileEffect
- CICategoryTransition
源文件 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])
参考资料
- https://developer.apple.com/videos/play/wwdc2018/719/
- https://developer.apple.com/library/archive/documentation/GraphicsImaging/Reference/CoreImageFilterReference/
- https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_intro/ci_intro.html#//apple_ref/doc/uid/TP30001185
pyCoreImage Learn的更多相关文章
- Atitit learn by need 需要的时候学与预先学习知识图谱路线图
Atitit learn by need 需要的时候学与预先学习知识图谱路线图 1. 体系化是什么 架构 知识图谱路线图思维导图的重要性11.1. 体系就是架构21.2. 只见树木不见森林21.3. ...
- Python 爬取所有51VOA网站的Learn a words文本及mp3音频
Python 爬取所有51VOA网站的Learn a words文本及mp3音频 #!/usr/bin/env python # -*- coding: utf-8 -*- #Python 爬取所有5 ...
- [转载]VIM 教程:Learn Vim Progressively
文章来源:http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/ Learn Vim Progressively TL ...
- some tips learn from work experience
1.you can't avoid office politics 2.you'll never have a job which you "can't quit" - if yo ...
- Java-集合(没做出来)第四题 (List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列。 例如: List list = new ArrayList(); list.add(“Hello”); list.add(“World”); list.add(“Learn”); //此时list 为Hello World Learn reverseL
没做出来 第四题 (List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列. 例如: List list = new ArrayList(); list.a ...
- Learn RxJava
Learn RxJava http://reactivex.io/documentation/operators.html https://github.com/ReactiveX/RxJava/wi ...
- ANSI Common Lisp Learn
It has been a long time that I haven't dealt with my blog. On one hand I was preparing the exams.On ...
- [Notes] Learn Python2.7 From Python Tutorial
I have planed to learn Python for many times. I have started to learn Python for many times . Howeve ...
- 十分钟入门less(翻译自:Learn lESS in 10 Minutes(or less))
十分钟入门less(翻译自:Learn lESS in 10 Minutes(or less)) 注:本文为翻译文章,因翻译水平有限,难免有缺漏不足之处,可查看原文. 我们知道写css代码是非常枯燥的 ...
随机推荐
- 英语口语练习系列-C29-鸟类-辨别身份-断弦的琴
鸟类 sparrow sparrow 英 ['spærəʊ] 美 ['spæro] n. 麻雀:矮小的人 swallow swallow 英 ['swɒləʊ] 美 ['swɑlo] vt. 忍受:吞 ...
- 英语口语练习系列-C28-海滨-辨别身份-悬崖边的树
beach 海滨 brown hut near body of water green trees near sea during daytime photography 海滨度假胜地 beach [ ...
- ES6快速入门(一)函数与作用域
ES6快速入门 一.块级绑定 1.var声明与变量提升 使用var声明的变量,不论在何处都会被视为(声明)在函数级作用域顶部的位置发生. function getValue(condition) { ...
- DWM1000 长距离模块讨论
蓝点DWM1000 模块已经打样测试完毕,有兴趣的可以申请购买了,更多信息参见 蓝点论坛 正文: DWM1000 作为超宽室内带测距/定位的代表,官方给出的模块标准距离30-50m,而目前X宝上有很多 ...
- React Native小白入门学习路径——一
前言 过去这段时间一直忙着实验室考核任务,拼尽全力完成了自己的任务之后.正准备开始高强度的实验室的学习的时候,实验室组织了新老生交流会,这也应该是头一次这么近距离的面对大四前辈交流想法.感觉自己受益颇 ...
- 第2讲——wiz
PC端信息收集 网页剪藏 win+s 屏幕截图:win+printscreen微博收集:@mywiz @我的印象笔记 按ESC隐藏/恢复左边导航栏 F11全屏阅读 打标签:解决文件夹重叠问题 搜索 ...
- BZOJ4095 : [Usaco2013 Dec]The Bessie Shuffle
首先将排列和整个序列以及询问都反过来,问题变成给定一个位置$x$,问它经过若干轮置换后会到达哪个位置. 每次置换之后窗口都会往右滑动一个,因此其实真实置换是$p[i]-1$. 对于每个询问,求出轮数, ...
- HashMap实现原理(jdk1.7/jdk1.8)
HashMap的底层实现: 1.简单回答 JDK1.7:HashMap的底层实现是:数组+链表 JDK1.8:HashMap的底层实现是:数组+链表/红黑树 为什么要红黑树? 红黑树:一个 ...
- Java 服务端监控方案(四. Java 篇)
http://jerrypeng.me/2014/08/08/server-side-java-monitoring-java/ 这个漫长的系列文章今天要迎来最后一篇了,也是真正与 Java 有关的部 ...
- Quartz 定时器任务调度配置(以及如何配置quartz启动执行一次)
1. 添加maven依赖.pom.xml 中添加jar文件 <!-- 作业任务调度机制 --> <dependency> <groupId>org.quartz-s ...