How can I change my image's depth?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
cnhome

How can I change my image's depth?

Post by cnhome »

I have an picture with the fomat "jpg" which depth is 8. So I write the following code:

string file_name = "d:\\test.jpg";
image.read(file_name);
image.write("d:\\test0.jpg");

I find the image "test0.jpg" which depth is 24. I want to konw how can I control the depth. Anyone can help me?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How can I change my image's depth?

Post by snibgo »

Both these images are probably 8 bits per channel, 3 channels, so 24 bits per pixel.
snibgo's IM pages: im.snibgo.com
cnhome

Re: How can I change my image's depth?

Post by cnhome »

I can see the image's property in windows system. One is depth 8, and other is depth 24.

In my code, I add some code to show depth info, as following:

string file_name = "d:\\test.jpg";
image.read(file_name);
image.depth(); //<------------this is 8
image.write("d:\\test0.jpg");
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How can I change my image's depth?

Post by snibgo »

Can you post URLs to test.jpg and test0.jpg?
snibgo's IM pages: im.snibgo.com
cnhome

Re: How can I change my image's depth?

Post by cnhome »

I can email to u

May I?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How can I change my image's depth?

Post by snibgo »

It's better to put them on the web, so anyone can comment and learn.

But if the images are confidential, you can email them to me: snibgo at earthling dot net.
snibgo's IM pages: im.snibgo.com
cnhome

Re: How can I change my image's depth?

Post by cnhome »

My main() code as following:

InitializeMagick("C:\\Program Files\\ImageMagick-6.6.2-Q16\\");
Image image;
// Read a file into image object

string file_name0 = "d:\\test.jpg";
image.depth(8);
image.read(file_name0);
image.depth(8);
cout<<image.depth()<<endl;
image.write("d:\\test10.jpg");

test.jpg-> code a722ccfa
http://61.dc.ftn.qq.com/ftn_handler/2c8 ... 309be66c7d

test10.jpg-> code d1e59c7f

http://61.dc.ftn.qq.com/ftn_handler/9f2 ... 49b64ef3e9
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How can I change my image's depth?

Post by snibgo »

Your input file (test.jpg) has 32 different colours. ImageMagick reads it, decompresses, then compresses again. Jpeg compression will generally change the values of some pixels. If you save it as "quality 100" there are fewer changes. For this file, the number of colours does increase, but not beyond 255.

If you dislike jpeg messing with the number of colours, use a lossless format (such as PNG) instead.
snibgo's IM pages: im.snibgo.com
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: How can I change my image's depth?

Post by Drarakel »

cnhome wrote:test.jpg-> code a722ccfa
That is a GIF file!!
If you convert a GIF with a 8bit palette to JPG :), it has to be stored with 3x8 bit (unless the image is grayscale - but this one isn't).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How can I change my image's depth?

Post by snibgo »

That is a GIF file!!
Well spotted. So it is.
snibgo's IM pages: im.snibgo.com
cnhome

Re: How can I change my image's depth?

Post by cnhome »

My mistake. Thanks a lot! :)
Post Reply