pythonresizeimage
Ⅰ 如何利用python实现图片转字符画详解
# -*- coding: utf-8 -*-from PIL import Image
codeLib = '''@B%8&WM#*/\|()1{}[]?-_+~<>i!lI;:,"^`'. '''#生成字符画所需的字符集count = len(codeLib)def transform1(image_file):
image_file = image_file.convert("L")#转换为黑白图片,参数"L"表示黑白模式
codePic = ''
for h in range(0,image_file.size[1]): #size属性表示图片的分辨率,'0'为横向大小,'1'为纵向
for w in range(0,image_file.size[0]):
gray = image_file.getpixel((w,h)) #返回指定位置的像素,如果所打开的图像是多层次的图片,那这个方法就返回一个元组
codePic = codePic + codeLib[int(((count-1)*gray)/256)]#建立灰度与字符集的映射
codePic = codePic+'\r\n'
return codePicdef transform2(image_file):
codePic = ''
for h in range(0,image_file.size[1]): for w in range(0,image_file.size[0]):
g,r,b = image_file.getpixel((w,h))
gray = int(r* 0.299+g* 0.587+b* 0.114)
codePic = codePic + codeLib[int(((count-1)*gray)/256)]
codePic = codePic+'\r\n'
return codePic
fp = open(u'暴走.jpg','rb')
image_file = Image.open(fp)
image_file=image_file.resize((int(image_file.size[0]*0.75), int(image_file.size[1]*0.5)))#调整图片大小print u'Info:',image_file.size[0],' ',image_file.size[1],' ',count
tmp = open('tmp.txt','w')
tmp.write(transform1(image_file))
tmp.close()
Ⅱ Python 读取文件夹将里面的图片处理成想要的大小并保存在个指定位置
fromPILimportImage
importos.path
importglob
defconvertjpg(jpgfile,outdir,width=1280,height=720):
img=Image.open(jpgfile)
new_img=img.resize((width,height),Image.BILINEAR)
new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
forjpgfileinglob.glob("D:/python/*.jpg"):
convertjpg(jpgfile,"D:/newfile")
convertjpg调用时可以有四个参数,如convertjpg(jpgfile,"D:/newfile",800,600)
Image open了jpg用完后要不要close?
Ⅲ python PIL如何才能把图片修改成正方形或者任意尺寸而不产生挤压
改变图像尺寸有两类方法:
一是缩放(resize),即重采样。这时,如果图像纵横比发生变化就会导致“挤压”。
二是裁剪(crop)。当然图只能越裁越小;不过可以配合缩放,先放大再裁剪。
既然题主要求不能“挤压”,那就只能裁剪了。函数名我给出来了,具体用法题主自己研究。
Ⅳ 请问可以用python实现将大图片变成小图片处理吗,这边要做一个图像识别,太大的分辨率运行慢
python有一个图像处理库——PIL,可以处理图像文件。PIL提供了功能丰富的方法,比如格式转换、旋转、裁剪、改变尺寸、像素处理、图片合并等等等等,非常强大。
举个简单的例子,调整图片的大小:
12345678910111213141516171819
import Image infile = 'D:\\original_img.jpg'outfile = 'D:\\adjust_img.jpg'im = Image.open(infile)(x,y) = im.size #read image sizex_s = 250 #define standard widthy_s = y * x_s / x #calc height based on standard widthout = im.resize((x_s,y_s),Image.ANTIALIAS) #resize image with high-qualityout.save(outfile) print 'original size: ',x,yprint 'adjust size: ',x_s,y_s '''OUTPUT:original size: 500 358adjust size: 250 179'''
Ⅳ python用graphics中的image图片保存与调用
from PIL import Image
import os.path
import glob
def convertjpg(jpgfile,outdir,width=1280,height=720):
img=Image.open(jpgfile)
new_img=img.resize((width,height),Image.BILINEAR)
new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
for jpgfile in glob.glob("D:/python/*.jpg"):
convertjpg(jpgfile,"D:/newfile")
convertjpg调用时可以有四个参数,如convertjpg(jpgfile,"D:/newfile",800,600)
Image open了jpg用完后要不要close?
Ⅵ opencv python 调用resize函数时一直报错
你可以重新看一下opencv 的文档,重新理解一下resize函数。resize函数提供了两种方法来修改图像的大小,一种是提供一个目标图像大小(dsize)这和目标大小包含两个维度:width和heigth。换句话说就是我要告诉resize函数我要将图片变为dsize这么大/小。另一种方式是通过两个参数fx,fy,这两个参数是缩放比例,分别表示对目标图像的长宽进行缩放的比例。
Ⅶ python可以用来处理图像吗
可以的,
PythonWare公司提供了免费的Python图像处理工具包PIL(Python Image Library),该软件包提供了基本的图像处理功能,如:
改变图像大小,旋转图像,图像格式转换,色场空间转换,图像增强,直方图处理,插值和滤波等等。虽然在这个软件包上要实现类似MATLAB中的复杂的图像处理算法并不太适合,但是Python的快速开发能力以及面向对象等等诸多特点使得它非常适合用来进行原型开发。
在PIL中,任何一副图像都是用一个Image对象表示,而这个类由和它同名的模块导出,因此,最简单的形式是这样的:
import Image img = Image.open(“dip.jpg”)
注意:第一行的Image是模块名;第二行的img是一个Image对象;
Image类是在Image模块中定义的。关于Image模块和Image类,切记不要混淆了。现在,我们就可以对img进行各种操作了,所有对img的
操作最终都会反映到到dip.img图像上。
PIL提供了丰富的功能模块:Image,ImageDraw,ImageEnhance,ImageFile等等。最常用到的模块是
Image,ImageDraw,ImageEnhance这三个模块。下面我对此分别做一介绍。关于其它模块的使用请参见说明文档.有关PIL软件包和
相关的说明文档可在PythonWare的站点www.Pythonware.com上获得。
Image模块:
Image模块是PIL最基本的模块,其中导出了Image类,一个Image类实例对象就对应了一副图像。同时,Image模块还提供了很多有用的函数。
(1)打开一文件:
import Image img = Image.open(“dip.jpg”)
这将返回一个Image类实例对象,后面的所有的操作都是在img上完成的。
(2)调整文件大小:
import Image img = Image.open("img.jpg") new_img = img.resize
((128,128),Image.BILINEAR) new_img.save("new_img.jpg")
原来的图像大小是256x256,现在,保存的new_img.jpg的大小是128x128。
就是这么简单,需要说明的是Image.BILINEAR指定采用双线性法对像素点插值。
在批处理或者简单的Python图像处理任务中,采用Python和PIL(Python Image Library)的组合来完成图像处理任务是一个很不错的选择。设想有一个需要对某个文件夹下的所有图像将对比度提高2倍的任务。用Python来做将是十分简单的。当然,我也不得不承认Python在图像处理方面的功能还比较弱,显然还不适合用来进行滤波、特征提取等等一些更为复杂的应用。我个人的观点是,当你要实现这些“高级”的算法的时候,好吧,把它交给MATLAB去完成。但是,如果你面对的只是一个通常的不要求很复杂算法的图像处理任务,那么,Python图像处理应该才是你的最佳搭档。
Ⅷ python 创建固定大小的图片
不知道你对图片的格式有没有要求,如果是bmp的话是没有压缩的。也就是说大小和颜色深度决定了图片的大小。这样也比较容易控制。当然也就不能不改size随便调节文件的大小。
如果是jpg的话,可以通过改变图片的质量来调节文件的大小。
比如
im = Image.open("aa.JPG")
print im.format, im.size, im.mode
print im.size[0]
im.resize((720,540), Image.ANTIALIAS).save('a.jpg', quality = 95)
你也可以做一个循环,对生成的文件大小与目标大小做比较,直到满足条件为止。
只是给个思路,也许帮不上忙。
Ⅸ python中PLE调整图片大小,等比例压缩文件,怎么写代码
How do I read image data from a URL in Python?
importosimportImagefileName='c:/py/jb51.jpg'fp=open(fileName,'rb')im=Image.open(fp)fp.close()x,y=im.sizeifx <300or y <300:os.remove(fileName)from PIL import Imageimport requestsimport numpy as npfrom StringIO import StringIOresponse = requests.get(url)img = np.array(Image.open(StringIO(response.content)))
from PIL import Imageimport urllib2
im = Image.open(urllib2.urlopen(url))
or if you userequests:
from PIL import Imageimport requests
im = Image.open(requests.get(url, stream=True).raw)
[python] view plain
#coding:utf-8
'''
python图片处理
'''
importImageasimage
#等比例压缩图片
defresizeImg(**args):
args_key={'ori_img':'','dst_img':'','dst_w':'','dst_h':'','save_q':75}
arg={}
forkeyinargs_key:
ifkeyinargs:
arg[key]=args[key]
im=image.open(arg['ori_img'])
ori_w,ori_h=im.size
widthRatio=heightRatio=None
ratio=1
if(ori_wandori_w>arg['dst_w'])or(ori_handori_h>arg['dst_h']):
ifarg['dst_w']andori_w>arg['dst_w']:
widthRatio=float(arg['dst_w'])/ori_w#正确获取小数的方式
ifarg['dst_h']andori_h>arg['dst_h']:
heightRatio=float(arg['dst_h'])/ori_h
ifwidthRatioandheightRatio:
ifwidthRatio<heightRatio:
ratio=widthRatio
else:
ratio=heightRatio
ifwidthRatioandnotheightRatio:
ratio=widthRatio
ifheightRatioandnotwidthRatio:
ratio=heightRatio
newWidth=int(ori_w*ratio)
newHeight=int(ori_h*ratio)
else:
newWidth=ori_w
newHeight=ori_h
im.resize((newWidth,newHeight),image.ANTIALIAS).save(arg['dst_img'],quality=arg['save_q'])
'''
image.ANTIALIAS还有如下值:
NEAREST:usenearestneighbour
BILINEAR:
BICUBIC:
ANTIALIAS:bestdown-sizingfilter
'''
#裁剪压缩图片
defclipResizeImg(**args):
args_key={'ori_img':'','dst_img':'','dst_w':'','dst_h':'','save_q':75}
arg={}
forkeyinargs_key:
ifkeyinargs:
arg[key]=args[key]
im=image.open(arg['ori_img'])
ori_w,ori_h=im.size
dst_scale=float(arg['dst_h'])/arg['dst_w']#目标高宽比
ori_scale=float(ori_h)/ori_w#原高宽比
ifori_scale>=dst_scale:
#过高
width=ori_w
height=int(width*dst_scale)
x=0
y=(ori_h-height)/3
else:
#过宽
height=ori_h
width=int(height*dst_scale)
x=(ori_w-width)/2
y=0
#裁剪
box=(x,y,width+x,height+y)
#这里的参数可以这么认为:从某图的(x,y)坐标开始截,截到(width+x,height+y)坐标
#所包围的图像,crop方法与php中的image方法大为不一样
newIm=im.crop(box)
im=None
#压缩
ratio=float(arg['dst_w'])/width
newWidth=int(width*ratio)
newHeight=int(height*ratio)
newIm.resize((newWidth,newHeight),image.ANTIALIAS).save(arg['dst_img'],quality=arg['save_q'])
#水印(这里仅为图片水印)
defwaterMark(**args):
args_key={'ori_img':'','dst_img':'','mark_img':'','water_opt':''}
arg={}
forkeyinargs_key:
ifkeyinargs:
arg[key]=args[key]
im=image.open(arg['ori_img'])
ori_w,ori_h=im.size
mark_im=image.open(arg['mark_img'])
mark_w,mark_h=mark_im.size
option={'leftup':(0,0),'rightup':(ori_w-mark_w,0),'leftlow':(0,ori_h-mark_h),
'rightlow':(ori_w-mark_w,ori_h-mark_h)
}
im.paste(mark_im,option[arg['water_opt']],mark_im.convert('RGBA'))
im.save(arg['dst_img'])
#Demon
#源图片
ori_img='D:/tt.jpg'
#水印标
mark_img='D:/mark.png'
#水印位置(右下)
water_opt='rightlow'
#目标图片
dst_img='D:/python_2.jpg'
#目标图片大小
dst_w=94
dst_h=94
#保存的图片质量
save_q=35
#裁剪压缩
clipResizeImg(ori_img=ori_img,dst_img=dst_img,dst_w=dst_w,dst_h=dst_h,save_q=save_q)
#等比例压缩
#resizeImg(ori_img=ori_img,dst_img=dst_img,dst_w=dst_w,dst_h=dst_h,save_q=save_q)
#水印
#waterMark(ori_img=ori_img,dst_img=dst_img,mark_img=mark_img,water_opt=water_opt)
[html] view plain