How to save 8-bit PNGs with ChunkyPNG or RMagick - ruby

I've been trying to save 8-bit PNGs (PNG8) using RMagick (http://rmagick.rubyforge.org/) and ChunkyPNG (https://github.com/wvanbergen/chunky_png) but have been unable to do so.
However the only time I can get it to work on RMagick is if the ImageMagick installation is based on the QuantumDepth of 8 rather than the usual 16. It is not possible to change this setting on the fly - the installation of ImageMagick has to be compiled with this setting.
Also setting the depth to 8 when creating the image or prefixing a format type when saving have no effect.
ie. img.write('PNG8:image.png')
Anyway I've had a look at ChunkyPNG and I really prefer to use this over RMagick - simply because it is pure Ruby and doesnt depend on ImageMagick. I can't save a PNG8 using that too.
I have to convert the PNG to 8bit afterwards using a graphics program -
My questions:
Is there a way of saving 8bit PNGs properly like it does on ImageMagick Q8 on a machine with ImageMagick Q16 installed?
Can anyone provide pointers as to do my own 4-bit encoder in ChunkyPNG or know of a way to save PNG8 with it?
Thanks in advance..

What exactly do you mean by PNG8? 8-bit grayscale, 8-bit indexed color, 3x8 bit RGB or 4x8 bit RGBA? All of these color modes are supported by ChunkyPNG.
By default, ChunkyPNG tries to determine the best color mode to save your image. You can overwrite it by providing an options hash to the save method:
image.save('filename.png', color_mode: ChunkyPNG::COLOR_TRUECOLOR)
# Or: ChunkyPNG::COLOR_TRUECOLOR_ALPHA
image.to_blob(color_mode: ChunkyPNG::COLOR_INDEXED, bit_depth: 8)
More info: https://github.com/wvanbergen/chunky_png/wiki

Related

Remove meta information in picture using "convert" without changing the colors of the picture

I have a problem using the tool convert (imagemagick) to remove metainformation of pictures to make them smaller for faster delivery in websites.
When I use the convert command with following parameters:
convert -format png -strip pic1.png pic2.png
then the converted picture is much more darker than the original one.
I also tried to require the colorspace of the original picture with:
convert -format png -colorspace sRGB -strip pic1.png pic2.png
but it's the same problem.
Has anyone an idea how to solve it?
These are the example pictures:
(Original) Pic1:
(Converted) Pic 2:
The problem is the installed version 6.7.7 of ImageMagick (Standard-Version of Linux Mint 17)
In version 6.4 and (said in comment) in version 6.9 it works.
So the 6.7.7 version is buggy in that case.

MiniMagick's "strip" function makes picture filesize bigger

I have used MiniMagick to compress JPEG files.
With strip function, I want to get rid of EXIF from image. So, I do:
image = MiniMagick::Image.open("my_picture.jpg")
image.strip
image.write("my_picture_small.jpg")
but sometimes the size of my_picture_small.jpg is bigger than my_picture.jpg.
However, when I don't use the strip function, like
image = MiniMagick::Image.open("my_picture.jpg")
# image.strip
image.write("my_picture_small.jpg")
my_picture_small.jpg's size is smaller.
That situation happened with some picture deal with Photoshop and in my CentOS computer, but run well with my Macbook. I don't know why stripping some information led to more storage.
Can anyone explain it?
Have found that ImageMagick will recompress image even if it with any arguments, such as
convert image.jpg new_image.jpg
new_image.jpg will be different from image.jpg more or less. If image.jpg is from a phone or camera or a image processing tools, the degree of difference is also different.
So compress images with MiniMagick or Rmagick that use ImageMagick as there system support, just do convert -strip image.jpg new_image.jpg may led to a unexpected result, avoid to use MiniMagick command if there is no need to greatly compress file.

Uncrush PNG image on ubuntu?

IPA image uses pngcrush to compress PNG image, but I want to uncrush a PNG image on Ubuntu.
Can anyone give me any idea?
The standard PNG utility pngcrush has been modified by Apple, which makes it produce technically invalid PNGs: a new chunk is inserted before the mandatory first chunk IHDR, RGB(A) order of pixel data is inverted, and RGB pixels get premultiplied with their alpha.
Hence, I'd rather call these PNGs "fried", rather than just "crushed".
Try my own pngdefry. The source code is written on a Mac OSX machine but it should be compilable for other OSes as well; it's pretty straightforward C code.

ruby - Dragonfly - Force CMYK to RGB conversion when doing thumbnails

I'm using a nice enough cms (locomotive(github)) to allow some non-tech savy users to upload pictures to the system. The program is able to resize and crop pictures to any given size.
Trouble is, Internet explorer doesn't know how to deal with CMYK-encoded images. The users of this app are not exactly tech-savy; asking them to transform their images from CMYK to RGB is not an option. I'd like to modify locomotive so that it does the change automatically. I've been trying this for some hours but had no luck so far.
This is what I have found:
Locomotive uses dragonfly to perform the resizing.
Concretely, it uses dragonfly's imagemagick module.
The file that defines how Dragonfly is used in locomotive can be found here.
There is also a dragonfly initializer file.
I have also found that what (think) I need is adding a -colorspace RGB option to the parameter sent to Imagemagick by Dragonfly. It doesn't look like Dragonfly provides an easy option to do that.
I've tried several things, the last one consisting on monkeypatching Dragonfly's Imagemagick Processor so that the -colorspace RGB option is always used. I've added this in locomotive's config/initializers/dragonfly.rb:
# locomotive's config/initializers/dragonfly.rb
# ... Locomotive's default initialization
module Dragonfly
module ImageMagick
class Processor
alias :old_convert :convert
def convert(temp_object, args='', format=nil)
args += ' -colorspace RGB' # force RGB in all thumbnails
old_convert(temp_object, args, format)
end
end
end
end
I was pretty sure that this should work, but unfortunately it doesn't. And I have run out of ideas. Can anyone help?
On the commandline, I sometimes need to add -type truecolor to make colorspace conversions work reliably:
convert cmyk.jpeg -colorspace rgb -type truecolor rgb.jpeg
Maybe you try to add it in your code as well?
From the related list to the right, might this SO answer help?
Properly converting a CMYK image to RGB with RMagick
Unfortunately there doesn't seem to be a straightforward way to do this with Dragonfly. I've given up.

Reduce bit-depth of PNG files from the command line

What command or series of commands could I execute from the CLI to recursively traverse a directory tree and reduce the bit-depth of all PNG files within that tree from 24bpp to 16bpp? Commands should preserve the alpha layer and should not increase the file size of the PNGs - in fact a decrease would be preferable.
I have an OSX based system at my disposal and am familiar with the find command so am really more keen to to locate a suitable PNG utility command.
Install fink
Say "fink install imagemagick" (might be "ImageMagick")
"convert -depth 16 old/foo.png new/foo.png"
If that did what you want, wrap it in a find call and be happy. If not, say "convert -help" and RTF-ImageMagick-M. :)
Optional: "fink install pngcrush" and run that as a second pass after the convert pass.
AFAIK the only PNG format that supports the alpha layer is PNG-24; Reducing the PNG to another format may require specifying a transparent color in a CLUT, which will not give you the output you want.
From the feature list on PNG's website:
8- and 16-bit-per-sample (that is, 24- and 48-bit) truecolor support
full alpha transparency in 8- and 16-bit modes, not just simple on-off transparency like GIF
... which I read to mean that anything other than PNG-24 or PNG-48 does not support full alpha transparency.

Resources