使用python来处理图片是非常方便的,下面提供一小段python处理图片的代码,需要安装图像处理工具包PIL(Python Image Library)。
#coding=utf-8
import Image
import urllib2
import StringIO
import os
#改变图片大小
def resize_img(img_path):
try:
img = Image.open(img_path)
(width,height) = img.size
new_width = 200
new_height = height * new_width / width
out = img.resize((new_width,new_height),Image.ANTIALIAS)
ext = os.path.splitext(img_path)[1]
new_file_name = '%s%s' %('small',ext)
out.save(new_file_name,quality=95)
except Exception,e:
print e
#改变图片类型
def change_img_type(img_path):
try:
img = Image.open(img_path)
img.save('new_type.png')
except Exception,e:
print e
#处理远程图片
def handle_remote_img(img_url):
try:
request = urllib2.Request(img_url)
img_data = urllib2.urlopen(request).read()
img_buffer = StringIO.StringIO(img_data)
img = Image.open(img_buffer)
img.save('remote.jpg')
(width,height) = img.size
out = img.resize((200,height * 200 / width),Image.ANTIALIAS)
out.save('remote_small.jpg')
except Exception,e:
print e
if __name__ == '__main__':
img_path = 'test.jpg'
resize_img(img_path)
change_img_type(img_path)
img_url = 'http://img.hb.aicdn.com/042f8a4a70239f724ff7b9fa0fc8edf18658f41022ada-WcItWE_fw554'
handle_remote_img(img_url)
可能会遇到的问题
ImportError: No module named Image
解决办法:安装Python Imaging Library(PIL)
sudo easy_install PIL
安装PIL出现:
— JPEG support not available
— ZLIB (PNG/ZIP) support not available
— FREETYPE2 support not available
操作jpeg图片和png图片出现:
IOError: decoder jpeg not available 和IOError: encoder zip not available
解决办法:
(1) 删除已经安装的PIL
sudo rm -rf /usr/local/lib/python2.6/site-packages/PIL-1.1.7-py2.6-linux-x86_64.egg/
(2) 安装相关库
sudo apt-get install libjpeg8 libjpeg62-dev libfreetype6 libfreetype6-dev
sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/
sudo ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib/
sudo ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/lib/
(3) 重新安装PIL
sudo easy_install PIL
终端出现:
— JPEG support available
— ZLIB (PNG/ZIP) support available
— FREETYPE2 support available
现在试试,已经ok了
转载请注明来自:Alex
Zhou,本文链接:http://codingnow.cn/python/208.html
分享到:
相关推荐
Python-imagematch快速搜索数十亿张图片
Image 2 ANSI - 实现在ANSI终端中显示图像和摄像机视频
这是一个方便的图片上传实用工具,可以方便, 快速地把一张图片上传然后得到一个图片链接
Python图像处理食谱 这是Packt发行的的代码存储库。 超过60种配方可帮助您轻松执行复杂的图像处理和计算机视觉任务 这本书是关于什么的? 无线设备和移动技术的进步使人们能够获取大量的图形,图片和视频。 通过...
Image-Color 利用深度学习方法给黑白图片图片上色
做cnn的难免要做大量的图片处理。由于接手项目时间不长,且是新项目,前段时间写代码都很赶,现在稍微总结(恩,总结是个好习惯)。 1,首先安装python-Image和python-skimage、python-matplotlib。 简单代码: ...
主要介绍了python使用Image处理图片的常用技巧,实例分析了Python使用image处理图片过程中改变图片大小、图片类型及远程图片中常见问题与解决方法,需要的朋友可以参考下
Python version of Luban(鲁班)—Image compression with efficiency very close to WeChat Moments/可能是最接近微信朋友圈的图片压缩算法
对多张图片进行基于SIFT的特征检测算法,如果符合最小拼接要求大的关键点matchKeypoints数量,使用OpenCV-Python自带的stitching方法进行全景拼接,但是对于拼接后的黑边裁剪效果不好,可以修改优化。 使用方式 ...
Research techniques and papers related to image generation and image restoration used by DeepNude. 研究DeepNude使用的图像生成和图像修复相关的技术和论文。
pyCAIR是一个通过内容感知来调整图片大小(CAIR)库,基于 Seam Carving for Content-Aware Image Resizing论文
一般的单张图片处理使用的是reshape或resize对图片进行缩放,但是如果我们是长方形图片去获得正方形图片的话就会造成图片变形,第二种就是对较长边进行裁剪成正方形,这样会导致图片部分内容被裁剪掉,所以具体选择...
Lossy Image Compression with Compressive Autoencoders
Python-OpenCV中虽然不存在直接得函数,但是很容易使用相关的函数来实现。 代码: import numpy as np import random import cv2 def sp_noise(image,prob): ''' 添加椒盐噪声 prob:噪声比例 ''' output = np....
# 打开文件夹中的图片 image=Image.open('./Img/'+str(i)+'.jpg') # 灰度图 lim=image.convert('L') # 灰度阈值设为165,低于这个值的点全部填白色 threshold=165 table=[] for j in range(256): if j<
A simple interactive image matting app by opencv and qt. 使用opencv进行交互式抠图
Python pillow图像处理 01Image打开显示图片.mp4
利用Python语言实现对图片的批量处理,参考图片编辑/美图等应用操作,实现对图片进行曝光/鲜明度/对比度/水印等属性处理,将处理后的...图片预存路径为"D:\PythonProject\Pic",处理结果图片会存放在桌面Image文件夹下