pythoncstringio
1. python如何获取图片长宽等信息
使用PIL模块,windows安装包下载:
http://www.pythonware.com/procts/pil/
使用方法
#coding:utf8
#获取指定图片的长和宽
fromPILimportImage
img=Image.open("img.jpg")
printimg.size
运行结果:
(52,54)
2. python 爬取图片,没有后缀名
首先你要安装Pillow这个库,它可以帮助你获取这个图片的类型。
importcStringIO,urllib2,time
fromPILimportImage
url='http://mmbiz.qpic.cn/mmbiz/KrBnGnvYojpichqTUY5X3g/0'
req=urllib2.urlopen(url)
data=req.read()
tmpIm=cStringIO.StringIO(data)
im=Image.open(tmpIm)
tm='%s.%s'%(int(time.time()),im.format.lower())
withopen(tm,'wb')asfp:
fp.write(data)
给分吧,哈啊哈。
如果解决了您的问题请采纳!
如果未解决请继续追问!
3. Python中的cStringIO问题
Python3 没有cStringIO, 改为io
tim@ubtim:~$ python
Python 2.7.3 (default, Sep 26 2013, 20:08:41)
[GCC 4.6.3] on linux2
Type "help", "right", "credits" or "license" for more information.
>>> import cStringIO
>>> exit()
tim@ubtim:~$ python3
Python 3.2.3 (default, Sep 25 2013, 18:25:56)
[GCC 4.6.3] on linux2
Type "help", "right", "credits" or "license" for more information.
>>> import cStringIO
Traceback (most recent call last):
File "<stdin>", line 1, in <mole>
ImportError: No mole named cStringIO
>>> import io
>>> ios = io.StringIO()
>>>
4. 如何利用Python抓取PDF中的某些内容
可以转换成TXT再抓取
fromcStringIOimportStringIO
frompdfminer.pdfinterp
importPDFResourceManager,PDFPageInterpreter
frompdfminer.converterimportTextConverter
frompdfminer..pdfpage
importPDFPage
defconvert_pdf_2_text(path):
rsrcmgr=PDFResourceManager()
retstr=StringIO()
device=TextConverter(rsrcmgr,retstr,codec='utf-8',laparams=LAParams())
interpreter=PDFPageInterpreter(rsrcmgr,device)
withopen(path,'rb')asfp:
forpageinPDFPage.get_pages(fp,set()):
interpreter.process_page(page)
text=retstr.getvalue()
device.close()
retstr.close()
returntext
5. 如何在python界面显示图片
wxpython:
# 使用wx.Image得到对象
bmp = wx.Image('bitmaps/image.bmp', wx.BITMAP_TYPE_BMP).ConvertToBitmap()
gif = wx.Image('bitmaps/image.gif', wx.BITMAP_TYPE_GIF).ConvertToBitmap()
png = wx.Image('bitmaps/image.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap()
jpg = wx.Image('bitmaps/image.jpg', wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
# 把它们显示出来
pos = 10
wx.StaticBitmap(frame, -1, bmp, (10, pos), (bmp.GetWidth(), bmp.GetHeight()))
pos = pos + bmp.GetHeight() + 10
wx.StaticBitmap(frame, -1, gif, (10, pos), (gif.GetWidth(), gif.GetHeight()))
pos = pos + gif.GetHeight() + 10
wx.StaticBitmap(panel, -1, png, (10, pos), (png.GetWidth(), png.GetHeight()))
pos = pos + png.GetHeight() + 10
wx.StaticBitmap(frame, -1, jpg, (10, pos), (jpg.GetWidth(), jpg.GetHeight()))
具体的请根据你的实际情况修改,最好去http://www.wxpython.org/download.php#binaries
下载wxpython和wxpython demo看看,这个demo很强大的。
6. python怎么处理xml节点包含命名空间,也就是冒号的情况
a:b为名不行吧,要展开为{URI}b这种形式,看看下面小例子取出的tag名称:
# -*- coding: utf-8 -*-
from xml.etree import ElementTree as ET
import cStringIO
xml = """\
<?xml version="1.0"?>
<root xmlns = "http://default-namespace.org/"
xmlns:py = "http://www.python.org/ns/">
<py:elem1 />
<elem2 xmlns="" />
</root>
"""
f = cStringIO.StringIO(xml)
#find all elements and print tag's name.
tree = ET.parse(f)
print repr(tree.getroot().tag)
elems = tree.findall('.//*')
for elem in elems:
print repr(elem.tag)
#same as above, but using iterparse.
f.seek(0)
for event, elem in ET.iterparse(f, ("start",)):
print repr(elem.tag)
输出:
'{http://default-namespace.org/}root'
'{http://www.python.org/ns/}elem1'
'elem2'
'{http://default-namespace.org/}root'
'{http://www.python.org/ns/}elem1'
'elem2'