S
S
Sergey2014-02-23 12:18:11
PHP
Sergey, 2014-02-23 12:18:11

How to replace the white background around the pictures with a transparent one?

Good day to all!
There are many thousands of pictures in a bunch of directories with subdirectories. Pictures - products with a white background around. I'm looking (ideally) for a php program or script that in all files in all subdirectories will be able to replace the white background around the pictures without affecting just the white color in the pictures themselves. This is ideal. In extreme cases, you can replace the entire white color with a transparent one. Or, in the most unfortunate case, crop the white background strictly according to the square with the size of the picture.
And it's quite a complication of the task: in some files I noticed that some areas of the background may not be #ffffff, but #fdfffe. Those. a couple of units different from pure white.

Answer the question

In order to leave comments, you need to log in

22 answer(s)
T
topbanana, 2014-02-23
@topbanana

Take 10-15 pictures from your database, open them in a graphics editor, and fill the white background with a contrasting color. In normal editors, the fill has settings so that it considers white not only #ffffff, but also allows some deviations, like #fdfffe.
I think the result will not suit you, because the edges of the picture will still have a white border, or the fill will climb onto the object itself with a large tolerance of deviations from white. This is especially noticeable in the shadows. This is how the result of automatically replacing white with transparent will be.

Y
Yuri Lobanov, 2014-02-23
@iiil

You will not be satisfied with the result. I advise, since there are so many images, fix the page template so that the picture on a white background looks normal in it. It's quite real.

K
Kerman, 2014-02-23
@Kerman

A naive Chukchi youth. People from adobe (and not only) have been struggling with the task of changing the background for decades. The chromakey technology for filming was invented, but it is not perfect either.
And he is going to find a ready-made library, and if he can’t write it in two evenings.
One glare along the edge of the product - and all the background change algorithms smoke on the sidelines. And if something fluffy around the edge - it's generally a nightmare. And reflective objects become unnatural when placed against a different background, as any photographer will tell you. If the contour is blurred, then only complex, very complex algorithms for separating light from the background will help here.
Briefly speaking. Changing the background is possible if the object is matte, has a clear dark outline, everything is in focus and there are no glare along the edge. In this case, you can use Photoshop batch - Layer -> Matting -> Remove white matting.

A
Alexey Timofeev, 2014-04-13
@phtimofeeff

I took your photo and, using its example, I will show you what will happen if you do everything in automatic mode ...
Next, I will give 4 results with varying degrees of white tolerance, and for clarity, I put a red background instead of transparency, so it will be clearer and clearer :
Automatic white selection with a tolerance of 1:
Automatic white selection with a tolerance of 2:
Automatic white selection with a tolerance of 4:
Automatic white selection with a tolerance of 8:
These are the results. And each photo will be different.
Everything depends very much on the run-up of white.

M
Mikhail Osher, 2014-02-23
@miraage

If they are equally spaced, then Photoshop macros might work.
Not strong in graphics/design.

S
SazereS, 2014-02-23
@SazereS

The simplest thing that came to mind was a self-written script in any language. Recursively sort through the files in the folder, replace white with transparent and save to another directory (so as not to spoil the source code)

T
tosteruser, 2014-02-23
@tosteruser

Under Windows there is a FastStone Photo Resizer program . A lot of things he can do in bulk with images. Perhaps it will help.

M
MadMapGuy, 2014-02-23
@MadMapGuy

I understand that your pictures are in the shape of a square, and you would like to make the background transparent around.
To do it programmatically, I'm not sure that this is in principle possible without any artifacts in the image. (unless the pictures are vector, but again, there is a certainty that you have a raster).
So, the only way out is the magic wand tool, in any more or less normal graphic editor, and a simple cutting out of the background + light finishing of the edges with a brush with transparency.

A
Andrey Tokmakov, 2014-02-24
@NPC

Is the white background the same everywhere as the frame?
Then:
1. Why was it necessary to do it, if it was possible to make a frame using styles?
2. Is it necessary to replace white with transparent? Or, again, you can simply crop it, and then make styles at least a transparent frame, at least a colored one and of any size.
If this background in the form of a frame is the same everywhere, then you can use php to take the original image, copy it to a new one, but not completely, but for example, starting from 5px (top and left) and ending 5px (right and bottom) before the image ends (if the frame is 5px) and save with a replacement there.
ps if this option is suitable, then it does not matter what color the background is, the main thing is that the size is suitable.

A
Alexey Timofeev, 2014-03-24
@phtimofeeff

in jpeg this is definitely not possible, or rather, of course, but the result will not even be very interesting, firstly, white by eye, not always white in reality, and secondly, the objects themselves may contain white color and overexposure zones, which are also in fact, they are white ...
so there is no unambiguous solution to this issue.
but I always suggest my clients to shoot on a black background, it is better in this regard.
here it is still possible to cut strictly in a square, and then there is no guarantee that the object will not be cut, it is better to do it with pens. alas, but manual labor in photography is the basis of the fundamentals ...

S
Sergey, 2014-03-24
@sergey_privacy

> in jpeg it's definitely not possible, or rather it is possible of course, but the result will not even be very interesting,
What does it mean possible? In jpg transparency? The first time I've heard. gif, png - it's understandable, but there was no spawning in jpg.
> firstly, it is white to the eye, not always white in reality, and secondly, the objects themselves can contain white color and overexposure zones, which are also actually white ...
I was engaged in pixel-by-pixel photo processing. Line by line, pixel by pixel, you get the color code, you compare. You can set the run-up of the range of each channel to 5-15 units and replace all these colors with transparent. The program will be small, a couple of pages. I just don't want to deal with this hemorrhoids anymore.
> so there is no definitive solution to this issue.
but I always suggest my clients to shoot on a black background, it is better in this regard.
The answer is, there is nothing magical about the algorithm. And it is necessary to build on the already existing situation.
> here you can still cut strictly in a square, and then there is no guarantee that the object will not be cut, it's better to do it with pens. alas, but manual work in photography is the basis of the basics ...
What kind of program is it that crops along with the image? This is some nonsense. Pulling out the left, right, upper and lower borders of the image is a task at the level of the first year of the institute. The usual finding of extrema of a two-dimensional array.

S
Sergey, 2014-04-11
@sergey_privacy

I would try in Photoshop through automation - one is done, while a macro is recorded, then processing the entire folder. it's quite simple and fast.
I'm not that deep in Photoshop climbed, so I'll take an interest. Can it only process within that folder or with a tree of subfolders? I have several thousand files and a complex structure of subfolders of the order of a hundred. Can recursively process everything?
Naturally, he will not be able to repeat all the actions.
I guess the idea with my program comes to my mind more and more often. Looks like it's going to take a few days.
still as an option - a macro, and part of the actions by hand. those click "magic brush" manually, then the keyboard shortcut that runs the macro.
Will it work for nested folders?
1. be sure to make a copy - you will screw up a lot
So not my choice. That's why I ask the question so that the spoiled photos turn out to be in an amount of less than 1%
2. "transparent", as correctly answered above, there is in png, gif, but not in jpg
3. what kind of images do you have??? it is very difficult to work as a telepath - a couple of examples of the most typical ones would be shown. or at least similar.
what format and so on, again, how many images ...
Since I'm talking about transparency, then the photo is in one of the indicated formats. gif to store real data is, I believe, left over from the 90s. For sprites, logos or design elements, it will still be used for some time. left png. As I said above, contrasting photos of goods on a white background like this Does the 294a7a420178f225e50c7b2ca41fa671.JPG
presence of a photo somehow cancel the meaning of the phrase "contrasting photos on a pure white background"?
in general, there are many questions, without answers to which we cannot advise something more specific.
There are many questions and comments that are not to the point, and the phrase "we are not telepaths" already tells me that a solution will not be offered.
and so .... purely theoretically - you can write a program that will change only the background
, but it will cost you more than putting 10 students to photoshop with your hands for a couple of weeks ....

It will take me about 3-4 full days of work to write the program. And students still have to pay and they will definitely make the work worse.

S
Sergey, 2014-04-13
@sergey_privacy

the photo that you have in the example should be processed normally,
but where is the guarantee that everything will be processed normally?
Photos of the same type, a normal tool should cope with all equally.
deep???? it's not deep. Unfortunately, I'm at home, and there is nothing here except Paintnet and Gimp. In Photoshop, "automation" is a very simple and convenient tool. There is an analogue in Gimp, but it is more difficult to "understand".
I know how to use layers in Photoshop, the main tools at the level of a novice user. Dealing with automation is not a problem, I just haven’t looked in this direction before.
ALWAYS make backups. something glitches during processing - will you re-create everything?
I work as an administrator, backups are the first commandment. Everything you need is stored in a dozen places.
now if you find how to poke a magic wand in Photoshop into the corner of the image automatically ...
And open each photo manually, apply a macro, then save it? This will take months. Need a recursive traversal of directories.
about the solution.
do everything for you? isn't it too fat? you get money for it.
say thank you for suggesting where to dig

What are you excited about? Did I ask you to write a program for me? I asked if someone knows a finished product that has similar functions? Or is it too hard work to say the name of a program you know?
Some time ago, there was a need for mass file renaming and format conversion. Asked for advice, immediately threw a few names of existing products. Now the situation is the same and I do not understand what kind of problem you are developing. Do not know the name - well, figs with him. I did not force anyone to write the program.

S
Sergey, 2014-04-13
@sergey_privacy

That's why I said that you need to replace not all the white color, but only around the image. The task is one to one, like captcha recognition. Those. we calculate the edges of the image that are different from the background, we discard the background. We do not touch everything inside the contours. There are a lot of algorithms, I think the software is ready. It remains to find the name.

A
Alexey Timofeev, 2014-04-14
@phtimofeeff

To make it clearer to you, I cut off the extra range and expanded the range of white to the entire spectrum...
As a result, it came out bright and intelligible:
Here is your actual white in the picture...;)
And all this is solely due to the peculiarity of the JPEG standard.. .

S
Sergey, 2014-04-14
@sergey_privacy

The task is not urgent, as long as I plan to remake the site. While hope has not died, I will try to find a ready-made solution. I really do not like to process graphics pixel by pixel.
A couple of tens of thousands of photos in hundreds of catalogs will take much longer. Even one minute per file will already take 2 full weeks of time. The whole site does not cost as much as I will give for processing photos.

S
Sergey, 2014-04-15
@sergey_privacy

What exactly should I be trying to do? Manually photoshop tens of thousands of photos? Thank you, please! Enough experience with a couple of hundred photos.
Write the program yourself? I already had a similar experience, I wrote pattern recognition like that. The source code has not been preserved, it will have to be written again. I know that I can write the same program, but I also remember all the hemorrhoids in developing and debugging the algorithm. So I decided to wait for now.

S
Sergey, 2014-04-15
@sergey_privacy

For starters, stop being rude.
Writing a program in 20-30 minutes will not work, it will take several days.
I have not yet seen a working version in the form I need, there is nothing to try.
For 30 bucks, a student will not undertake to process 20-30 thousand photos, especially in a bunch of subdirectories.
As for the ineptness - keep your unqualified opinion to yourself. Without knowing the situation, making a diagnosis is bad manners. There is a site, the site works, I want to do better without wasting time and money. While I'm working on a bunch of other projects and I'm waiting for someone to appear among the trolls like you, who, instead of a bunch of crutches, will tell you the name of a normal program. The set of crutches that you offer does not suit me.

S
Sergey, 2014-04-15
@sergey_privacy

for starters, you show yourself as a clumsy who categorically refuses to try something
even from chewed
I worked with photoshop and I know what kind of garbage it will turn out. The magic wand requires careful selection of tolerance parameters. Slightly mistaken - the selection will climb onto the image itself. The result will be bad, a few percent of the images will be messed up. And I will have to manually look through all the pictures, go through the entire directory tree. Why waste time on an obviously bad result?
for 30 bucks, a student will gladly undertake to write that "program" that I threw above
because the price for it is less than an hour of working time. and that's 10 bucks for a student.
20 more - for machine time - I think a computer with a good percentage will do everything overnight.
And then I again look at all the pictures myself, find a huge number of bugs and return to the original conditions.
you don't want/can't. it's not just a diagnosis. this is a verdict. you can't work at difficult jobs - you don't know how. go dig trenches. although you have to think the same... ;-(
Like I said, you are a jerk.

S
Sergey, 2014-04-15
@sergey_privacy

SEOs are such SEOs. they don’t know anything, they don’t know how to do anything, they want someone to do everything for them, and they only rowed grandmas ...

Who said I'm an SEO?
I am a network administrator for 5000+ users with more than 15 years of experience, a programmer with programming experience in more than 10 programming languages. Certificates I have on the floor of the wall. Website building is more of a hobby, sometimes I help friends and acquaintances. Experience and knowledge in the field of IT is enough to understand that your option is a set of crutches with a deliberately bad outcome, which will require hundreds of man-hours of work to check and correct imperfections. If your qualifications and knowledge are not enough to consider other options - your problem. The decision sucks, I don't consider it. And stop counting money in other people's pockets. Whining is just humiliating.

S
Sergey, 2014-04-16
@sergey_privacy

2Arlakz: stop trolling, just back off. It's none of your business where I spend money, how I earn it, who I pay, how much I pay, and I'm not going to ask your permission or advise what I should do next. You don't know anything about me and you're useless except for rudeness. Your phrase "difficult task" or "too difficult task" shows that you are not in the subject at all. Why should I argue with an amateur? I wrote software for real-time photo and video recognition, and I tell you that this is a task in its current form for a student of the institute. Highlighting the contours of a contrasting image with sharp edges is bullshit. The link to the program that you provided is really a very difficult and painstaking work. And I myself am not going to pay decent money to students for a coven with an unpredictable result. Only recently I received a set of about 2-3 thousand pictures processed by such a student. I had to redo several dozen photos myself, do I need it? Especially if you received a couple of bottles of cognac from friends for the site, and you have to pay for the improvement from your own pocket an amount exceeding the cost of this cognac.

S
Sergey, 2014-04-21
@sergey_privacy

>> especially if you received a couple of bottles of cognac from friends for the site, and you have to pay for the improvement from your own pocket an amount exceeding the cost of this cognac.
>Here you can see the price of your "work". your hour is worth less than a baseboard.
If your Jewish roots prevent you from doing something for your friends for free, then the problem is only in your Jewish roots. Or not having friends. I helped my friends do repairs in the apartment, and build a private house, and set up computers, and pulled the computer grid, and had to make websites. My friends did a lot of things for free in return. As a symbolic retribution - beer, cognac, barbecue. The work that I do for money does not concern you at all and has nothing to do with website building. Therefore, as usual, you know where you got your finger about dismissals and compliance with the position. A lot of idle talk and insults are not on the merits of the issue.
> what TK you wrote - you got such an answer
To begin with, it was not a TK, but a question. I'm just looking for the name of a program with such a function. I don't need a set of crutches that does a bad job and takes hundreds of hours of my time. Your decision is just that. And since you cling to him so much, it means that you don’t have enough qualifications to consider other options. What is there to argue about then? Moreover, the picture for an example was provided, the description of the task was quite voluminous. Nobody specified any additional data requiring telepathy and, in principle, they are not required.
> judging by "I had to redo" the TK, you never learned how to compose it. Yes, and the contract probably did not draw up with them ...
What contract, what TK? Friends had a problem that they asked to help solve with the initial data. I helped them, in the design of the site everything looks great, they are quite satisfied. I myself can and want to make the design better, but the current look of the pictures does not suit me. There will be a good solution, not consisting of crutches, not requiring hundreds of hours of work or investing my money - then I'll do it.
So stop trolling. If you don't know a normal solution - do your own education instead of being rude to people. Everything, the beads are over, the hand is tired of throwing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question