使用python PIL处理图片。怎么获取图片的像素数据

使用python PIL处理图片。怎么获取图片的像素数据,第1张

用image模块更直接,可以用getpixel获得像素值,给你个例子吧。

01#!/usr/bin/env python

02import Image

03import sys

04im = Imageopen(sysargv[1])

05width = imsize[0]

06height = imsize[1]

07print "/ width:%d /"%(width)

08print "/ height:%d /"%(height)

09count = 0

10for h in range(0, height):

11 for w in range(0, width):

12 pixel = imgetpixel((w, h))

13 for i in range(0,3):

14 count = (count+1)%16

15 if (count == 0):

16 print "0x%02x,/n"%(pixel[i]),

17 else:

18 print "0x%02x,"%(pixel[i]),

pBand->RasterIO获取某一波段数组,然后根据像素间的偏移去索引,估计就这种方式了,没有opencv处理起来好用,我们一般是gdal分块读了之后转成Mat,然后逐个处理。

Private Type BMP_File_Header

    fileType As Integer

    fileLength As Long

    reserve As Long

    offset As Long

End Type

Private Type BMP_Info_Header

    infoLength As Long

    width As Long

    height As Long

    plane As Integer

    pixBits As Integer

    compress As Long

    dataLength As Long

    H_res As Long

    V_res As Long

    colorNumber As Long

    impColor As Long

End Type

Private Sub Form_Load()

Dim bfh As BMP_File_Header

Dim bih As BMP_Info_Header

Dim ct(3, 255) As Byte

Dim P(255, 255) As Byte

Dim i As Long, j As Long

bfhfileType = &H4D42

bfhoffset = Len(bfh) + Len(bih) + 4  256

bihdataLength = &H100&  &H100&

bfhfileLength = bfhoffset + bihdataLength  '文件大小

bihinfoLength = Len(bih)

bihwidth = 256   '宽度

bihheight = 256   '高度

bihplane = 1

bihpixBits = 8   '每像素位数

bihcompress = 0

bihH_res = &HB12

bihV_res = &HB12

bihcolorNumber = 256

bihimpColor = 0

For i = 0 To 255  '产生256级的灰度调色板

    ct(0, i) = i

    ct(1, i) = i

    ct(2, i) = i

    ct(3, i) = 0

Next

For j = 0 To 255   '这里是生成一个256256的位图数据,你有现成的数据就可直接用

    For i = 0 To 255

        P(i, j) = i

    Next

Next

Open "d:\testbmp" For Binary As #1

Put #1, , bfh

Put #1, , bih

Put #1, , ct

Put #1, , P

Close #1

MsgBox "OK"

End Sub

用gdi+ 、OpenCV、cximge、freeimage等等库 都有方法加载图像,然后获取对象的像素:

比如用gdi+:

1、#include "GdiPlush"

#pragma comment(lib, "gdipluslib")

2、初始化gdi+(具体可以百度下:)

3、Bitmap image(文件名);

int w = imageGetWidth();

int h = imageGetHeight()。

以上就是关于使用python PIL处理图片。怎么获取图片的像素数据全部的内容,包括:使用python PIL处理图片。怎么获取图片的像素数据、图像处理之GDAL逐个像素值 *** 作的函数有没有、如何在VB中逐像素生成一个bmp文件等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9580791.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存