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

python自動裁剪圖像代碼分享

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

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

本代碼需要PIL模塊

pil相關介紹

PIL:Python Imaging Library,已經是Python平臺事實上的圖像處理標準庫了。PIL功能非常強大,但API卻非常簡單易用。

由于PIL僅支持到Python 2.7,加上年久失修,于是一群志愿者在PIL的基礎上創(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()

 總結

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

python圖像常規(guī)操作

python好玩的項目—色情圖片識別代碼分享

Python生成數字圖片代碼分享

相關文章

  • anaconda的安裝和配置環(huán)境及導入pycharm的方法

    anaconda的安裝和配置環(huán)境及導入pycharm的方法

    這篇文章主要介紹了anaconda的安裝和配置環(huán)境及導入pycharm的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • pycharm激活碼2020最新分享適用pycharm2020最新版親測可用

    pycharm激活碼2020最新分享適用pycharm2020最新版親測可用

    這篇文章主要介紹了pycharm激活碼2020最新分享適用pycharm2020最新版親測可用,同時也支持Intellij IDEA激活碼,PHPStorm激活碼大家可以放心使用需要的朋友可以參考下
    2020-11-11
  • python數組復制拷貝的實現方法

    python數組復制拷貝的實現方法

    這篇文章主要介紹了python數組復制拷貝的實現方法,實例分析了Python數組傳地址與傳值兩種復制拷貝的使用技巧,需要的朋友可以參考下
    2015-06-06
  • 使用django的objects.filter()方法匹配多個關鍵字的方法

    使用django的objects.filter()方法匹配多個關鍵字的方法

    今天小編就為大家分享一篇使用django的objects.filter()方法匹配多個關鍵字的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Python用61行代碼實現圖片像素化的示例代碼

    Python用61行代碼實現圖片像素化的示例代碼

    這篇文章主要介紹了Python用61行代碼實現圖片像素化的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-12-12
  • Python爬取網易云音樂上評論火爆的歌曲

    Python爬取網易云音樂上評論火爆的歌曲

    最近跟著網上教程學著用python爬取問題,于是就想試著扒一扒Python爬取網易云音樂上評論火爆的歌曲,下面這篇文章就主要介紹了利用Python如何爬取網易云音樂上那些評論火爆的歌曲,需要的朋友可以參考借鑒,一起來看看吧。
    2017-01-01
  • 淺談優(yōu)化Django ORM中的性能問題

    淺談優(yōu)化Django ORM中的性能問題

    這篇文章主要介紹了淺談優(yōu)化Django ORM中的性能問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 一文詳細NumPy中np.empty的用法

    一文詳細NumPy中np.empty的用法

    np.empty是NumPy庫中一個強大但潛在危險的工具,本文主要介紹了一文詳細NumPy中np.empty的用法,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • Python fileinput模塊使用實例

    Python fileinput模塊使用實例

    這篇文章主要介紹了Python fileinput模塊使用實例,本文講解了典型用法、基本格式、默認格式、常用函數和常見例子等內容,需要的朋友可以參考下
    2015-06-06
  • Python異常處理操作實例詳解

    Python異常處理操作實例詳解

    這篇文章主要介紹了Python異常處理操作,結合實例形式分析了Python異常處理的相關原理、操作語句與使用技巧,需要的朋友可以參考下
    2018-05-05

最新評論