[Perl]BMP图片转TXT,暂时只支持黑白,0和1
[i=s] 本帖最后由 523066680 于 2015-11-5 11:08 编辑 [/i][code]
=info
Code: 523066680
Date: 2015-11
=cut
use utf8;
use IO::Handle;
STDOUT->autoflush(1);
binmode(STDOUT, ":encoding(gbk)");
my $file = "1.bmp";
our $READ;
open $READ, "<:raw", $file or die "$!";
GET_INFO:
{
our $v;
read($READ, $v, 14, 0);
our ($type, $bfSize, $res1, $res2, $offset) = (unpack 'SLSSL', $v);
read($READ, $v, 4+4+4+2+2, 0);
our ($headSize, $width, $height, $planes, $bitCount) = (unpack 'L3S2', $v);
our $Compoments_per_pixel = $bitCount / 8;
#RGBA, 4bytes, 32bit
#RGB, 3bytes, 24bit
#Windows的BMP规定一行所占的字节数须是 4字节的倍数,不足的以0填充
our $rowLen = ($bfSize - $offset) / $height;
our $rowCut = ($width * $Compoments_per_pixel) % 4; #RGBA的情况下自然为0
printf "文件字节数:%04x -> %d\n", $bfSize, $bfSize;
printf "位图偏移量:%04x -> %d\n", $offset, $offset;
printf " 宽 × 高:%d×%d\n", $width, $height;
printf " 位图色深:%d 位\n", $bitCount;
}
TO_TEXT:
{
#跳过文件头
seek($READ, $offset, 0);
my ($R, $G, $B);
my $col = 0;
my @all;
my $tstr="";
while ( read( $READ, $v, $Compoments_per_pixel, 0) )
{
$col++;
($B, $G, $R) = unpack("C$Compoments_per_pixel", $v); #C4 or C3
$tstr .= ($R || $G || $B) > 0 ? 0 : 1; #黑白取反, 白色为0,其他色为1
if ($col == $width)
{
unshift @all, $tstr;
seek($READ, $rowCut, 1); #从当前去掉多余的填充字节
$col = 0;
$tstr="";
}
}
close $READ;
print join("\n", @all);
}[/code][img]http://imgout.ph.126.net/48091001/1.jpg[/img]
[font=宋体]文件字节数:1426 -> 5158
位图偏移量:0036 -> 54
宽 × 高:58×29
位图色深:24 位
0000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000
0000000011110000000000000000000000000000000000000000000000
0000000111111000000000000000000000000000000000000000000000
0000000111111100000000000000000000000000000000000000000000
0000000110001110000000000000000000000000000000000000000000
0000001110001110000000000000000000000100000000000000000000
0000001110001110000000000000000000001110000000000000000000
0000011100001110000000000000000000001110000000000000000000
0000011100001110000000000000000000001110000000000000000000
0000011000011110000111110000000011111111111111110000000000
0000111011111110001111111000000111111111111111111000000000
0000110111111100011111111100000111111111111111111000000000
0001110111111110111000011100000000011100000000000000000000
0001110000011111111000011100000000011100000000000000000000
0001100000000111110000011100000000011100000000000000000000
0011100000000011110000011100000000011100000000000000000000
0011100000000011110000111100000000011111000000000000000000
0111000000000011110000111100000000001111100000000000000000
0011000000000011111111111111111000001111100000000000000000
0010000000000011111111100111111000000000000000000000000000
0111111111111111111110000011110000000000000000000000000000
0111111111111110000000000000000000000000000000000000000000
0011111111111100000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000[/font] [i=s] 本帖最后由 523066680 于 2018-11-27 20:07 编辑 [/i]
轮廓贴合(未完善)[code]
514 x 268
7 14
__ ?^.
, _ 2 _,,
( "+' "=~._ ,&=~, __,4
`) \"``"0__.""___""%_ ~ ,.",
{ _"$ ( ," ,*` "+,",_ 2 *__
"' %+ _" _" "{"+=&,_
, , 7"*+."%,
, 2 _.=^""`7""*=,_ "%_%,
_ 2 ,*" __.+;+=+. `"<_ %,".
_' , ='_$"` ) | _.% ". ",".
' 0'_6+. 0 *0, _", ", "" ", % :
? 2 7{^%."; ".` ..+.: : ,( $ (
$ ?_*+* _ _+ _ ;=~.' :* `"" "; % .
? ? 9*"2 ?% "%.' [ : %7{ ( $
",_ _" _' '_ `` *=' %_+, |2
"%&_ _? _+ `` "%_7"+=&.:2
"$3+._ _=" _< `"=~~="
`"=._""^"""` _,="
7""^*^""`[/code][code]380 x 198
7 18
": ;,
%,"` "1<_ _,.._,4""{ =='
[_".% .',"` `*.%__'3_)
, ,` "^+~2=,_
? ? _."` ____ `"=. ".".
? ?+_>" 2 { +?, ,_%. '.:
' ''.%.0 %" 6.~% ( _& %'
_ , %.+:,.%(,".( *0. 3 (4
"%_ _" ,' *. ="=.__ {3
"0+~___ __." _* "^*"
`"^+++""`
[/code]
页:
[1]