python不改变像素大小修改存储空间

Python010

python不改变像素大小修改存储空间,第1张

Python本身不支持改变像素大小来修改存储空间,但是可以使用第三方库来实现这一功能。Pillow是一个开源的Python图像处理库,可以用来修改图片的大小,从而减少存储空间。

使用Pillow库,可以使用resize()函数来调整图片的大小:

from PIL import Image

img = Image.open('image.jpg')

img = img.resize((200, 200)) # 调整图片大小为200x200像素

img.save('image_resized.jpg') # 保存调整后的图片

先对图片进行二值化处理。

由于这些格子大小均匀,所以可以使用常规的除法,先裁剪出第一个格子:

e=a[:,:,0].shape

f=a[0:int(e[0]/9),0:int(e[1]/9)]

io.imsave('00.png',f)

下图是等比例放大的效果图。

第一列第二个格子:

m=1

n=0

f=a[int(m*e[0]/9):int((m+1)*e[0]/9),int(n*e[1]/9):int((n+1)*e[1]/9)]

第一行第二个格子:

m=0

n=1

f=a[int(m*e[0]/9):int((m+1)*e[0]/9),int(n*e[1]/9):int((n+1)*e[1]/9)]

这样,用for循环,可以实现全部裁剪:

for m in range(9):

for n in range(9):

f=a[int(m*e[0]/9):int((m+1)*e[0]/9),int(n*e[1]/9):int((n+1)*e[1]/9)]

io.imsave('0/'+str(m)+'-'+str(n)+'.png',f)

去除边界的黑框,只保留数字或空白:

for m in range(9):

for n in range(9):

f=a[int(m*e[0]/9)+10:int((m+1)*e[0]/9)-10,int(n*e[1]/9)+10:int((n+1)*e[1]/9)-10]

io.imsave('0/'+str(m)+'-'+str(n)+'.png',f)

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 use requests:

from PIL import Imageimport requests

im = Image.open(requests.get(url, stream=True).raw)

[python] view plain copy

[html] view plain copy

#coding:utf-8

'''

python图片处理

'''

import Image as image

#等比例压缩图片

def resizeImg(**args):

args_key = {'ori_img':'','dst_img':'','dst_w':'','dst_h':'','save_q':75}

arg = {}

for key in args_key:

if key in args:

arg[key] = args[key]

im = image.open(arg['ori_img'])

ori_w,ori_h = im.size

widthRatio = heightRatio = None

ratio = 1

if (ori_w and ori_w > arg['dst_w']) or (ori_h and ori_h > arg['dst_h']):

if arg['dst_w'] and ori_w > arg['dst_w']:

widthRatio = float(arg['dst_w']) / ori_w #正确获取小数的方式

if arg['dst_h'] and ori_h > arg['dst_h']:

heightRatio = float(arg['dst_h']) / ori_h

if widthRatio and heightRatio:

if widthRatio < heightRatio:

ratio = widthRatio

else:

ratio = heightRatio

if widthRatio and not heightRatio:

ratio = widthRatio

if heightRatio and not widthRatio:

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: use nearest neighbour

BILINEAR: linear interpolation in a 2x2 environment

BICUBIC:cubic spline interpolation in a 4x4 environment

ANTIALIAS:best down-sizing filter

'''

#裁剪压缩图片

def clipResizeImg(**args):

args_key = {'ori_img':'','dst_img':'','dst_w':'','dst_h':'','save_q':75}

arg = {}

for key in args_key:

if key in args:

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 #原高宽比

if ori_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中的imagecopy方法大为不一样

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'])

#水印(这里仅为图片水印)

def waterMark(**args):

args_key = {'ori_img':'','dst_img':'','mark_img':'','water_opt':''}

arg = {}

for key in args_key:

if key in args:

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)