最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python自動(dòng)裁剪圖像代碼分享

 更新時(shí)間:2017年11月25日 11:03:32   作者:b573  
這篇文章主要介紹了python自動(dòng)裁剪圖像代碼分享,具有一定參考價(jià)值,需要的朋友可以了解下。

本代碼可以幫你自動(dòng)剪切掉圖片的邊緣空白區(qū)域,如果你的圖片有大片空白區(qū)域(只要是同一顏色形成一定的面積就認(rèn)為是空白區(qū)域),下面的python代碼可以幫你自動(dòng)切除,如果是透明圖像,會(huì)自動(dòng)剪切大片的透明部分。

本代碼需要PIL模塊

pil相關(guān)介紹

PIL:Python Imaging Library,已經(jīng)是Python平臺(tái)事實(shí)上的圖像處理標(biāo)準(zhǔn)庫了。PIL功能非常強(qiáng)大,但API卻非常簡(jiǎn)單易用。

由于PIL僅支持到Python 2.7,加上年久失修,于是一群志愿者在PIL的基礎(chǔ)上創(chuàng)建了兼容的版本,名字叫Pillow,支持最新Python 3.x,又加入了許多新特性,因此,我們可以直接安裝使用Pillow。

import Image, ImageChops
 
def autoCrop(image,backgroundColor=None):
  '''Intelligent automatic image cropping.
    This functions removes the usless "white" space around an image.
    
    If the image has an alpha (tranparency) channel, it will be used
    to choose what to crop.
    
    Otherwise, this function will try to find the most popular color
    on the edges of the image and consider this color "whitespace".
    (You can override this color with the backgroundColor parameter) 
 
    Input:
      image (a PIL Image object): The image to crop.
      backgroundColor (3 integers tuple): eg. (0,0,255)
         The color to consider "background to crop".
         If the image is transparent, this parameters will be ignored.
         If the image is not transparent and this parameter is not
         provided, it will be automatically calculated.
 
    Output:
      a PIL Image object : The cropped image.
  '''
   
  def mostPopularEdgeColor(image):
    ''' Compute who's the most popular color on the edges of an image.
      (left,right,top,bottom)
       
      Input:
        image: a PIL Image object
       
      Ouput:
        The most popular color (A tuple of integers (R,G,B))
    '''
    im = image
    if im.mode != 'RGB':
      im = image.convert("RGB")
     
    # Get pixels from the edges of the image:
    width,height = im.size
    left  = im.crop((0,1,1,height-1))
    right = im.crop((width-1,1,width,height-1))
    top  = im.crop((0,0,width,1))
    bottom = im.crop((0,height-1,width,height))
    pixels = left.tostring() + right.tostring() + top.tostring() + bottom.tostring()
 
    # Compute who's the most popular RGB triplet
    counts = {}
    for i in range(0,len(pixels),3):
      RGB = pixels[i]+pixels[i+1]+pixels[i+2]
      if RGB in counts:
        counts[RGB] += 1
      else:
        counts[RGB] = 1  
     
    # Get the colour which is the most popular:    
    mostPopularColor = sorted([(count,rgba) for (rgba,count) in counts.items()],reverse=True)[0][1]
    return ord(mostPopularColor[0]),ord(mostPopularColor[1]),ord(mostPopularColor[2])
   
  bbox = None
   
  # If the image has an alpha (tranparency) layer, we use it to crop the image.
  # Otherwise, we look at the pixels around the image (top, left, bottom and right)
  # and use the most used color as the color to crop.
   
  # --- For transparent images -----------------------------------------------
  if 'A' in image.getbands(): # If the image has a transparency layer, use it.
    # This works for all modes which have transparency layer
    bbox = image.split()[list(image.getbands()).index('A')].getbbox()
  # --- For non-transparent images -------------------------------------------
  elif image.mode=='RGB':
    if not backgroundColor:
      backgroundColor = mostPopularEdgeColor(image)
    # Crop a non-transparent image.
    # .getbbox() always crops the black color.
    # So we need to substract the "background" color from our image.
    bg = Image.new("RGB", image.size, backgroundColor)
    diff = ImageChops.difference(image, bg) # Substract background color from image
    bbox = diff.getbbox() # Try to find the real bounding box of the image.
  else:
    raise NotImplementedError, "Sorry, this function is not implemented yet for images in mode '%s'." % image.mode
     
  if bbox:
    image = image.crop(bbox)
     
  return image
 
 
 
#范例:裁剪透明圖片:
im = Image.open('myTransparentImage.png')
cropped = autoCrop(im)
cropped.show()
 
#范例:裁剪非透明圖片
im = Image.open('myImage.png')
cropped = autoCrop(im)
cropped.show()

 總結(jié)

以上就是本文關(guān)于python自動(dòng)裁剪圖像代碼分享的全部內(nèi)容,希望對(duì)大家有所幫助。如有不足之處,歡迎留言指出。感興趣的朋友可以繼續(xù)參閱本站:

python圖像常規(guī)操作

python好玩的項(xiàng)目—色情圖片識(shí)別代碼分享

Python生成數(shù)字圖片代碼分享

相關(guān)文章

  • Python之string編碼問題

    Python之string編碼問題

    這篇文章主要介紹了Python之string編碼問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Python 如何批量更新已安裝的庫

    Python 如何批量更新已安裝的庫

    這篇文章主要介紹了Python 如何批量更新已安裝的庫,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • python連接手機(jī)自動(dòng)搜集螞蟻森林能量的實(shí)現(xiàn)代碼

    python連接手機(jī)自動(dòng)搜集螞蟻森林能量的實(shí)現(xiàn)代碼

    這篇文章主要介紹了python連接手機(jī)自動(dòng)搜集螞蟻森林能量的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • Python Pexpect庫的簡(jiǎn)單使用方法

    Python Pexpect庫的簡(jiǎn)單使用方法

    這篇文章主要介紹了Python Pexpect庫的簡(jiǎn)單使用方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • Python實(shí)現(xiàn)Appium端口檢測(cè)與釋放的實(shí)現(xiàn)

    Python實(shí)現(xiàn)Appium端口檢測(cè)與釋放的實(shí)現(xiàn)

    這篇文章主要介紹了Python實(shí)現(xiàn)Appium端口檢測(cè)與釋放的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Python?Matplotlib實(shí)現(xiàn)三維數(shù)據(jù)的散點(diǎn)圖繪制

    Python?Matplotlib實(shí)現(xiàn)三維數(shù)據(jù)的散點(diǎn)圖繪制

    這篇文章主要為大家詳細(xì)介紹了Python?Matplotlib實(shí)現(xiàn)三維數(shù)據(jù)的散點(diǎn)圖繪制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • 用Python做一個(gè)久坐提醒小助手的示例代碼

    用Python做一個(gè)久坐提醒小助手的示例代碼

    這篇文章主要介紹了用Python做一個(gè)久坐提醒小助手的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • python交互模式下輸入換行/輸入多行命令的方法

    python交互模式下輸入換行/輸入多行命令的方法

    這篇文章主要介紹了python交互模式下輸入換行/輸入多行命令的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • pycharm終端無法激活conda環(huán)境的解決辦法

    pycharm終端無法激活conda環(huán)境的解決辦法

    如果您在PyCharm終端中無法激活conda環(huán)境,可能是由于PyCharm沒有正確配置conda解釋器,這篇文章主要給大家介紹了關(guān)于pycharm終端無法激活conda環(huán)境的解決辦法,需要的朋友可以參考下
    2023-09-09
  • python根據(jù)開頭和結(jié)尾字符串獲取中間字符串的方法

    python根據(jù)開頭和結(jié)尾字符串獲取中間字符串的方法

    這篇文章主要介紹了python根據(jù)開頭和結(jié)尾字符串獲取中間字符串的方法,涉及Python操作字符串截取的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03

最新評(píng)論

舒兰市| 巢湖市| 江孜县| 基隆市| 昌乐县| 凤山县| 普格县| 卢氏县| 四子王旗| 盐源县| 无锡市| 峨山| 永登县| 仙桃市| 丰原市| 北碚区| 阿克陶县| 莆田市| 灌阳县| 丰原市| 大田县| 海南省| 大同县| 方城县| 桂林市| 甘肃省| 刚察县| 大厂| 榆树市| 东宁县| 涡阳县| 抚州市| 杂多县| 元江| 读书| 启东市| 盘山县| 营口市| 西藏| 乐亭县| 曲水县|