用python解压图片并打印代码

Python016

用python解压图片并打印代码,第1张

import zipfile

# 传入压缩文件zfile.zip获取相关信息

zip_file = zipfile.ZipFile('zfile.zip')

# 获取压缩文件中的内容

f_content = zip_file.namelist()

# 压缩前的大小

f_size = zip_file.getinfo('zfile/a.txt').file_size

# 压缩后的大小

c_size = zip_file.getinfo('zfile/a.txt').compress_size

ZipFile 对象有一个 namelist()方法,返回 ZIP 文件中包含的所有文件和文件夹 的字符串的列表。这些字符串可以传递给 ZipFile 对象的 getinfo()方法,返回一个关 于特定文件的 ZipInfo 对象。ZipInfo 对象有自己的属性,诸如表示字节数的 file_size 和 compress_size,它们分别表示原来文件大小和压缩后文件大小。ZipFile 对象表示 整个归档文件,而 ZipInfo 对象则保存该归档文件中每个文件的有用信息。

从 ZIP 文件中解压缩

ZipFile 对象的 extractall()方法从 ZIP 文件中解压缩所有文件和文件夹,放到当 前工作目录中。

import zipfile

zip_file = zipfile.ZipFile('zfile.zip')

# 解压

zip_extract = zip_file.extractall()

zip_extract.close()

运行这段代码后, example.zip 的内容将被解压缩到 C:\。 或者, 你可以向 extractall()传递的一个文件夹名称,它将文件解压缩到那个文件夹,而不是当前工作 目录。如果传递给 extractall()方法的文件夹不存在,它会被创建。例如,如果你用 exampleZip.extractall('C:\ delicious')取代处的调用,代码就会从 example.zip 中解压 缩文件,放到新创建的 C:\delicious 文件夹中。

ZipFile 对象的 extract()方法从 ZIP 文件中解压缩单个文件。

创建和添加到 ZIP 文件

要创建你自己的压缩 ZIP 文件,必须以“写模式”打开 ZipFile 对象,即传入'w' 作为第二个参数(这类似于向 open()函数传入'w',以写模式打开一个文本文件)。

如果向 ZipFile 对象的 write()方法传入一个路径,Python 就会压缩该路径所指 的文件,将它加到 ZIP 文件中。write()方法的第一个参数是一个字符串,代表要添 加的文件名。第二个参数是“压缩类型”参数,它告诉计算机使用怎样的算法来压 缩文件。可以总是将这个值设置为 zipfile.ZIP_DEFLATED(这指定了 deflate 压缩 算法,它对各种类型的数据都很有效)。

import zipfile

zip_file = zipfile.ZipFile('new.zip','w')

# 把zfile整个目录下所有内容,压缩为new.zip文件

zip_file.write('zfile',compress_type=zipfile.ZIP_DEFLATED)

# 把c.txt文件压缩成一个压缩文件

# zip_file.write('c.txt',compress_type=zipfile.ZIP_DEFLATED)

zip_file.close()

这段代码将创建一个新的 ZIP 文件,名为 new.zip,它包含 spam.txt 压缩后的内容。

要记住,就像写入文件一样,写模式将擦除 ZIP 文件中所有原有的内容。如果 只是希望将文件添加到原有的 ZIP 文件中,就要向 zipfile.ZipFile()传入'a'作为第二 个参数,以追加模式打开 ZIP 文件。

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)

首先需要安装 PIL 库

然后 from PIL import Image

im = Image.open(pash)

im.thumbnail((new_width, new_hight))

im.save(path)