国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python讀取圖片任意范圍區域

2020-02-16 00:51:10
字體:
來源:轉載
供稿:網友

使用python進行圖片處理,現在需要讀出圖片的任意一塊區域,并將其轉化為一維數組,方便后續卷積操作的使用。
下面使用兩種方法進行處理:

convert 函數

from PIL import Imageimport numpy as npimport matplotlib.pyplot as pltdef ImageToMatrix(filename): im = Image.open(filename)  # 讀取圖片 im.show()      # 顯示圖片 width,height = im.size print("width is :" + str(width)) print("height is :" + str(height)) im = im.convert("L")    # pic --> mat 轉換,可以選擇不同的模式,下面有函數源碼具體說明 data = im.getdata() data = np.matrix(data,dtype='float')/255.0 new_data = np.reshape(data * 255.0,(height,width)) new_im = Image.fromarray(new_data) # 顯示從矩陣數據得到的圖片 new_im.show() return new_datadef MatrixToImage(data): data = data*255 new_im = Image.fromarray(data.astype(np.uint8)) return new_im''' convert(self, mode=None, matrix=None, dither=None, palette=0, colors=256)  |  Returns a converted copy of this image. For the "P" mode, this  |  method translates pixels through the palette. If mode is  |  omitted, a mode is chosen so that all information in the image  |  and the palette can be represented without a palette.  |    |  The current version supports all possible conversions between  |  "L", "RGB" and "CMYK." The **matrix** argument only supports "L"  |  and "RGB".  |    |  When translating a color image to black and white (mode "L"),  |  the library uses the ITU-R 601-2 luma transform::  |    |   L = R * 299/1000 + G * 587/1000 + B * 114/1000  |    |  The default method of converting a greyscale ("L") or "RGB"  |  image into a bilevel (mode "1") image uses Floyd-Steinberg  |  dither to approximate the original image luminosity levels. If  |  dither is NONE, all non-zero values are set to 255 (white). To  |  use other thresholds, use the :py:meth:`~PIL.Image.Image.point`  |  method.  |    |  :param mode: The requested mode. See: :ref:`concept-modes`.  |  :param matrix: An optional conversion matrix. If given, this  |   should be 4- or 12-tuple containing floating point values.  |  :param dither: Dithering method, used when converting from  |   mode "RGB" to "P" or from "RGB" or "L" to "1".  |   Available methods are NONE or FLOYDSTEINBERG (default).  |  :param palette: Palette to use when converting from mode "RGB"  |   to "P". Available palettes are WEB or ADAPTIVE.  |  :param colors: Number of colors to use for the ADAPTIVE palette.  |   Defaults to 256.  |  :rtype: :py:class:`~PIL.Image.Image`  |  :returns: An :py:class:`~PIL.Image.Image` object.'''

原圖:

filepath = "./imgs/"imgdata = ImageToMatrix("./imgs/0001.jpg")print(type(imgdata))print(imgdata.shape)plt.imshow(imgdata) # 顯示圖片plt.axis('off')  # 不顯示坐標軸plt.show()

運行結果:

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 内丘县| 邢台市| 呈贡县| 青铜峡市| 常熟市| 武安市| 大余县| 河西区| 乐昌市| 汝州市| 沂水县| 栖霞市| 济源市| 阿合奇县| 清水县| 衡阳县| 通河县| 龙岩市| 三明市| 铁岭县| 美姑县| 扎赉特旗| 长葛市| 库车县| 濉溪县| 桃江县| 民县| 滨海县| 渝中区| 巴塘县| 新龙县| 安平县| 威远县| 民权县| 黑河市| 区。| 天津市| 邮箱| 科技| 邮箱| 科技|