How to adjust screening (frequency and angle) using Ghostscript? - ghostscript

I have an EPS file that contains c, m, y and k 4 channels.
I try to use Ghostscript ps command to separate it to 4 1bit tiff images.
I don't have any idea about screening function in gs command .
Does anyone known how to adjust screening in gs command?
How to set 1 bit tiff's frequency(lpi) and angle for each separation color?

I presume you mean you have an EPS file, not an ESP file. If so then the EPS program should not contain any halftoning information.
You can use the -c "..." -f sequence to send PostScript directly to Ghostscript, or write PostScript into a file to be processed before your EPS file.
You will find that halftone screening is covered in considerable depth in the PostScript Language Reference Manual, Section 7.4 on page 494 onwards. You can use the old setscreen (one halftone screen only), setcolorscreen (one halftone per plane, Gray, CMYK or RGB) or sethalftone which allows considerably more flexibility.
See also the -dCOLORSCREEN switch described here

Related

Recent MATLAB and Octave have stronger JPEG compression and show artifacts

I wonder why the jpeg compression in recent MATLAB and Octave versions has gone so strong that it causes noticable compression artifacts:
Octave 3 jpeg image with size of 41 KB with no artifacts:
MATLAB 9 jpeg image with size of 26 KB with artifacts:
Octave 5 jpeg image with size of 23 KB with artifacts:
Here is the code to plot:
description = strcat('version-', num2str(version));% find out MATLAB/Octave version
x=1:2; % simple var
figure; % plot
plot(x, x);
title(description);
print(strcat("test_jpeg_size_", description ,'.jpg'), '-djpeg'); % write file
Do you know a possibility to tell MATLAB and Octave to do a weaker jpeg compression. I cannot find anything like this on https://de.mathworks.com/help/matlab/ref/print.html.
I know that I could plot png files and use imagemagick to convert it to jpeg with a given quality but this would be a workaround with additional tools. Or I could png files in the first place but the real images have no compression advantages for png (like like simple one here) and I would have to change a lot of other stuff.
This used to be documented*, I was surprised to not find it in the documentation pages. I tested it with the latest version of MATLAB (R2019b) and it still works:
The -djpeg option can take a quality value between 0 and 100, inclusive. The device option becomes -djpeg100 or -djpeg80, or whatever value you want to use.
print(strcat("test_jpeg_size_", description ,'.jpg'), '-djpeg100');
* Or at least I remember it being documented... The online documentation goes back to R13 (MATLAB 6.5), and it's not described in that version of the documentation nor in a few random versions in between that and the current version.
However, I strongly recommend that you use PNG for line drawings. JPEG is not intended for line drawings, and makes a mess of them (even at highest quality setting). PNG will produce better quality with a much smaller file size.
Here I printed a graph with -djpeg100 and -dpng, then cut out a small portion of the two files and show them side by side. JPEG, even at 100 quality, makes a mess of the lines:
Note that, in spite of not having any data loss, the PNG file is about 10 times smaller than the JPEG100 file.
You can go for
f = getframe(gcf);
imwrite(f.cdata, 'Fig1.jpg')
where imwrite takes the following options
Compression (compression scheme)
Quality (quality of JPEG-compressed file from 0 to 100)
See the doc of imwrite.

Ghostscript command for finding the number of colors used for each page in pdf file

I'm new to GhostScript. Can you let me know the Ghostscript command for finding the number of colors used for each page in pdf file. I need to parse the results of this command from java program
There is no such Ghostscript command or device. It would also be difficult to figure out; so much depends on what you mean. Do you intend to count the colour of each pixel in every image for example ? Which colour spaces are you interested in ? What about ICCBased colour spaces, do you want the component values, or the CIE values ?
[edit]
Yeah there's no Ghostscript equivalent, I did say that.
You wuold have to intercept every call to the colour operators, examine the components being supplied and see if they were no black and white. For example, if you set a CMYK colour with C=M=Y=0 and K!=0 then its still black and white. Similar arguments apply for RGB, CIE and ICC colour spaces.
Now I bet ImageMagick doesn't do that, I suspect it simply uses Ghostscript to render a bitmap (probably RGB) and then counts the number of pixels of each colour in the output. Image manipulation tools pretty much all have to have a way to do that counting already, so its a low cost for them.
Its also wrong.
It doesn't tell you anything about the original colour. If you render a colour object to a colour space that is different to the one it was specified in, then the rendering engine has to convert it from the colour space it was in, to the expected one. This often leads to colour shifts, especially when converting from RGB to CMYK but any conversion will potentially have this problem.
So if this is what ImageMagick is doing, its inaccurate at best. It is possible to write PostScript to do this accurately, with some effort, but exactly what counts as 'colour' and 'black and white' is still a problem. You haven't said why you want to know if an input file is 'black and white' (you also haven't said if gray counts as black and white, its not the same thing)
I'm guessing you intend to either charge more for colour printing, or need to divert colour input to a different printer. In which case you do need to know if the PDF uses (eg) R=G=B=1 for black, because that often will not result in C=M=Y=0 K=1 when rendered to the printer. Not only that, but the exact colour produced may not even be the same from one printer to another (colour conversion is device-dependent), so just because Ghostscript produced pure black doesn't mean that another printer would.
This is not a simple subject.

Automating the cropping of many large image files

I have two thousand large (20mb) image files. Each file has the same height, width, and resolution. The files are currently JPGs, though they are available in other formats.
For each image, I need to extract 6 cropped images from the larger file, each of which are in exactly the same location across the larger files.
I am looking to provide the coordinates of the regions I need extracted (or that I need each larger image cropped to) and then to loop through all 2,000 of the larger images and extract the size sub-images (specified by coordinates), from the larger images.
Is there a program, software package, or straight forward way to do this in python, Java, or some other language?
It seems that ImageMagick is exactly what you need.
ImageMagick has interfaces for Python, C and C++, Perl and Java. See full list of APIs on the page: http://www.imagemagick.org/script/api.php
Also uou can use mogrify utility (often comes with ImageMagick package), which can process images massively. Then your command line might look like this:
mogrify -crop 420x400+365+25 -path '/home/user/images' *.jpg

Ghostscript tiffg3 gray=black

Trying to take a pdf and convert it to a tiff, but make all grayscale into pure black. In other words, if it's not white, it should be black. The tiffg3 device is 1 bit, but it's still taking gray and trying to fake it.
Example command:
gs -dQUIET -dNOPAUSE -dBATCH -r200 -sPAPERSIZE=letter -sDEVICE=tiffg3 -sOutputFile=out.tiff in.pdf
Example input:
Example output:
Desired output:
The 1-bit device uses halftoning (aka screening) to represent shades of gray using only black and white pixels. That's what its intended to do, its not intended to change colours at all.
There are various ways you can fake what you want in PostScript (redefining setgray is one method, also setting a transfer function), but PDF is not a programming language, so this approach doesn't really work.
You could use a custom ICC profile to colour correct gray values so that they are all black. Ghostscript versions 9 and above use Little CMS as the colour management system, and have a 'default' Gray ICC profile which you can override. Presumably it would be easy enough to construct a profile which maps anything other than white to pure black. However, this is not my field. You also need to consider how this is going to affect (for example) images.
There are additional controls based on object type in Ghostscript, so you could optionally only apply this conversion to text, or linework.
Ideally you should go back to the original document, alter it there, and make a new PDF.
You can use convert from imagemagick for that purpose, using the flag -level. For example to convert an image to pure black/white splitting the gray scale in half:
convert -level 50%,50% file.pdf blackwhite.pdf
and then pass it to GS. My suggestion for fax though is to leave part of the grayscale there since many documents rely on it for its legibility:
convert -density 816x784 -level 55%,95% file.pdf blackwhite.pdf
This one leaves a 40% of the grayscale there, converts to black every dot in the range 0-55 (being 0 black and 100 white) and puts everything in the range 95-100 to white, you can play with that and get your best match. The density flag will help it later when converting to tiffg3 in GS giving a better quality in the resulting tif.

PNG to eps conversion massive increase in file size

I'm converting an image from .png to .eps and it hugely increases the file size.
Can anyone explain why this is, and how to prevent it increasing so much.
I'm using Unix convert: convert image.png image.eps
Thanks for any help
Use level 3 postscript if you can. In general, level 3 will produce the smallest files. Level 2 provides the best compatibility, and works well with jpeg images. (see http://electron.mit.edu/~gsteele/pdf/)
convert fig.png eps3:fig.eps
convert indeed blows up the file size because it stores the graphics data uncompressed. A better compression and smaller file size can be achieved by e.g. manually converting using gimp, or by doing
convert image.png image.pdf
pdftops -eps image.pdf
In my case, for example, this results in an eps file with a size of 0.3 MB instead of 5.2 MB (when using convert file.png file.eps directly).
Converting a PNG file (designed for bitmap data) into an EPS (designed for vector data) is always going to result in a larger file size, as the EPS is effectively just wrapping an EPS data structure around the original image data (which it most likely won't store in anywhere near as effective a manner as a PNG file).
The correct solution is to store bitmaps as PNGs and vector graphics as EPS or SVG, etc. (i.e.: Use the appropriate file format for the content in question rather than attempting to impose a meaningless "one size fits all" approach.)
I had a similar problem and solved it using the -sample parameter:
convert image.png -sample 100 image.eps
and tweak the -sample value to fit your need.
Also see ImageMagick v6 Examples --
Resize or Scaling
N.B. The inverse (from EPS/PDF to PNG/JPG) can be tuned using -density.
It is possible that the problem is that the convert application you use just does not support embedding PNG into EPS. When using properly configured Adobe Acrobat Professional I newer got unexpectedly huge increase of the file size. But you should properly configure first your "PNG to PDF" conversion settings. Then you should export (or Save As) generated PDF as EPS from Acrobat.

Resources