㈠ python pil 怎么设定图片的dpi
在python ide 可以这样处理:
im.resize(box, Image.ANTIALIAS)
im.save(path, 'JPEG', quality = 95)
DPI貌似不能设,只能设定图片质量,请试试看吧。
㈡ python opencv中imshow输出图像太大,如何调整输出尺寸
通过resize重置图片尺寸
参数:图片,输出图片尺寸(一般我不用这个,我传None),宽的比例,高的比例
例子:将图片img,缩小一半显示,那么宽高为原尺寸的0.5倍
img = cv2.resize(img,None,fx=0.5,fy=0.5)
设置完尺寸后再显示
cv2.imshow('img',img)
cv2.waitKey(0)
.......
希望我的回答对你有帮助!
㈢ 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处理图片。怎么获取图片的像素数据
importimage
importsys
img=image.open("图片位置")
width=img.size[0]
height=img.size[1]
forwinrange(width):
forhinrange(height):
pixel=img.getpixel(w,h)
printpixel
#width,height是图片的宽度与长度
#pixel是像素值
㈤ python中image怎样缩小图像
首先需要安装 PIL 库
然后 from PIL import Image
im = Image.open(pash)
im.thumbnail((new_width, new_hight))
im.save(path)
㈥ python PIL如何才能把图片修改成正方形或者任意尺寸而不产生挤压
使用裁剪(crop)
img.crop(0,0,w,h)
㈦ python 怎么通过 PIL 获得图片尺寸
fromPILimportImage
pic_path='logo_white_ee663702.png'
im=Image.open(pic_path)
#这样就可以了
x,y=im.size
print"size:",x,y
以下为 PIL Image.py 源码
classImage:
format=None
format_description=None
def__init__(self):
#FIXME:take"new"parameters/otherimage?
#FIXME:?
self.im=None
self.mode=""
self.size=(0,0)
self.palette=None
self.info={}
self.category=NORMAL
self.readonly=0
#可以看到,初始化时有属性size
㈧ 请问可以用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中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
[html] 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)