标签:
#需要安装PIL模块
#encoding=gbk
#-------------------------------------------------------------------------------
# Name: picRead
# Purpose:
#
# Author: wangchao
#
# Created: 27/06/2014
# Copyright: (c) wangchao 2014
# Licence: <your licence>
#-------------------------------------------------------------------------------
from PIL import Image
def main():
filename = ‘Z:/360pic/116_0618/IMGP3828.JPG‘
img = Image.open(filename)
imgSize = img.size #图片的长和宽
print imgSize
maxSize = max(imgSize) #图片的长边
print maxSize
minSize = min(imgSize) #图片的短边
print minSize
if __name__ == ‘__main__‘:
main()
判断图片分辨率并修改 # -*- coding: utf-8 -*- import os,sys,Image rootDir = r‘c:\\images‘ targetDir = r‘c:\\imagesover‘ def encodeChinese(msg): type = sys.getfilesystemencoding() return msg.decode(‘UTF-8‘).encode(type) errFile = open(r‘c:\\errFile.txt‘,‘w‘) def judgeSize(im): #判断图片分辨率,如果最大边超过1024返回False,如果不超过返回True mySize = im.size maxValue = max(mySize) minValue = min(mySize) if(maxValue > 1024): return False else: return True def returnSize(im): #返回图片大小,返回两个值,第一个返回值总为最大 max,min = im.size if max > min: return max,min else: return min,max def changeSize(im,max,min): value = max/1024 min = min/value newimg = im.resize((1024,min),Image.ANTIALIAS) return newimg def main(): for parent,dirnames,filenames in os.walk(rootDir): for filename in filenames: fName = filename filename = parent + os.sep + filename fPostfix = os.path.splitext(filename)[1] try: img = Image.open(filename) except: print filename print encodeChinese(‘打开这个文件出错‘) continue #img.load() print filename print fPostfix if(fPostfix !=‘.jpg‘ and fPostfix !=‘.png‘ and fPostfix != ‘.JPG‘ and fPostfix != ‘.PNG‘): errFile.write(str(filename) + ‘\\n‘) errFile.write(encodeChinese(‘上面这个文件不是图片,请检查...‘) + ‘\\n‘) errFile.write(‘\\n‘) else: print ‘juageSize()‘ if(judgeSize(img) == False): print ‘judgeSize == False‘ max,min = returnSize(img) newimg = changeSize(img,max,min) newimg.save(targetDir + os.sep + fName) print str(targetDir + os.sep + fName) print encodeChinese(‘保存完毕‘) print encodeChinese(‘处理完毕‘) errFile.close() main()
用image模块,可以用getpixel获得像素值,给你个例子吧。得到的像素值应该是(R,G,B,A),大家读读看每个图片是什么。
#!/usr/bin/env python
import Image
import sys
im = Image.open(sys.argv[1])
width = im.size[0]
height = im.size[1]
print "/* width:%d */"%(width)
print "/* height:%d */"%(height)
count = 0
for h in range(0, height):
for w in range(0, width):
pixel = im.getpixel((w, h))
for i in range(0,3):
count = (count+1)%16
if (count == 0):
print "0x%02x,/n"%(pixel[i]),
else:
print "0x%02x,"%(pixel[i]),
工作中要把bmp图片的数据转换成数组放到uboot代码中,因为有几张图片,使用bin转换成文本的工具还不如自己写脚本方便来得快
标签:
原文地址:http://www.cnblogs.com/x113/p/4735396.html