Convert PSD files with alpha channel to transparent background

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
johnbasi
Posts: 14
Joined: 2017-12-08T02:45:45-07:00
Authentication code: 1152

Convert PSD files with alpha channel to transparent background

Post by johnbasi »

Hi,

I'm sorry for asking this question but I am very new to IM.

I have these PSD files(a lot of them) with Alpha channels and I want them to be converted into PSD files again but with transparent or white background.

How can I come up with this? I tried this command provided by the company but it is not working.

Code: Select all

composite.exe -compose Dst_In -gravity center art.psd[0] art.psd[1] - | convert.exe - -colorspace rgb -background white -gravity center -resize 1000x1000 -extent 1000x1000 -adaptive-sharpen 0 -quality 80 -strip art.jpg
I have these sample images below. I am not an expert with PS and I can't give you what the output should be. I just want it to be transparent and if it is not possible then white background will do.
https://drive.google.com/open?id=1w2_E5 ... 7eIO5G9CXn
https://drive.google.com/open?id=1C0Pe4 ... iFaWdCTlHF

IM version: ImageMagick 7.0.1-3 Q16 x64 2016-05-11 http://www.imagemagick.org
GhostScript: 9.22
Plaform: Windows
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert PSD files with alpha channel to transparent background

Post by fmw42 »

I cannot access your images because one needs to log-in and I do not have an account there. Can you post them so that one does not need a log-in or to some other public hosting service.

Your command looks like you ultimately want a jpg output. It would be easier to convert the PSD flattened layer [0] directly to jpg.

Does this work:

composite.exe -compose Dst_In -gravity center art.psd[0] art.psd[1] miff:- | convert.exe - -colorspace rgb -background white -gravity center -resize 1000x1000 -extent 1000x1000 -adaptive-sharpen 0 -quality 80 -strip art.jpg
johnbasi
Posts: 14
Joined: 2017-12-08T02:45:45-07:00
Authentication code: 1152

Re: Convert PSD files with alpha channel to transparent background

Post by johnbasi »

Sorry about the link. Please try the new ones below.
https://www.dropbox.com/s/hoj6pwvnomleh ... 1.psd?dl=0
https://www.dropbox.com/s/ms8vaficqed7o ... 0.psd?dl=0

Sorry about that command I just copy and pasted it without changing the output. But I really want it to remove the background from the psd file and output it again as a psd but with White background.

I tried your command but doesn't work. It still has a background of light gray and blue for the two images.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert PSD files with alpha channel to transparent background

Post by fmw42 »

Your file has a fully opaque alpha channel according to identify -verbose 44-2868_11.psd. So the best I can do the convert it to transparent background is to change the blue color to transparent as follows using IM 6.9.9.26 Q16 Mac OSX.

Code: Select all

convert 44-2868_11.psd[0] -fuzz 10% -transparent "#4CDEFE" result.psd
This seems to work for me for this image.
johnbasi
Posts: 14
Joined: 2017-12-08T02:45:45-07:00
Authentication code: 1152

Re: Convert PSD files with alpha channel to transparent background

Post by johnbasi »

There would be lots of psd files with different backgound on it. Is their a way to make it work on all of them?
I am very sorry. I am new to this and I don't have background on PS either.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert PSD files with alpha channel to transparent background

Post by snibgo »

The two PSD files have two different images. The images have the same object, but with different backgrounds. I think the task is to make an image that contains the object but with a blank (or white) background:


convert 44-2868_10.psd[0] 44-2868_11.psd[0] ( -clone 0-1 -compose
Difference -composite -fill White +opaque Black -negate ) -delete 1 -alpha off
-compose CopyOpacity -composite x.png

convert 44-2868_10.psd[0] 44-2868_11.psd[0] ( -clone 0-1 -compose
Difference -composite -fill White +opaque Black -negate ) -delete 1 -alpha off
-compose CopyOpacity -composite -background White -compose Over -layers Flatten
x2.png

Each command is one line. This is Windows CMD syntax. For bash, escape the parentheses \( and \).

For PSD output, change the output filenames to .PSD. (I can't test that.)
snibgo's IM pages: im.snibgo.com
johnbasi
Posts: 14
Joined: 2017-12-08T02:45:45-07:00
Authentication code: 1152

Re: Convert PSD files with alpha channel to transparent background

Post by johnbasi »

@snibgo, you're correct. I need to extract the object and make the background blank(or white) when converting the file. Your 1st command works great and it removes the background when converting to PSD file. But I noticed that you combined the two PSD files. When I try to use the same file using
convert 44-2868_10.psd[0] 44-2868_10.psd[0] and so on
it won't convert to transparent background anymore.

The attached files are just the sample files wherein the user will feed the system and convert it to blank or white background(whichever they want in the future). There are lots of PSD files with different background and I need them to work independent from each other.

Is there a command to extract the object that has alpha channel in it?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert PSD files with alpha channel to transparent background

Post by snibgo »

Each PSD file contains two images. The second is identical to the first. (At least, the two images that IM gets from the file are identical). All four images (two from each file) have an alpha channel, but it is fully opaque.

The command I showed need two images with identical foreground but different background. If you have images with transparent backgrounds, the task is far simpler.
snibgo's IM pages: im.snibgo.com
johnbasi
Posts: 14
Joined: 2017-12-08T02:45:45-07:00
Authentication code: 1152

Re: Convert PSD files with alpha channel to transparent background

Post by johnbasi »

Yes, it would be easier if the images has transparent backgrounds and that's what I'm trying to achieve. This is a really tricky task and I've spent so much time on this already. :(

Anyways, thanks for the help. But I need the images to work independently from each other. You solution works great but it is not applicable in my scenario. If you have any other solution to get the main object please let me know. :)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert PSD files with alpha channel to transparent background

Post by snibgo »

Okay, so you get single images from users, and want to remove backgrounds in general. That is possible if there is some way to distinguish background from foreground. In your "gray background" image, the foreground and background almost merge together towards the top-left. Separation is possible (find the slope magnitude; threshold at 1%; remove noise), but that method would probably not work on some other images.

I don't know a method that is guaranteed to remove any background. I think the best you can do is apply many methods, then choose the best result. There might be an automated method to choose the "best".
snibgo's IM pages: im.snibgo.com
johnbasi
Posts: 14
Joined: 2017-12-08T02:45:45-07:00
Authentication code: 1152

Re: Convert PSD files with alpha channel to transparent background

Post by johnbasi »

In your "gray background" image, the foreground and background almost merge together towards the top-left. Separation is possible (find the slope magnitude; threshold at 1%; remove noise)
Maybe this is possible. Do you have a script or command for this Separation thingy?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert PSD files with alpha channel to transparent background

Post by snibgo »

Windows BAT syntax for slope magnitude and threshold:

Code: Select all

convert 44-2868_10.psd[1] -profile sRGB.icc -strip x1.png

call %PICTBAT%slopeMag x1.png

%IM%convert ^
  x1_sm.png ^
  -threshold 1%% ^
  t.png
For slopeMag.bat, see Details, details.
snibgo's IM pages: im.snibgo.com
Post Reply