I have the following code on my site where i have to create images out of uploaded pdf files, my problem is that i cannot get the colors right (and yes there can be both text and images in the pdf files):
$img = new imagick();
$img->setResolution(72, 72);
$img->readImage($dir_f.$file);
$img->set ImageCompression(imagick::COMPRESSION_NO);
$img->setImageCompressionQuality(100);
$num_pages = $img->getNumberImages();
for($i = 0;$i < $num_pages; $i++) {
$fina l_name = str_replace(" ", "+", basename(str_replace(".".$file_ext,"", $file_name)));
$final_name = preg_replace("/[^a-zA-Z0-9-+]/", "", $final_name);
$save_to = $pdf_dir."/".str_replace(".".$file_ext,"", $final_name).'-'.$i.'.png';
$img->setIteratorIndex($i);
$img->setImageCompression(imagick::COMPRESSION_NO);
$img->setImageCompressionQuality(100);
$img->setimageformat("png");
$img->writeImage($save_to);
$file_image = str_replace(dirname(__FILE__)."/../../", "/", $save_to);
$file_images[] = array($file_image, $index[$ii]);
}
$img->destroy();
I have gotten different results depending on wich type of colorspace i have been using but non of them are correctly made colors.
when i dont set a colorspace it gets to bright.
when i use colorspace srgb or cmyk the image gets to bright,
when i use colorspace COMPRESSION_NO it gets to bright
when i use colorspace rgb it gets to dark
Hopefully someone is able to help me out here.
This looks like it's a bug in Ghostscript, which is the delegate program ImageMagick normally uses to convert PDFs to bitmap format.
Using Ghostscript version 8.70 I see the same output you are seeing, i.e. the image is lighter than it is when viewing the PDF directly.
After downloading Ghostscript version 9 and running the command below to do the conversion, I see the same colours as in the PDF.
You're probably going to need to either upgrade to Ghostscript 9 or figure out what is special about the PDF that makes Ghostscript not convert it correctly. I think the PDF might either have a non-standard color profile or weird colorspace. But as converting it with GS9 works fine, that is probably the easiest way to fix the problem.
./ghostscript-9.16-linux-x86_64/gs-916-linux_x86_64 -q -dQUIET -dSAFER -dBATCH \
-dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 \
-dAlignToPixels=0 -dGridFitTT=1 -sDEVICE=pngalpha \
-dFirstPage=1 -dLastPage=3 \
-dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r72 \
-sOutputFile=gs-%d.png 153311-flyers-sample-papers-volume-2.pdf
Related
I'm testing a simple script as shown below to convert JPG to PDF, but somehow the output PDF comes out inverted. The same behaviour is not seen when I convert the image to 'RGB' before saving it as PDF. The original image is in 'CMYK'. How can I avoid this?
Sample code:
from PIL import Image, ImageOps
image = Image.open('door.jpg')
image.save(
'output.pdf',
resolution=180.0,
quality=100
)
Input and output images:
If the image is known to be CMYK, try converting it to RGB before saving.
cmyk = Image.open('door.jpg')
rgb = cmyk.convert("RGB")
rgb.save(...)
This was caused by a limitation in Pillow Python and will be fixed in the latest release (v7.2.^) - https://github.com/python-pillow/Pillow/issues/4860
I've tried to convert a SVG file to PNG with antialiasing off in Magick++ but I wasn't successful. But I was able to convert the SVG file to PDF with another program and the use the ImageMagick convert command to convert the PDF file to PNG.
How can I use ImageMagick to do it? The command I use for converting PDF to PNG is this:
convert +antialias -interpolate Nearest -filter point -resize 1000x1000 "img.pdf" PNG24:"filter.png"
Is there any way to use Magick++ to do that or better, convert SVG to PNG directly with antialiasing off?
Thanks in advance.
Edit:
The answer given in this post doesn't work for me. Possible because I'm using a colored SVG instead of 1-bit alpha channel. Also I mentioned in my question that I'm also looking for a way to do this in Magick++.
Magick++ has the Magick::Image::textAntiAlias & Magick::Image::strokeAntiAlias methods available, but they would only be useful if your parsing the SVG and rebuilding the image (i.e. roll-your-own SVG engine) one SVG element at a time.
As #ccprog pointed out in the comments; once the decoder utility rasters the vectors, the damage is done & setting the flags would not have an effect on the resulting resize.
Without seeing the SVG, I can only speculate what the problem is. I would suggest setting the document size before reading the SVG content.
For example, read the image at a smaller size than resample up.
Magick::Image img;
img.size(Magick::Geometry(100, 100)); // Decode to a small context
img.read("input.svg");
img.interpolate(Magick::NearestInterpolatePixel);
img.filterType(Magick::PointFilter);
img.resize(Magick::Geometry(600, 600));
img.write("PNG24:output#100x100.png");
Or render at larger size then the finial image.
Magick::Image img;
img.size(Magick::Geometry(1000, 1000)); // Decode to a larger context
img.read("input.svg");
img.interpolate(Magick::NearestInterpolatePixel);
img.filterType(Magick::PointFilter);
img.resize(Magick::Geometry(600, 600));
img.write("PNG24:output#1000x1000.png");
Update from comments
For Postscript (PDF) & True-Type antialiasing, you would set Magick::Image::textAntiAlias (or Magick::Image::antiAlias if using IM6) to false. Just ensure that the density is set to allow any overhead.
Magick::Image img;
img.density(Magick::Point(300));
if (MagickLibVersion < 0x700) {
img.antiAlias(false);
} else {
img.textAntiAlias(false);
}
img.interpolate(Magick::NearestInterpolatePixel);
img.filterType(Magick::PointFilter);
img.read("input.pdf");
img.resize(Magick::Geometry(1000, 1000));
img.write("PNG24:output.png");
I'm currently looking for a way to generate the thumbnail image for a given pdf file, which shows several pages in the same image. The output should like what shows in the arxiv sanity website. I want to know if there is any npm package which supports this functionality. Thanks.
In ImageMagick command line, you can do that as follows. Suppose you want 8 pages from the PDF.
Input PDF from http://www.arxiv-sanity.com:
convert image.pdf[0-7] -thumbnail 140x140 -background white +smush 20 -bordercolor white -border 10 result.jpg
This takes the first 8 pages, makes thumbnails of size 140x140 and appends them side-by-side with a 20 pixels white spacing between them and adds a 10 pixel white border around it all.
Sorry, I do not know Node.js. But apparently there is a module that integrates ImageMagick. See https://github.com/yourdeveloper/node-imagemagick
var PDFImage = require("pdf-image").PDFImage; //pdf to image convert
var pdfImage = new PDFImage("1120.pdf");
pdfImage.convertPage(0).then(function (imagePath) {
},(err)=>{
console.log("err",err)
})
//##jimp Npm use thumbnail image generate
//if auth error Follow this step :
-> In /etc/ImageMagick-6/policy.xml (or /etc/ImageMagick/policy.xml) find the following line
->
and change it to allow reading and writing by the PDF coder in ImageMagick:
How to define the input image format for imagemagick convert, if convert -identify can't identify the input format by content, but I know it format well, and I can define it directly?
For example, I want to convert the svg file to png, but I have an example of valid svg which can't be identified by content.
Step to reproduce
Take this valid https://commons.wikimedia.org/wiki/File:Svg.svg
Rename file Svg.svg to image
Try to convert image image.png
Got the
convert.exe: NoDecodeDelegateForThisImageFormat ' # error/constitute.cReadImage/501.
ImageMagick version is 6.9.2-0 Q16 x86 2015-08-15
Like this - you need to tell ImageMagick what to expect with the svg: prefix as it can't work out what is coming from the extension since there isn't one:
curl https://upload.wikimedia.org/wikipedia/commons/1/15/Svg.svg > image
convert svg:image image.png
OpenCV provided function to convert Bayer to RGB, but how to use this CV_BayerBG2BGR , and other similar function?
I used code below, but the error appears stated invalid channel number. Since I use RGB image as originalImage, anyway how this function actually works?
void main(){
// Declare and load the image
// Assume we have sample image *.png
IplImage *originalImage = cvLoadImage("bayer-image.jpg",-1);
// The image size is said to be 320X240
IplImage *bayer2RGBImage;
bayer2RGBImage = cvCreateImage(cvSize(100,100),8,3);
cvCvtColor(originalImage,bayer2RGBImage,CV_BayerBG2BGR);
//Save Convertion Image to file.
cvSaveImage("test-result.jpg",bayer2RGBImage);
//Release the memory for the images that were created.
cvReleaseImage(&originalImage);
cvReleaseImage(&bayer2RGBImage);}
Furthermore, I'd like to convert common RGB image to bayer format (let say bilinear) too, whether openCV provide this function as well?
any help would be really appreciated.
Thanks in advance.
Unfortunately OpenCV does not provide BGR to Bayer conversion. Only backward conversion is available.
If you need a conversion to Bayer format then you should implement this conversion yourself or use another library.