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

首頁 > 編程 > Python > 正文

python放大圖片和畫方格實現算法

2020-01-04 15:28:29
字體:
來源:轉載
供稿:網友

本文實例為大家分享了python放大圖片和畫方格的具體代碼,供大家參考,具體內容如下

1、Python 放大圖片和畫方格算法

#!C:/Python27 # -*- coding: utf-8 -*-  import os  import sys  from PIL import Image,ImageDraw      def make_doc_data(lf):      #li, ri = make_regalur_image(Image.open(lf)), make_regalur_image(Image.open(rf))#兩張圖片方法   li = Image.open(lf)      size = (256, 256)   #幾何轉變,全部轉化為256*256像素大小   li =li.resize(size).convert('RGB')      li.save(lf + '_regalur.png')#轉換圖片格式:img.save('file.jpg'),保存臨時的      #ri.save(rf + '_regalur.png')#img對象到硬盤      fd = open('stat.csv', 'w')#stat模塊是做隨機變量統計的,stat用來計算隨機變量的期望值和方差      #這句是關鍵啊,把histogram的結果進行map處理    #fd.write('/n'.join(l + ',' + r for l, r in zip(map(str, li.histogram()), map(str, ri.histogram()))))     fd.write(','.join(map(str, li.histogram())))    fd.close()     li = li.convert('RGB') #與save對象,這是轉換格式      draw = ImageDraw.Draw(li)      for i in xrange(0, 256, 64):      draw.line((0, i, 256, i), fill = '#ff0000')      draw.line((i, 0, i, 256), fill = '#ff0000')        #從始至終劃線!通過把每一列刷成紅色,來進行顏色的隨機分布劃分        #用法:pygame.draw.line(Surface, color, start_pos, end_pos, width=1)        li.save(lf + '_lines.png')       make_doc_data('testpic/1370.bmp') 

 2、放大縮小圖片的幾種方法

#!C:/Python27 #coding=utf-8  import pytesseract from pytesser import * from PIL import Image,ImageEnhance,ImageFilter import os import fnmatch import re,time  import urllib, random  #修改文件名 #os.rename("E:/pythonScript/Model/font/2.bmp","E:/pythonScript/Model/font/dock2.bmp")   def CutImg():      img = Image.open('.//6907.jpg').convert('L')    print img.size    w, h = img.size        #rowheight = h // rownum       #colwidth = w // colnum       #imgry.show()   j = 10   for i in range(4):           x = 10 + i*24 #驗證碼的x,y坐標      y = 6       img.crop((x-4, y,x+6, y+14)).save("pic/%d.bmp" % j)       print "j=",j       j += 1   img.close()   infile = ('.//testpic//001n.bmp')  outfile = ('.//testpic//001n.png')     def fixed_size(infile):      """按照固定尺寸處理圖片"""    im = Image.open(infile)    size = (256, 256)      im2 =im.resize(size).convert('RGB')   out = im2.resize(size,Image.ANTIALIAS)    out.save(outfile)   print u"/n按固定尺寸放大圖片,處理已完成" def resize_by_width(w_divide_h):      """按照寬度進行所需比例縮小"""    im = Image.open(infile)   print im.size      (x, y) = im.size     x_s = x   print x_s   y_s = x/w_divide_h #w_divide_h > x   print y_s   out = im.resize((x_s, y_s), Image.ANTIALIAS)     out.save(outfile)     def resize_by_height(w_divide_h):      """按照高度進行所需比例縮放"""    im = Image.open(infile)   (x, y) = im.size   print im.size   x_s = y*w_divide_h    y_s = y    out = im.resize((x_s, y_s), Image.ANTIALIAS)     out.save(outfile,quality = 95,dpi=(72, 72)) def cut_by_ratio(width, height):      """按照圖片長寬比進行分割"""      im = Image.open(infile)      width = float(width)      height = float(height)      (x, y) = im.size      if width > height:        region = (0, int((y-(y * (height / width)))/2), x, int((y+(y * (height / width)))/2))      elif width < height:        region = (int((x-(x * (width / height)))/2), 0, int((x+(x * (width / height)))/2), y)      else:        region = (0, 0, x, y)        #裁切圖片      crop_img = im.crop(region)      #保存裁切后的圖片      crop_img.save(outfile) def Lager(size):   im = Image.open(infile)   im_resized=im.resize(size, Image.ANTIALIAS)   im_resized.save(outfile,quality = 95,dpi=(72, 72)) def mohuimg():   """   模糊圖片   """   im = Image.open(infile)   im2 = im.filter(ImageFilter.BLUR)   im2.save(outfile) """ 多種尺寸icon的存儲 """ image_size = [512,250,144,140,128,120,108,100,88,72,48,32,28] def create_icon():    for size in image_size:      '''''pri_image = Image.open("icon.png")      pri_image.thumbnail((size,size))      image_name = "icon_%d.png"%(size)      pri_image.save(image_name)'''      pri_image = Image.open(infile)      pri_image.resize((size,size),Image.ANTIALIAS ).save("testpic/icom_%d.png"%(size))       fixed_size(infile) #resize_by_width(10) #resize_by_height(1) #cut_by_ratio(50,20) #Lager(256)   #mohuimg() #create_icon() #CutImg() 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 象州县| 蚌埠市| 梧州市| 车险| 普格县| 胶州市| 吉木乃县| 长乐市| 西宁市| 长兴县| 肥西县| 阜城县| 石城县| 巩义市| 屏南县| 台中县| 安岳县| 思茅市| 大竹县| 同仁县| 通城县| 冀州市| 平果县| 郎溪县| 海伦市| 潮州市| 汉阴县| 台东市| 定陶县| 成都市| 佛坪县| 巴楚县| 资兴市| 内乡县| 金乡县| 磴口县| 鹿邑县| 东安县| 乐平市| 康保县| 广河县|