python曲线图局部放⼤_python放⼤图⽚和画⽅格实现算法本⽂实例为⼤家分享了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
resizebyimport 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
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 = size(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_size(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)
size((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()
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持脚本之家。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论