python放大圖片和畫(huà)方格實(shí)現(xiàn)算法
本文實(shí)例為大家分享了python放大圖片和畫(huà)方格的具體代碼,供大家參考,具體內(nèi)容如下
1、Python 放大圖片和畫(huà)方格算法
#!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)
#幾何轉(zhuǎn)變,全部轉(zhuǎn)化為256*256像素大小
li =li.resize(size).convert('RGB')
li.save(lf + '_regalur.png')#轉(zhuǎn)換圖片格式:img.save('file.jpg'),保存臨時(shí)的
#ri.save(rf + '_regalur.png')#img對(duì)象到硬盤(pán)
fd = open('stat.csv', 'w')#stat模塊是做隨機(jī)變量統(tǒng)計(jì)的,stat用來(lái)計(jì)算隨機(jī)變量的期望值和方差
#這句是關(guān)鍵啊,把histogram的結(jié)果進(jìn)行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對(duì)象,這是轉(zhuǎn)換格式
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')
#從始至終劃線!通過(guò)把每一列刷成紅色,來(lái)進(jìn)行顏色的隨機(jī)分布劃分
#用法: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 #驗(yàn)證碼的x,y坐標(biāo)
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):
"""按照寬度進(jìn)行所需比例縮小"""
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):
"""按照高度進(jìn)行所需比例縮放"""
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):
"""按照?qǐng)D片長(zhǎng)寬比進(jìn)行分割"""
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的存儲(chǔ)
"""
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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python+tkinter編寫(xiě)電腦桌面放大鏡程序?qū)嵗a
- 教你使用python畫(huà)一朵花送女朋友
- Python使用matplotlib繪制動(dòng)畫(huà)的方法
- 使用Python編寫(xiě)簡(jiǎn)單的畫(huà)圖板程序的示例教程
- python使用reportlab畫(huà)圖示例(含中文漢字)
- Python使用matplotlib實(shí)現(xiàn)在坐標(biāo)系中畫(huà)一個(gè)矩形的方法
- Python實(shí)現(xiàn)在matplotlib中兩個(gè)坐標(biāo)軸之間畫(huà)一條直線光標(biāo)的方法
- python計(jì)算圓周長(zhǎng)、面積、球體體積并畫(huà)出圓
- 利用Python畫(huà)ROC曲線和AUC值計(jì)算
- 從零學(xué)python系列之教你如何根據(jù)圖片生成字符畫(huà)
相關(guān)文章
python爬取網(wǎng)站數(shù)據(jù)保存使用的方法
這篇文章主要介紹了使用Python從網(wǎng)上爬取特定屬性數(shù)據(jù)保存的方法,其中解決了編碼問(wèn)題和如何使用正則匹配數(shù)據(jù)的方法,詳情看下文2013-11-11
Python實(shí)現(xiàn)將Excel某范圍單元格內(nèi)容截圖
Openpyxl是一個(gè)強(qiáng)大的Python庫(kù),主要用于讀取、寫(xiě)入和操作Excel文件,本文將使用Openpyxl實(shí)現(xiàn)將Excel某范圍單元格內(nèi)容截圖,感興趣的可以了解下2024-11-11
Python網(wǎng)絡(luò)編程之使用TCP方式傳輸文件操作示例
這篇文章主要介紹了Python網(wǎng)絡(luò)編程之使用TCP方式傳輸文件操作,結(jié)合實(shí)例形式分析了使用socket模塊進(jìn)行tcp協(xié)議下文件傳輸?shù)脑硪约胺?wù)器端、客戶端相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-11-11
django框架之cookie/session的使用示例(小結(jié))
這篇文章主要介紹了django框架之cookie/session的使用示例(小結(jié)),詳細(xì)的介紹了cookie和session技術(shù)的接口獲取等問(wèn)題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Python實(shí)現(xiàn)判斷一行代碼是否為注釋的方法
今天小編就為大家分享一篇Python實(shí)現(xiàn)判斷一行代碼是否為注釋的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
使用TensorFlow搭建一個(gè)全連接神經(jīng)網(wǎng)絡(luò)教程
今天小編就為大家分享一篇使用TensorFlow搭建一個(gè)全連接神經(jīng)網(wǎng)絡(luò)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02

