图标处理小程序, 妈妈再也不用担心我不会制作图标了

# PythonMargick包可以到Unofficial Windows Binaries for Python Extension Packages下载

import PythonMagick

import os

CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))

IMAGE_DIR = os.path.abspath(os.path.join(CURRENT_DIR, '../../images'))

def to_ico(img_src, img_tar='icons'):

img = PythonMagick.Image(img_src)

# 这里要设置一下尺寸,不然会报ico尺寸异常错误

img.sample('128x128')

parent_dir, simple_img_src = os.path.split(img_src)

simple_img_src = os.path.splitext(simple_img_src)[0]

img_tar = os.path.join(parent_dir, img_tar)

img_tar = os.path.join(img_tar, simple_img_src)+'.ico'

img.write(img_tar)

if __name__ == '__main__':

img_name = 'a.gif'

img_src = os.path.join(IMAGE_DIR, img_name)

to_ico(img_src)

"""

Tk() 下有个 iconbitmap 可以指定 icon

"""

pdf 转 图片

最近工作中需要把pdf文件转化为图片,想用python来实现,于是在网上找啊找啊找啊找,找了半天,倒是找到一些代码。

1、第一个找到的代码,我试了一下好像是反了,只能实现把图片转为pdf,而不能把pdf转为图片。。。

把图片转成pdf

代码如下:

#!/usr/bin/env python

import os

import sys

from reportlab.lib.pagesizes import A4, landscape

from reportlab.pdfgen import canvas

f = sys.argv[1]

filename = ''.join(f.split('/')[-1:])[:-4]

f_jpg = filename+'.jpg'

print f_jpg

def conpdf(f_jpg):

f_pdf = filename+'.pdf'

(w, h) = landscape(A4)

c = canvas.Canvas(f_pdf, pagesize = landscape(A4))

c.drawImage(f, 0, 0, w, h)

c.save()

print "okkkkkkkk."

conpdf(f_jpg)

2、第二个是文章写的比较详细,可惜的是linux下的代码,所以仍然没用。

3、第三个文章指出有一个库PythonMagick可以实现这个功能,需要下载一个库 PythonMagick-0.9.10-cp27-none-win_amd64.whl 这个是64位的。

这里不得不说自己又犯了一个错误,因为自己从python官网上下载了一个python 2.7,以为是64位的版本,实际上是32位的版本,所以导致python的版本(32位)和下载的PythonMagick的版本(64位)不一致,弄到晚上12点多,总算了发现了这个问题。。。

4、然后,接下来继续用搜索引擎搜,找到很多stackoverflow的问题帖子,发现了2个代码,不过要先下载PyPDF2以及ghostscript模块。

先通过pip来安装 PyPDF2、PythonMagick、ghostscript 模块。

C:\Users\Administrator>pip install PyPDF2

Collecting PyPDF2

Using cached PyPDF2-1.25.1.tar.gz

Installing collected packages: PyPDF2

Running setup.py install for PyPDF2

Successfully installed PyPDF2-1.25.1

You are using pip version 7.1.2, however version 8.1.2 is available.

You should consider upgrading via the 'python -m pip install --upgrade pip' command.

C:\Users\Administrator>pip install C:\PythonMagick-0.9.10-cp27-none-win_amd64.whl

Processing c:\pythonmagick-0.9.10-cp27-none-win_amd64.whl

Installing collected packages: PythonMagick

Successfully installed PythonMagick-0.9.10

You are using pip version 7.1.2, however version 8.1.2 is available.

You should consider upgrading via the 'python -m pip install --upgrade pip' command.

C:\Users\Administrator>pip install ghostscript

Collecting ghostscript

Downloading ghostscript-0.4.1.tar.bz2

Requirement already satisfied (use --upgrade to upgrade): setuptools in c:\python27\lib\site-packages (from ghostscript)

Installing collected packages: ghostscript

Running setup.py install for ghostscript

Successfully installed ghostscript-0.4.1

You are using pip version 7.1.2, however version 8.1.2 is available.

You should consider upgrading via the 'python -m pip install --upgrade pip' command.

下面是代码

代码1:

import os

import ghostscript

from PyPDF2 import PdfFileReader, PdfFileWriter

from tempfile import NamedTemporaryFile

from PythonMagick import Image

reader = PdfFileReader(open("C:/deep.pdf", "rb"))

for page_num in xrange(reader.getNumPages()):

writer = PdfFileWriter()

writer.addPage(reader.getPage(page_num))

temp = NamedTemporaryFile(prefix=str(page_num), suffix=".pdf", delete=False)

writer.write(temp)

print temp.name

tempname = temp.name

temp.close()

im = Image(tempname)

#im.density("3000") # DPI, for better quality

#im.read(tempname)

im.write("some_%d.png" % (page_num))

os.remove(tempname)

代码2:

import sys

import PyPDF2

import PythonMagick

import ghostscript

pdffilename = "C:\deep.pdf"

pdf_im = PyPDF2.PdfFileReader(file(pdffilename, "rb"))

print '1'

npage = pdf_im.getNumPages()

print('Converting %d pages.' % npage)

for p in range(npage):

im = PythonMagick.Image()

im.density('300')

im.read(pdffilename + '[' + str(p) +']')

im.write('file_out-' + str(p)+ '.png')

#print pdffilename + '[' + str(p) +']','file_out-' + str(p)+ '.png'

然后执行时都报错了,这个是 代码2 的报错信息:

Traceback (most recent call last):

File "C:\c.py", line 15, in

im.read(pdffilename + '[' + str(p) +']')

RuntimeError: pythonw.exe: PostscriptDelegateFailed `C:\DEEP.pdf': No such file or directory @ error/pdf.c/ReadPDFImage/713

总是在上面的     im.read(pdffilename + '[' + str(p) +']') 这一行报错。

于是,根据报错的信息在网上查,但是没查到什么有用的信息,但是感觉应该和GhostScript有关,于是在网上去查安装包,找到一个在github上的下载连接,但是点进去的时候显示无法下载。

最后,在csdn的下载中找到了 这个文件:GhostScript_Windows_9.15_win32_win64,安装了64位版本,之后,再次运行上面的代码,都能用了。

不过代码2需要做如下修改,不然还是会报 No such file or directory @ error/pdf.c/ReadPDFImage/713 错误:

#代码2

import sys

import PyPDF2

import PythonMagick

import ghostscript

pdffilename = "C:\deep.pdf"

pdf_im = PyPDF2.PdfFileReader(file(pdffilename, "rb"))

print '1'

npage = pdf_im.getNumPages()

print('Converting %d pages.' % npage)

for p in range(npage):

im = PythonMagick.Image(pdffilename + '[' + str(p) +']')

im.density('300')

#im.read(pdffilename + '[' + str(p) +']')

im.write('file_out-' + str(p)+ '.png')

#print pdffilename + '[' + str(p) +']','file_out-' + str(p)+ '.png'

这次有个很深刻的体会,就是解决这个问题过程中,大部分时间都是用在查资料、验证资格资料是否有用上了,搜索资料的能力很重要。

而在实际搜索资料的过程中,国内关于PythonMagick的文章太少了,搜索出来的大部分有帮助的文章都是国外的,但是这些国外的帖子文章,也没有解决我的问题或者是给出有用的线索,最后还是通过自己的思考,解决了问题。

批量修改图片大小 (mb)

# !/usr/bin/python

# -*- coding: utf-8 -*-

"""

author : 蛙鳜鸡鹳狸猿

create_time : 2016年 11月 01日 星期二 17:38:06 CST

program : *_* script of resizing image *_*

"""

import sys

import PythonMagick

class ManImage:

"""

Manipulate Image Object

"""

def __init__(self, i_file, o_dire):

"""

init args

:param i_file: (str) input image file (eg: "/home/img.jpg")

:param o_dire: (str) output image directory (eg: "/home/")

"""

self.i_file = i_file

self.o_dire = o_dire

def __str__(self):

traceback = "Executing under {0.argv[0]} of {1.i_file} into {2.o_dire}......".format(sys, self, self)

return traceback

def playimage(self, rs):

"""

resize image file

:param rs: (int) set rs = 400 ~= 100KB output under my test

:return: resized PNG image file

"""

image = PythonMagick.Image(self.i_file)

try:

image.resize(str(rs))

image.monochrome(True)

image.magick("PNG")

image.write(self.o_dire + self.i_file.split('/')[-1].split('.')[0] + '.png')

print('"{0.i_file}" play OK......'.format(self))

except Exception, e:

print(str(e))

以上代码写入一个“class_image.py”文件,下面是调取的简单示例。

# !/usr/bin/python

# -*- coding: utf-8 -*-

# te_author : 蛙鳜鸡鹳狸猿

# create_time : 2016年 11月 01日 星期二 17:38:06 CST

# NOTICE : *_* script of resizing image to set*_*

import os

import sys

import class_image

i_dire = sys.argv[1]

o_dire = sys.argv[2]

rs = sys.argv[3]

for i_file in os.listdir(i_dire):

class_image.ManImage(i_file=i_dire + i_file, o_dire=o_dire).playimage(rs=rs)

即在命令行分别传入读入目录、输出目录以及图片大小三个参数,操作起来方便简捷。图片可以直接从pdf文件里面逐页转换出来并同时修改图片大小,参考:http://blog.csdn.net/sweeper_freedoman/article/details/53000145。如果已经安装使用“ImageMagick”,也可以通过Python调用处理图片,参考http://blog.csdn.net/sweeper_freedoman/article/details/69789307。

链接地址

好用的python_好用的 python 工具集合相关推荐

  1. 循环 直到 python_如果您在Python中存在慢循环,则可以对其进行修复……直到无法解决为止...

    循环 直到 python by Maxim Mamaev 马克西姆·马马耶夫(Maxim Mamaev) Let's take a computational problem as an exampl ...

  2. gzip解压str python_有哪些你不知道的Python小工具

    ython作为越来越流行的一种编程语言,不仅仅是因为它语言简单,有许多现成的包可以直接调用. python中还有大量的小工具,让你的python工作更有效率. 1. 快速共享 HTTP服务器 Simp ...

  3. matlab2019支持python_全方位对比:Python、Julia、MATLAB、IDL 和 Java (2019 版)

    引言 我们使用简单的测试用例来对各种高级编程语言进行比较.我们是从新手程序员的角度来实现测试用例,假设这个新手程序员不熟悉语言中可用的优化技术.我们的目的是突出每一种语言的优缺点,而不是宣称一种语言比 ...

  4. python 获取集合里面的某一个元素

    python 获取集合里面的某一个元素,想想呢集合是不支持所以,切片,相加,相乘操作的, 所以想获取集合里面的某一个元素需要转化下思路,比如把即可转成列表然后在利用索引获取 例如: list_a = ...

  5. Python培训教程分享:“高效实用” 的Python工具库

    作为一名合格Python技术员,对于Python工具库的使用是少不了的,本期Python培训教程就为大家分享的是""高效实用" 的Python工具库",希望能够 ...

  6. python爬取淘宝商品做数据挖掘_Python 3爬虫 数据清洗与可视化实战 Python数据抓取技术 python3网络爬虫教程书籍 运用Python工具获取电商平台页面数据挖掘书籍...

    A8 书    名:Python 3爬虫 数据清洗与可视化实战 作 译 者:零一,韩要宾,黄园园 出版时间:2018-03 千 字 数:200 版    次:01-01 页    数:212 开   ...

  7. python语言模型工具_Python工具整合,为程序员和新手准备的 8 大 Python 工具

    Python 是一种开源编程语言,用于 Web 编程.数据科学.人工智能和许多科学应用.学习 Python 使程序员能够专注于解决问题,而不是专注于语法,其丰富的库赋予它完成伟大任务所需的力量. 1) ...

  8. 十大python开发软件-5款开发安全、高质量代码的优秀Python工具

    怎样提高代码的质量.安全性和可维护性,本文作者推荐了五款工具,并从四个方面对其进行量化.并且,他还介绍了怎样将这些工具整进 CI pipeline. 为提高代码的质量.安全性和可维护性,软件工程师每天 ...

  9. 全部python编程语言-编程语言高质量代码的优秀Python工具

    [金融特辑]光大银行科技部DBA女神带你从0到1揭秘MGR 高代码的质量.安全性和可维护性,本文作者推荐了五款工具,并从四个方面对其进行量化.并且,他还介绍了怎样将这些工具整进 CI pipeline ...

最新文章

  1. 7-9 用天平找小球 (C语言)
  2. 使用ISAPI_Rewrite做实用的重定向
  3. 多线程学习笔记一之内置锁
  4. Prefix Sum Primes
  5. 初学C#中遇到的问题!
  6. oracle建共享dblink,oracle建多个dblink
  7. 如何在XP SP2下面使用DTC
  8. 浏览器桌面通知(notifications)
  9. 51nod-1131: 覆盖数字的数量
  10. 生活随笔:你会狗眼看人低吗?
  11. 面试中常被问到(七)封装继承多态知多少
  12. 2.ISIS协议原理
  13. ios软件商店上架老被打回_iOS APP上架App Store常见被拒原因及解决方案
  14. 评点SAP HR功能及人力资源管理软件
  15. PKUSC 模拟赛 day1 下午总结
  16. hql 语句不能包含关键字
  17. 解决MySQL远程过程调用失败
  18. 服务器安装360文档卫士,360文档卫士的安装使用方法
  19. 概率统计Python计算:离散型随机变量分布(bernoulli geom)
  20. 《时代周刊》2019年度100大最佳发明榜单发布!中国2项上榜

热门文章

  1. 285行c语言代码实现简易贪吃蛇
  2. 无代码平台是什么意思?使用无代码做数字化转型的好处?
  3. 硬件设计:元器件--二极管常用的七种用法
  4. win10-你需要来自XXX的权限才能对此文件夹进行更改 转载
  5. Jquery获取不到元素
  6. 2013年10月微软MVP申请开始了
  7. 为什么使用mybatis
  8. MATLAB中subplot函数的使用
  9. 应用服务器中对JDK的epoll空转bug的处理
  10. python 计算置信区间_从样本数据计算置信区间