In general, there is a jpg image with a white background, it needs to be converted to a transparent one. The language is not particularly important, but it is better to use PCP or C for the application to be cross-platform. The question is, which libraries / languages ​​should I use? I tried opencv, but I think you need to deal with it for a long time, but there is not much time. This option is not to offer too much noise.

<?php if(is_uploaded_file($_FILES["filename"]["tmp_name"])) { echo("success"); move_uploaded_file($_FILES["filename"]["tmp_name"], "/var/www/html/image-creator/".$_FILES["filename"]["name"]); $im = ImageCreateFromPng($_FILES["filename"]["name"]); $col = imagecolorat($im,0,0); imagecolortransparent ($im, $col); imagecopyresampled ($im, $im, 0, 0, 0, 0, imagesx($im), imagesy($im), imagesx($im), imagesy($im)); imagepng($im,"transparent-".$_FILES["filename"]["name"]); $preview = imagecreatetruecolor(88,87); imagecopyresampled ($preview, $im, 0, 0, 0, 0, 88, 87, imagesx($im), imagesy($im)); $col = imagecolorat($preview,0,0); imagecolorset($preview, $col, 255, 255, 255); imagepng($preview,"preview-".$_FILES["filename"]["name"]); imagedestroy($preview); imagedestroy($im); } else { echo("Ошибка загрузки файла"); } ?> 
  • Need to replace white with transparent? chopped off edges, holes in the picture (if there is white on it) do you care? - Sh4dow

1 answer 1

Sorry in advance if you didn’t really understand what your task was and what the problem was.

1) JPG itself does not support transparency. Therefore, you need a PNG for example (at least as an output format) - and most importantly in a search engine to enter PNG instead of JPG. ;-)

2) If you need to cross-platform, then I would recommend Java, since support for images of different formats is directly in the API. Snippets code on the Internet a lot ( for example, a similar question on stackoverflow ) - if it does not help, write - I will find in my own bins.

3) One of the problems of JPG for you will be that the white background is not completely white (that's why you see “a lot of noise”, as I understand it), you need to define some “threshold” background level and check each point - if it is lighter then mask it with transparency.

UPD: Found (your)

 protected void makeImagesTransparent(BufferedImage[] imgs) { for(int i = 0; i < imgs.length; i++) { BufferedImage bi = new BufferedImage(imgs[i].getWidth(), imgs[i].getHeight(), BufferedImage.TYPE_INT_ARGB); bi.getGraphics().drawImage(imgs[i], 0, 0, null); DataBuffer ar = bi.getAlphaRaster().getDataBuffer(); int transpColor = ar.getElem(0); for (int j = 0; j < ar.getSize(); j++) { int color = ar.getElem(j); // примерное условие проверки цвета точки с "порогом" if (((color >> 16) & 0xFF) + ((color >> 8) & 0xFF) + (color & 0xFF) > 700) ar.setElem(j, color & 0x00FFFFFF); } // for imgs[i] = bi; } // for } // makeImageTransparent 

4) And yes, the white color in the center will also be replaced with a transparent one; if this is undesirable, then it is necessary not only to go to all points, but to use the fill algorithm. The truth is it is not very good in the example fit.

  • Something like “take the point 1.1 and recursively search for 8 people around the match”, the simplest thing is Sh4dow
  • thanks, it worked. - xcorter