python 提取图片转为16 24BPP 的方法
python 中处理图片用的是 pil ,在 linux 和 win 上都可以使用。
centOS 5.x 上安装的方法是 yum install python-imaging
24BPP:
import Image
img = Image.open("1.jpg")
out = ""
width,height = img.size
for y in range(0, height):
for x in range(0, width):
r,g,b = img.getpixel((x, y))
px = hex(r<<16 | g<<8 | b)
out += str(px) + "," print out
就是取像素的颜色在转为16进制,24BPP 的颜色,每位占8位。
16BPP:
import Image
img = Image.open("1.jpg")
out = ""
width,height = img.size
for y in range(0, height):
for x in range(0, width):
r,g,b = img.getpixel((x, y))
#8bit convert to 5bit
px = hex((r>>3)<<11 | (g>>2)<<5 | b>>3)
out += str(px) + "," print out
2440 要求 分别占RGB 分别占 5 6 5 位。
程式只做了打印, 实际用的时候,可以通过 get24bbp.py > /home/24bpp.h 这样来使用。当然了,你还要手工编辑加上 static unsigned long img[] = {...}; 之类的。
24BPP:
$out = "static const unsigned long img1[]={";
$img = imagecreatefromjpeg("a.jpg");
$width = imagesx($img);
$height = imagesy($img);
for($y=0;$y<$height;$y++)
{
for($x=0;$x<$width;$x++)
{
$out .= sprintf("0x%X",imagecolorat($img,$x,$y)) . ',';
}
}
$out .="0};\r\n";
file_put_contents("out.h",$out);
php 中使用 sprintf 转为 16进制,因为 php 我平时是在浏览器上运行的, 不能 > 导出, 所以加了 文件保存。
php 中文件保存很容易, python 中就要先 open 在 write 最后关闭。 但是 python 中, r g b 分散的方式很好,要是做位运算方便些。
python 提取图片转为16 24BPP 的方法的更多相关文章
- Python提取图片的ROI
图像处理经常需要提取图片的ROI,本文使用Python提取图片的ROI. 使用的Module是PIL (Pillow),一个图像处理库,用到的函数为类 Image 中的 crop 方法. 函数原型为: ...
- python 读取二进制文件 转为16进制输出
示例: #!/usr/bin/env python #encoding: utf-8 import binascii fh = open(r'C:\Temp\img\2012517165556.png ...
- python将图片转为base64编码
import base64 f = open("m1.jpg", "rb") res = f.read() s = base64.b64encode(res) ...
- Python实现图片滑动式验证识别
1 abstract 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类 ...
- python2/3中 将base64数据写成图片,并将图片数据转为16进制数据的方法、bytes/string的区别
1.python2将base64数据写成图片,并将数据转为16进制字符串的方法 import binascii img = u'R0lGODlhagAeAIcAAAAAAAAARAAAiAAAzABE ...
- 从word中提取图片的三种方法
方法1:使用截图方法来提取并保存图片,如果你安装了QQ并且运行了的话,你可以使用Ctrl+Alt+A来截图,然后在QQ聊天框中按CTRL+V来保存图片,当然你可以在PS新建文档按CTRL+V来粘贴图片 ...
- python 如何将md5转为16字节
python的hashlib库中提供的hexdigest返回长度32的字符串. md5sum是128bit,也就是16字节,如何将python生成字符串的转为16字节呢? 请看下面代码 import ...
- 将python图片转为二进制文本的实例
https://www.jb51.net/article/155342.htm 写在最前面: 我在研究机器学习的过程中,给的数据集是手写数字图片被处理后的由0,1表达的txt文件,今天写一写关于图片转 ...
- python提取视频第一帧图片
一.实现代码 # -*- coding: utf-8 -*- import cv2 from PIL import Image from io import BytesIO def tryTime(m ...
随机推荐
- UIImage转换UIColor内存会莫名增大可以试试另一种方法
一般我们会用此方法加载被背景图片 [self.view setBackgroundColor:[UIColor colorWithPatternImage:[[UIImage alloc]initWi ...
- Hibernate中in语句ids
Query query=session.createQuery("from User where id in (:ids)"); query.setParameterList(&q ...
- POJ 1028题目描述
Description Standard web browsers contain features to move backward and forward among the pages rece ...
- webDriver运行过程中遇到的问题
1.org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 705 ...
- urllib下载文件
import urllib 1.用urlib.urlretrieve f = urllib.urlretrieve('http://www.baidu.com/img/bdlogo.gif','/tm ...
- Java基础之创建窗口——创建应用程序窗口(TryWindow)
控制台程序. 准备好应用程序窗口及其包含的组件并显示,这称为实现窗口.调用应用程序窗口对象的setVisible()方法就会实现窗口.实现了应用程序的GUI之后,在主线程中修改或查询GUI可能会导致死 ...
- 通过xib加载textfield的时候 发生 this class is not key value coding-compliant for the key textField. 情况怎么解决
连线的时候不要选files’owner 要选xib自己的class
- 当As3遇见Swift(二)
字符串:String 都是用String来表示,都是值类型,在传递过程中都会进行拷贝. 计算字符数量 As3: str.length Swift: countElements(str) 数组:Arra ...
- Leetcode: UTF-8 Validation
A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte char ...
- JS小练习 留言功能
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...