欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python讀取圖片任意范圍區(qū)域

 更新時(shí)間:2019年01月23日 10:56:18   作者:sinat_34022298  
這篇文章主要為大家詳細(xì)介紹了python讀取圖片任意范圍區(qū)域,以一維數(shù)組形式返回,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

使用python進(jìn)行圖片處理,現(xiàn)在需要讀出圖片的任意一塊區(qū)域,并將其轉(zhuǎn)化為一維數(shù)組,方便后續(xù)卷積操作的使用。
下面使用兩種方法進(jìn)行處理:

convert 函數(shù)

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

def 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 轉(zhuǎn)換,可以選擇不同的模式,下面有函數(shù)源碼具體說(shuō)明
 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)
 # 顯示從矩陣數(shù)據(jù)得到的圖片
 new_im.show()
 return new_data

def 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')  # 不顯示坐標(biāo)軸
plt.show()

運(yùn)行結(jié)果:

mpimg 函數(shù)

import matplotlib.pyplot as plt  # plt 用于顯示圖片
import matplotlib.image as mpimg  # mpimg 用于讀取圖片
import numpy as np

def readPic(picname, filename):
 img = mpimg.imread(picname)
 # 此時(shí) img 就已經(jīng)是一個(gè) np.array 了,可以對(duì)它進(jìn)行任意處理
 weight,height,n = img.shape  #(512, 512, 3)
 print("the original pic: \n" + str(img))

 plt.imshow(img)     # 顯示圖片
 plt.axis('off')     # 不顯示坐標(biāo)軸
 plt.show()

 # 取reshape后的矩陣的第一維度數(shù)據(jù),即所需要的數(shù)據(jù)列表
  img_reshape = img.reshape(1,weight*height*n)[0]
  print("the 1-d image data :\n "+str(img_reshape))

 # 截?。?00,300)區(qū)域的一小塊(12*12*3),將該區(qū)域的圖像數(shù)據(jù)轉(zhuǎn)換為一維數(shù)組
 img_cov = np.random.randint(1,2,(12,12,3))  # 這里使用np.ones()初始化數(shù)組,會(huì)出現(xiàn)數(shù)組元素為float類(lèi)型,使用np.random.randint確保其為int型
 for j in range(12):
  for i in range(12):
   img_cov[i][j] = img[300+i][300+j]

 img_reshape = img_cov.reshape(1,12*12*3)[0]
 print((img_cov))
 print(img_reshape)

 # 打印該12*12*3區(qū)域的圖像
 plt.imshow(img_cov) 
 plt.axis('off') 
 plt.show()

 # 寫(xiě)文件
 # open:以append方式打開(kāi)文件,如果沒(méi)找到對(duì)應(yīng)的文件,則創(chuàng)建該名稱(chēng)的文件
 with open(filename, 'a') as f:
  f.write(str(img_reshape))
 return img_reshape

if __name__ == '__main__':
 picname = './imgs/0001.jpg'
 readPic(picname, "data.py")

讀出的數(shù)據(jù)(12*12*3),每個(gè)像素點(diǎn)以R、G、B的順序排列,以及該區(qū)域顯示為圖片的效果:

參考:python 讀取并顯示圖片的兩種方法

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python中面向?qū)ο蟮淖⒁恻c(diǎn)概述總結(jié)

    python中面向?qū)ο蟮淖⒁恻c(diǎn)概述總結(jié)

    大家好,本篇文章主要講的是python中面向?qū)ο蟮淖⒁恻c(diǎn)概述總結(jié),感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下
    2022-02-02
  • python使用OS模塊操作系統(tǒng)接口及常用功能詳解

    python使用OS模塊操作系統(tǒng)接口及常用功能詳解

    os是?Python?標(biāo)準(zhǔn)庫(kù)中的一個(gè)模塊,提供了與操作系統(tǒng)交互的功能,在本節(jié)中,我們將介紹os模塊的一些常用功能,并通過(guò)實(shí)例代碼詳細(xì)講解每個(gè)知識(shí)點(diǎn)
    2023-06-06
  • Pyqt5自適應(yīng)布局實(shí)例

    Pyqt5自適應(yīng)布局實(shí)例

    今天小編就為大家分享一篇Pyqt5自適應(yīng)布局實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • 程序猿新手學(xué)習(xí)必備的Python工具整合

    程序猿新手學(xué)習(xí)必備的Python工具整合

    這篇文章主要介紹了程序猿新手必備的Python工具整合,Python 是一種開(kāi)源編程語(yǔ)言,用于 Web 編程、數(shù)據(jù)科學(xué)、人工智能和許多科學(xué)應(yīng)用
    2021-09-09
  • pandas多層索引的創(chuàng)建和取值以及排序的實(shí)現(xiàn)

    pandas多層索引的創(chuàng)建和取值以及排序的實(shí)現(xiàn)

    這篇文章主要介紹了pandas多層索引的創(chuàng)建和取值以及排序的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Python Json序列化與反序列化的示例

    Python Json序列化與反序列化的示例

    這篇文章主要介紹了Python Json序列化與反序列化的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Python實(shí)現(xiàn)登錄接口的示例代碼

    Python實(shí)現(xiàn)登錄接口的示例代碼

    本篇文章主要介紹了Python實(shí)現(xiàn)登錄接口的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • Python實(shí)現(xiàn)json對(duì)值進(jìn)行模糊搜索的示例詳解

    Python實(shí)現(xiàn)json對(duì)值進(jìn)行模糊搜索的示例詳解

    我經(jīng)常使用json進(jìn)行存儲(chǔ)配置,于是常常遇到這樣的問(wèn)題:如果想要對(duì)某個(gè)數(shù)組里的值進(jìn)行模糊搜索,同時(shí)輸出相關(guān)的其他數(shù)組相同位置的的值該如何實(shí)現(xiàn)呢?本文就來(lái)和大家詳細(xì)聊聊
    2023-01-01
  • django views重定向到帶參數(shù)的url

    django views重定向到帶參數(shù)的url

    這篇文章主要介紹了django views重定向到帶參數(shù)的url,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-03-03
  • Python中os模塊的12種用法總結(jié)

    Python中os模塊的12種用法總結(jié)

    OS?(?Operating?System?操作系統(tǒng)?)?操作系統(tǒng)模塊;它是屬于python的標(biāo)準(zhǔn)庫(kù),常用于處理文件和目錄(文件夾)的操作。本文為大家總結(jié)了這個(gè)模塊的12種用法,希望有所幫助
    2022-08-08

最新評(píng)論