MAC关闭谷歌浏览器同源安全策略 2020年10月22日 MAC ### 1.创建一个保存浏览器用户数据的文件夹 ### 2.创建一个脚本文件 ``` cd Desktop touch chrome-debug.command ``` ### 3.在文本文件里面写入脚本 ``` #! /bin/bash open -n /Applications/Google\ Chrome.app/ --args --disable-web-security --user-data-dir=/Users/xxx/Documents/ChromeDevUserData ``` > —user-data-dir 参数指向浏览器用户数据的存放位置 > --disable-web-security 表示关闭浏览器同源策略 ### 4.给创建好的脚本文件赋予权限 ``` chmod 755 chrome-debug.command ``` Unix系统的文件权限是三位数,第一个数字表示此设备现在用户的权限,第二个数字表示此设备其他用户的权限,第三个数字表示访客用户的权限。7是最高权限:可读可写可执行,5是可读可执行 ### 5.双击脚本运行,之后需要跨域调试时就通过双击这个脚本打开浏览器
C# 获取或设置图片像素点的颜色 2020年10月22日 NET 在C#中,获取或者设置图片像素点的颜色,一般用`Bitmap`对象的`GetPixel`方法和`SetPixel`方法来获取像素点和设置像素点,但这两个方法都很慢。 可以使用`BitmapData`类来加快速度。 ## Bitmap类 `Bitmap`对象封装了GDI+中的一个位图,此位图由图形图像及其属性的像素数据组成。该类的主要方法和属性如下: 1. `GetPixel`方法和`SetPixel`方法:获取和设置一个图像的指定像素的颜色 2. `PixelFormat`属性:返回图像的像素格式 3. `Palette`属性:获取和设置图像所使用的颜色调色板 4. `Heigh`和`Width`属性:返回图像的高度和宽度 5. `LockBits`方法和`UnlockBits`方法:分别锁定和解锁系统内存中的位图像素。在基于像素点的图像处理方法中使用`LockBits`和`UnlockBits`是一个很好的方式,这两种方法可以使我们指定像素的范围来控制位图的任意一部分,从而消除了通过循环对位图的像素逐个进行处理,每调用`LockBits`之后都应该调用一次`UnlockBits` ## BitmapData类 `BitmapData`对象指定了位图的属性 1. `Height`属性:被锁定位图的高度 2. `Width`属性:被锁定位图的高度 3. `PixelFormat`属性:数据的实际像素格式 4. `Scan0`属性:被锁定数组的首字节地址,如果整个图像被锁定,则是图像的第一个字节地址 5. `Stride`属性:步幅,也称为扫描宽度  如上图所示,数组的长度并不一定等于图像像素数组的长度,还有一部分未用区域,这涉及到位图的数据结构,系统要保证每行的字节数必须为4的倍数。 ## bpp 像素深度 像素深度是指存储每个像素所用的位数,也用它来度量图像的分辨率。像素深度决定彩色图像的每个像素可能有的颜色数,或者确定灰度图像的每个像素可能有的灰度级数。 例如,一幅彩色图像的每个像素用R,G,B三个分量表示,若每个分量用8位,那么一个像素共用24位表示,就说像素的深度为24,每个像素可以是16 777 216(2的24次方)种颜色中的一种。在这个意义上,往往把像素深度说成是图像深度。表示一个像素的位数越多,它能表达的颜色数目就越多,而它的深度就越深。 ## LockBitmap类 结合`Bitmap`类和`BitmapData`类新建一个`LockBitmap`类,用来方便获取或者设置图片像素点的颜色 ``` csharp public class LockBitmap { private readonly Bitmap _source = null; IntPtr _iptr = IntPtr.Zero; BitmapData _bitmapData = null; public byte[] Pixels { get; set; } public int Depth { get; private set; } public int Width { get; private set; } public int Height { get; private set; } public LockBitmap(Bitmap source) { this._source = source; } /// <summary> /// 锁定位图数据 /// </summary> public void LockBits() { try { // 获取位图的宽和高 Width = _source.Width; Height = _source.Height; // 获取锁定像素点的总数 int pixelCount = Width * Height; // 创建锁定的范围 Rectangle rect = new Rectangle(0, 0, Width, Height); // 获取像素格式大小 Depth = Image.GetPixelFormatSize(_source.PixelFormat); // 检查像素格式 if (Depth != 8 && Depth != 24 && Depth != 32) { throw new ArgumentException("仅支持8,24和32像素位数的图像"); } // 锁定位图并返回位图数据 _bitmapData = _source.LockBits(rect, ImageLockMode.ReadWrite, _source.PixelFormat); // 创建字节数组以复制像素值 int step = Depth / 8; Pixels = new byte[pixelCount * step]; _iptr = _bitmapData.Scan0; // 将数据从指针复制到数组 Marshal.Copy(_iptr, Pixels, 0, Pixels.Length); } catch (Exception ex) { throw ex; } } /// <summary> /// 解锁位图数据 /// </summary> public void UnlockBits() { try { // 将数据从字节数组复制到指针 Marshal.Copy(Pixels, 0, _iptr, Pixels.Length); // 解锁位图数据 _source.UnlockBits(_bitmapData); } catch (Exception ex) { throw ex; } } /// <summary> /// 获取像素点的颜色 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public Color GetPixel(int x, int y) { Color clr = Color.Empty; // 获取颜色组成数量 int cCount = Depth / 8; // 获取指定像素的起始索引 int i = ((y * Width) + x) * cCount; if (i > Pixels.Length - cCount) throw new IndexOutOfRangeException(); if (Depth == 32) // 获得32 bpp红色,绿色,蓝色和Alpha { byte b = Pixels[i]; byte g = Pixels[i + 1]; byte r = Pixels[i + 2]; byte a = Pixels[i + 3]; // a clr = Color.FromArgb(a, r, g, b); } if (Depth == 24) // 获得24 bpp红色,绿色和蓝色 { byte b = Pixels[i]; byte g = Pixels[i + 1]; byte r = Pixels[i + 2]; clr = Color.FromArgb(r, g, b); } if (Depth == 8) // 获得8 bpp { byte c = Pixels[i]; clr = Color.FromArgb(c, c, c); } return clr; } /// <summary> /// 设置像素点颜色 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="color"></param> public void SetPixel(int x, int y, Color color) { // 获取颜色组成数量 int cCount = Depth / 8; // 获取指定像素的起始索引 int i = ((y * Width) + x) * cCount; if (Depth == 32) { Pixels[i] = color.B; Pixels[i + 1] = color.G; Pixels[i + 2] = color.R; Pixels[i + 3] = color.A; } if (Depth == 24) { Pixels[i] = color.B; Pixels[i + 1] = color.G; Pixels[i + 2] = color.R; } if (Depth == 8) { Pixels[i] = color.B; } } } ``` ## 实例 ``` csharp using (Image img = Image.FromFile("ImagePath")) { using (Bitmap bmp = new Bitmap(img)) { int totalX = bmp.Width; int totalY = bmp.Height; var lockBitmap = new LockBitmap(bmp); lockBitmap.LockBits(); var pixelColor = lockBitmap.GetPixel(0, 50); lockBitmap.UnlockBits(); } } ``` ## 参考地址 [Work with Bitmaps Faster in C# - CodeProject](https://www.codeproject.com/Tips/240428/Work-with-Bitmaps-Faster-in-Csharp-3) [像素深度_百度百科](https://baike.baidu.com/item/%E5%83%8F%E7%B4%A0%E6%B7%B1%E5%BA%A6/10150486?fromtitle=BPP&fromid=15196141&fr=aladdin) [色彩深度 - 维基百科,自由的百科全书](https://zh.wikipedia.org/wiki/%E8%89%B2%E5%BD%A9%E6%B7%B1%E5%BA%A6)
jupyter 记事本的安装与试用 2020年10月22日 jupyter python ## jupyter 简介 Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言。 Jupyter Notebook是一个开源Web应用程序,允许您创建和共享包含实时代码,方程式,可视化和叙述文本的文档。用途包括:数据清理和转换,数值模拟,统计建模,数据可视化,机器学习等等。 ## 安装 > **在安装之前jupyter之前,需要先安装python(Python 3.3 或者更高版本, 或者 Python 2.7)。** 可以通过pip安装 Python3 ```shell python3 -m pip install --upgrade pip python3 -m pip install jupyter ``` python2.7 ``` shell python -m pip install --upgrade pip python -m pip install jupyter ``` **备注** 我在安装运行`python3 -m pip install --upgrade pip`这个命令的时候出现过下面的这个错误: ``` shell Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/local/bin/pip' Consider using the `--user` option or check the permissions. ``` 加上 `--user`在运行一遍就好了 ``` shell python3 -m pip install --upgrade --user pip ``` ## 运行使用 安装好之后,运行 jupyter 就简单了,直接运行`jupyter notebook`命令就可以了。运行之后会通过浏览器打开`http://localhost:8888/`这个网址,然后就可以看到如下界面:  然后新建一个可以运行python的文本文件,点击新建→Python3  点击之后,就会出来如下界面,这个界面就可以添加要运行的代码和文字了,下图画红框的地方可以选择当前编辑块,是代码还是markdown。选择标记,就是markdown编辑器;选择代码,就是代码编辑器。  选中编辑块,然后点击运行,就可以看到代码执行的结果  **备注** * 可以直接打开某个固定的文件 ```shell jupyter notebook notebook.ipynb ``` * 可以更改打开网页的端口号 ``` shell jupyter notebook --port 9999 ``` * 创建一个通用配置文件,注:`{application}`一般为`notebook` ``` shell jupyter {application} --generate-config ``` 可以通过配置文件,修改端口和起始文件夹等等配置,都有注释,只要把代码前的#删除就启用了 ``` python ## 用于笔记本和内核的目录。 #c.NotebookApp.notebook_dir = '' ## notebook服务会监听的IP端口. #c.NotebookApp.port = 8888 ``` ## 支持Javascirpt 需要安装nodejs,最好是v9或更高的版本,不然可能会出现问题,下面会讲。 ``` shell npm install -g ijavascript ``` 安装好之后,执行一次下面的命令 ``` shell ijsinstall ``` 然后运行`jupyter notebook`就可以了 **备注** 我的运行之后,编辑javascript的内容时,会报错 ``` php KernelRestarter: restarting kernel (2/5), new random ports internal/modules/cjs/loader.js:750 return process.dlopen(module, path.toNamespacedPath(filename)); ^ Error: The module '/Users/[name]/.nvm/versions/node/v8.9.1/lib/node_modules/ijavascript/node_modules/zeromq/build/Release/zmq.node' was compiled against a different Node.js version using NODE_MODULE_VERSION 57. This version of Node.js requires NODE_MODULE_VERSION 67. Please try re-compiling or re-installing the module (for instance, using `npm rebuild` or `npm install`). at Object.Module._extensions..node (internal/modules/cjs/loader.js:750:18) at Module.load (internal/modules/cjs/loader.js:620:32) at tryModuleLoad (internal/modules/cjs/loader.js:560:12) at Function.Module._load (internal/modules/cjs/loader.js:552:3) at Module.require (internal/modules/cjs/loader.js:657:17) at require (internal/modules/cjs/helpers.js:22:18) at Object.<anonymous> (/Users/geekzw/.nvm/versions/node/v8.9.1/lib/node_modules/ijavascript/node_modules/zeromq/lib/index.js:6:11) at Module._compile (internal/modules/cjs/loader.js:721:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10) at Module.load (internal/modules/cjs/loader.js:620:32) ``` 谷歌之后发现,好像是因为nodejs的版本问题,这个需要nodejs v9版本的,我的是v8版本的,不过也有解决办法,运行以下命令 ``` shell ijsinstall --spec-path=full ``` ## 参考 * [Project Jupyter | Installing the Jupyter Notebook](https://jupyter.org/install.html) * [Jupyter Notebook Quickstart — Jupyter Documentation 4.1.1 alpha documentation](https://jupyter.readthedocs.io/en/latest/content-quickstart.html) * [Jupyter’s Common Configuration Approach — Jupyter Documentation 4.1.1 alpha documentation](https://jupyter.readthedocs.io/en/latest/projects/config.html) * [ijavascript - npm](https://www.npmjs.com/package/ijavascript#installation)