I want to replace all white pixel from a completely white column with other color and if I have a black pixel on column don't change anything, but I don't know where is the problem...
Here is the code:
function findLines() {
$blank = 1;
$im_1 = imageCreateFromPng('resize_1.png');
for($i=0; $i<1090; $i++) {
if($blank == 1) {
for($j=0; $j<240; $j++) {
$rgb = imageColorAt($im_1, $i, $j);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$c = $r.$g.$b;
if($c === "0 0 0") {
$blank = 0;
}
$color = imageColorAllocate($im_1, 0, 255, 255);
imageSetPixel($im_1, $i, $j, $color);
}
}
if ($blank == 0) {
for($j=0; $j<240; $j++) {
$rgb = imageColorAt($im_1, $i, $j);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$c = $r . " " . $g . " " . $b;
if($c === "255 255 255") {
$blank = 1;
}
}
} else {
$blank = 0;
}
}
header("Content-Type: image/png");
imagepng($im_1);
}
This is the right one. Thanks anyway!
function findLines() {
$im_1 = imageCreateFromPng('resize_1.png');
for($i=0; $i<1090; $i++) {
$blank = 1;
for($j=0; $j<240; $j++) {
$rgb = imageColorAt($im_1, $i, $j);
if($rgb == 0) {
$blank = 0;
}
}
if ($blank == 1) {
for($j=0; $j<240; $j++) {
$color = imageColorAllocate($im_1, 155, 155, 155);
imageSetPixel($im_1, $i, $j, $color);
}
} else {
$blank = 0;
}
}
header("Content-Type: image/png");
imagepng($im_1);
}
Related
I have a issue with PHP 7.4.5 and GD. I have an image manipulation script and sometime I get this error:
imagecolorallocate(): Red component is out of range
Anybody has an idea how to avoid this?
$image = imagecreatetruecolor($calcWidth, $calcHeight);
for ($y = 0; $y < $height; ++$y) {
for ($x = 0; $x < $width; ++$x) {
[$r, $g, $b] = $pixels[$y][$x];
$allocate = imagecolorallocate($image, $r, $g, $b);
imagesetpixel($image, $x, $y, $allocate);
}
}
I fixed it with this condition, because sometimes an r/g/b value was 256 and rgb can only have up to 255:
[$r, $g, $b] = $pixels[$y][$x];
if($r > 255) { $r = 255; }
if($g > 255) { $g = 255; }
if($b > 255) { $b = 255; }
if($r < 0) { $r = 0; }
if($g < 0) { $g = 0; }
if($b < 0) { $b = 0; }
What I'm trying to do is to pixelate an image of a url, add another image above it and last save that image.
I have obtained this internet code to pixelate images but I think it does not work correctly. It does not show me the pixelated image nor does it save it. Just the black screen remains.
$pixel = 15;
$getImagen = 'https://ep00.epimg.net/elpais/imagenes/2017/06/05/album/1496652756_562670_1496654035_album_normal.jpg';
$imagen = imagecreatefromjpeg($getImagen);
if(!$imagen) exit('ERROR');
list($ancho,$alto)=getimagesize($getImagen);
$superficieTotal = $ancho*$alto;
//
$superficieRecorrida = 0;
$auxX=0;
$auxY=0;
while($superficieRecorrida <= $superficieTotal){
$posX=0;$posY=0;$data = array();
while($posX <= $pixel and (($auxX + $posX) < $ancho)){
$posY=0;
while($posY <= $pixel and (($auxY + $posY) < $alto)){
$rgb = imagecolorat($imagen, ($auxX + $posX), ($auxY + $posY));
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$data[] = array($r,$g,$b);
$posY++;
}
$posX++;
}
// Busco promedio
$r = 0; $g = 0; $b = 0;
foreach($data as $d){
$r+= $d[0];
$g+= $d[1];
$b+= $d[2];
}
$totalArray = count($data);
if($totalArray == 0) $totalArray = 1;
$r = $r/$totalArray;
$g = $g/$totalArray;
$b = $b/$totalArray;
$colorPromedio = imagecolorallocate($imagen, $r, $g, $b);
imagefilledrectangle($imagen, $auxX, $auxY, ($auxX + $pixel), ($auxY + $pixel), $colorPromedio);
//
$auxX+= $pixel;
if($auxX >= $ancho){
$auxX = 0;
$auxY+= ($pixel+1);
}
$superficieRecorrida+= $pixel*$pixel;
}
//
Header("Content-type: image/jpeg");
imagejpeg($imagen);
imagedestroy($imagen);
Thanks for your future answers. a greeting
How to implement imagebmp() function for monochrome BMP?
I found many implementation for 24bit bitmap but nothing about 1bit bitmap.
This is a working implemenation.
I used a bindec function, I can be optimized using shift operators, but I found very easy the string padding function.
function imagebmp(&$img, $filename = false)
{
$wid = imagesx($img);
$hei = imagesy($img);
$wid_pad = str_pad('', (4-ceil($wid/8) % 4) %4, "\0");
$size = 62 + ( ceil($wid/8) + strlen($wid_pad)) * $hei;
//prepare & save header
$header['identifier'] = 'BM';
$header['file_size'] = dword($size);
$header['reserved'] = dword(0);
$header['bitmap_data'] = dword(62);
$header['header_size'] = dword(40);
$header['width'] = dword($wid);
$header['height'] = dword($hei);
$header['planes'] = word(1);
$header['bits_per_pixel'] = word(1);
$header['compression'] = dword(0);
$header['data_size'] = dword(0);
$header['h_resolution'] = dword(0);
$header['v_resolution'] = dword(0);
$header['colors'] = dword(0);
$header['important_colors'] = dword(0);
$header['white'] = chr(255).chr(255).chr(255).chr(0);
$header['black'] = chr(0).chr(0).chr(0).chr(0);
if ($filename)
{
$f = fopen($filename, "wb");
foreach ($header AS $h)
{
fwrite($f, $h);
}
//save pixels
$str="";
for ($y=$hei-1; $y>=0; $y--)
{
$str="";
for ($x=0; $x<$wid; $x++)
{
$rgb = imagecolorat($img, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$gs = (($r*0.299)+($g*0.587)+($b*0.114));
if($gs>150) $color=0;
else $color=1;
$str=$str.$color;
if($x==$wid-1){
$str=str_pad($str, 8, "0");
}
if(strlen($str)==8){
fwrite($f, chr((int)bindec($str)));
$str="";
}
}
fwrite($f, $wid_pad);
}
fclose($f);
}
else
{
foreach ($header AS $h)
{
echo $h;
}
//save pixels
for ($y=$hei-1; $y>=0; $y--)
{
for ($x=0; $x<$wid; $x++)
{
$rgb = imagecolorat($img, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$gs = (($r*0.299)+($g*0.587)+($b*0.114));
if($gs>150) $color=0;
else $color=1;
$str=$str.$color;
if($x==$wid-1){
$str=str_pad($str, 8, "0");
}
if(strlen($str)==8){
echo chr((int)bindec($str));
$str="";
}
}
echo $wid_pad;
}
}
}
function dword($n)
{
return pack("V", $n);
}
function word($n)
{
return pack("v", $n);
}
I am wanting to get an array of RGB values from an image. E.g. (2 X 2 pix example.)
[[[R, G, B], [R, G, B]], [[R, G, B], [R, G, B]]]
The code I have now:
<?php
// open an image
$image = imagecreatefromjpeg('image.jpg'); // imagecreatefromjpeg/png/
// get image dimension, define colour array
$width = imagesx($image);
$height = imagesy($image);
$colors = [];
for ($y = 0; $y < $height; $y++)
{
for ($x = 0; $x < $width; $x++)
{
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
}
}
print_r($colors);
?>
The above is not working.
My image is now just a 2 X 2 pix jpeg which should output:
[[[0, 255, 0], [255, 0, 0]], [[0, 0, 255], [255, 255, 255]]]
Any help greatly appreciated!
OK, nailed it. Thanks to all.
<?php
$image = imagecreatefromjpeg('image.jpg'); // imagecreatefromjpeg/png/
$width = imagesx($image);
$height = imagesy($image);
$colors = array();
for ($y = 0; $y < $height; $y++) {
$y_array = array() ;
for ($x = 0; $x < $width; $x++) {
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$x_array = array($r, $g, $b) ;
$y_array[] = $x_array ;
}
$colors[] = $y_array ;
}
print_r($colors);
?>
Comments correct, added $r, $g, $b. Restructured #jari answer and now getting a good output.
Cheers!
function getArrayOfPixelsFromFile($source) {
$image = imagecreatefromjpeg($source); // imagecreatefromjpeg/png/
$width = imagesx($image);
$height = imagesy($image);
$colors = array();
for ($y = 0; $y < $height; $y++) {
$y_array = array();
for ($x = 0; $x < $width; $x++) {
//Seleciona a cor localizada em ($x, $y)
$rgb = imagecolorat($image, $x, $y);
//echo $rgb." = ".decbin($rgb),"<br>";
//Seleciona os primeiros dois bytes que representam vermelho
$r = ($rgb >> 16) & 0xFF;
//Seleciona os dois bytes do meio que representam o verde
$g = ($rgb >> 8) & 0xFF;
//Seleciona os dois Ășltimos bytes que representam o azul
$b = $rgb & 0xFF;
$x_array = array($r, $g, $b);
$y_array[] = $x_array;
}
$colors[] = $y_array;
}
return $colors;
}
I fixed your code by creating subarrays and adding elements to it as it should be.
for ($y = 0; $y < $height; $y++)
{
$height_arr = array() ;
for ($x = 0; $x < $width; $x++)
{
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$width_arr = array($r, $g, $b) ;
$height_array[] = $width_arr ;
}
$colors[] = $height_arr ;
}
How about this?
[...]
$colors = [];
for ($y = 0; $y < $height; $y++) {
for ($x = 0; $x < $width; $x++) {
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$colors[$y][$x] = array($r,$g,$b); // or, $colors[$x][$y] = array($r,$g,$b);
}
}
print_r($colors);
I work with python 2.7
I I've a string in DB that represente a image in Hexa. Exemple
sdata = "789C9D953D56C52010856363696D49E90AAC73ECDD4396C25228B210CE711B2CC2CAC622CECC9D0C0321313A27E411123EEEFCC07B7BFF7A9CC45EA9BD507BD6F620F769CAF4FEE3096DB76DDACEAEE9865D4CF79C6DAB34F46D441F7F23F88F6F728E6AD794724EDD5CBB9B790EF53FBF1595D9524C517E93CDEA3A433D984E83440327B318B633BF867A4C12734A5654CE26F24F29AB28704A067685363C665B0582D30ADF0F39A2717F3979C9412A6108A1D731C6992C04BD96252ECB9A2AC4A60F2B07904AA8166C84B51545D172C3C8D02B4CA3D51D841F7584B5CD2E17E2698A5DDE991302AD6240189666558242122D68F1C0F19F99475104D0F7C6216D5A6665AFAED62F8A27730A57E3BC4858669D25716B387BA04E39B41059BCC7E99CEAF4B05F971C75AAB0181AE938111CA9DB9A71C9B5443EA000D4231183A4F8ECEF79E7E5B40E2DEF647BDEA9AB6250EA59F70B6AC90E9FAABFB7D040E43C010107D4F1086A4ADA6D8DA66C8AEDD9C10E3514196A0F060220B59825C843883F5D71A67586809FEDF17FFCD75C4CFC012B43550B"
Now, i want to create the image using this string. I juste know that the image is 24x24 BMP (or PNG?) image.
I know the php code to get it, but not the python.
php :
function imagecreatefrombmpstring($im) {
$header = unpack("vtype/Vsize/v2reserved/Voffset", substr($im, 0, 14));
$info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant", substr($im, 14, 40));
extract($info);
extract($header);
if($type != 0x4D42)
return false;
$palette_size = $offset - 54;
$ncolor = $palette_size / 4;
$imres=imagecreatetruecolor($width, $height);
imagealphablending($imres, false);
imagesavealpha($imres, true);
$pal=array();
if($palette_size) {
$palette = substr($im, 54, $palette_size);
$gd_palette = "";
$j = 0; $n = 0;
while($j < $palette_size) {
$b = ord($palette{$j++});
$g = ord($palette{$j++});
$r = ord($palette{$j++});
$a = ord($palette{$j++});
if ( ($r == 255) && ($g == 0) && ($b == 255))
$a = 127; // alpha = 255 on 0xFF00FF
$pal[$n++] = imagecolorallocatealpha($imres, $r, $g, $b, $a);
}
}
$scan_line_size = (($bits * $width) + 7) >> 3;
$scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 0x03): 0;
for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) {
$scan_line = substr($im, $offset + (($scan_line_size + $scan_line_align) * $l), $scan_line_size);
if($bits == 24) {
$j = 0; $n = 0;
while($j < $scan_line_size) {
$b = ord($scan_line{$j++});
$g = ord($scan_line{$j++});
$r = ord($scan_line{$j++});
$a = 0;
if ( ($r == 255) && ($g == 0) && ($b == 255))
$a = 127; // alpha = 255 on 0xFF00FF
$col=imagecolorallocatealpha($imres, $r, $g, $b, $a);
imagesetpixel($imres, $n++, $i, $col);
}
}
else if($bits == 8) {
$j = 0;
while($j < $scan_line_size) {
$col = $pal[ord($scan_line{$j++})];
imagesetpixel($imres, $j-1, $i, $col);
}
}
else if($bits == 4) {
$j = 0; $n = 0;
while($j < $scan_line_size) {
$byte = ord($scan_line{$j++});
$p1 = $byte >> 4;
$p2 = $byte & 0x0F;
imagesetpixel($imres, $n++, $i, $pal[$p1]);
imagesetpixel($imres, $n++, $i, $pal[$p2]);
}
}
else if($bits == 1) {
$j = 0; $n = 0;
while($j < $scan_line_size) {
$byte = ord($scan_line{$j++});
$p1 = (int) (($byte & 0x80) != 0);
$p2 = (int) (($byte & 0x40) != 0);
$p3 = (int) (($byte & 0x20) != 0);
$p4 = (int) (($byte & 0x10) != 0);
$p5 = (int) (($byte & 0x08) != 0);
$p6 = (int) (($byte & 0x04) != 0);
$p7 = (int) (($byte & 0x02) != 0);
$p8 = (int) (($byte & 0x01) != 0);
imagesetpixel($imres, $n++, $i, $pal[$p1]);
imagesetpixel($imres, $n++, $i, $pal[$p2]);
imagesetpixel($imres, $n++, $i, $pal[$p3]);
imagesetpixel($imres, $n++, $i, $pal[$p4]);
imagesetpixel($imres, $n++, $i, $pal[$p5]);
imagesetpixel($imres, $n++, $i, $pal[$p6]);
imagesetpixel($imres, $n++, $i, $pal[$p7]);
imagesetpixel($imres, $n++, $i, $pal[$p8]);
}
}
}
return $imres;
}
$data = #gzuncompress(pack('H*', $sdata));
$image = imagecreatefrombmpstring($data);
I find this code on http://code.google.com/p/fluxcp/source/browse/branches/fluxcp-1.0/modules/guild/emblem.php?r=696
Can you help me.
EDIT::
A solution
from binascii import unhexlify
import zlib
import Image
from cStringIO import StringIO
data = StringIO(zlib.decompress(unhexlify(sdata)))
i = Image.open(data)
i.show()
The solution (in collaboration with OP) is:
import PIL
from binascii import unhexlify
import zlib
from cStringIO import StringIO
sdata = "789C9D953D56C52010856363696D49E90AAC73ECDD4396C25228B210CE711B2CC2CAC622CECC9D0C0321313A27E411123EEEFCC07B7BFF7A9CC45EA9BD507BD6F620F769CAF4FEE3096DB76DDACEAEE9865D4CF79C6DAB34F46D441F7F23F88F6F728E6AD794724EDD5CBB9B790EF53FBF1595D9524C517E93CDEA3A433D984E83440327B318B633BF867A4C12734A5654CE26F24F29AB28704A067685363C665B0582D30ADF0F39A2717F3979C9412A6108A1D731C6992C04BD96252ECB9A2AC4A60F2B07904AA8166C84B51545D172C3C8D02B4CA3D51D841F7584B5CD2E17E2698A5DDE991302AD6240189666558242122D68F1C0F19F99475104D0F7C6216D5A6665AFAED62F8A27730A57E3BC4858669D25716B387BA04E39B41059BCC7E99CEAF4B05F971C75AAB0181AE938111CA9DB9A71C9B5443EA000D4231183A4F8ECEF79E7E5B40E2DEF647BDEA9AB6250EA59F70B6AC90E9FAABFB7D040E43C010107D4F1086A4ADA6D8DA66C8AEDD9C10E3514196A0F060220B59825C843883F5D71A67586809FEDF17FFCD75C4CFC012B43550B"
fh = StringIO(zlib.decompress(unhexlify(sdata)))
image = PIL.Image.open(fh)
PIL is the Python Imaging Library, and using StringIO, I pretend to have a file-like object.