Python Magickwand pdf to image converting and resize the image - imagemagick

I need to create thumbnails of pdf files, and I am using Imagemagick to achieve that.
I have tried Pythonmagick and wand to convert the pdf to an image. However, when I try to resize the converted pdf, the resulting image becomes black.
Is there any option to set -define pdf:use-cropbox=true using python wrappers?
Is there any other method in Python to convert a pdf to a thumbnail?
The code is as follows:
import wand
img = wand.image.Image(filename="d:\\test.pdf[0]")
img.resize(160,160)
img.save(filename="d:\\test.jpg")

I found work around for this problem.
Convert pdf into image 1st and save the image. open newly saved image and and resize it.
import wand
img = wand.image.Image(filename="d:\\test.pdf[0]")
img.save(filename="d:\\temp.jpg")
img = wand.image.Image(filename="d:\\temp.jpg")
img.resize(160,160)
img.save(filename="d:\\resized_image.jpg")
I am still waiting for better answer.

I faced the same problem today. I found another solution in other post.
The reason why the image turns to black is that the background is transparent in PDF file. Since JPG file cannot recognize the alpha-channel (which record the transparent pixel information). JPG files will set those transparent pixel into black as default.
# Use the following two lines to fix this error
# img_page.background_color = Color('white')
# img_page.alpha_channel = 'remove'
with Image(filename="file.pdf",resolution= 350) as img_pdf:
num_pages = len(img_pdf.sequence)
for page, img in enumerate(img_pdf.sequence):
img_page = Image(image=img)
img_page.background_color = Color('white')
img_page.alpha_channel = 'remove'
img_page.resize(900,1200)
img_page.save(filename="file"+str(page)+".jpg")
if page == 0:
img_page.resize(500, 500)
img_page.save(filename="thumbnail.jpg")
ref: Python Wand convert PDF to PNG disable transparent (alpha_channel)

You can do it without using temporary files - if you don't depend on JPG.
The following works for me:
import wand
img=wand.image.Image(filename="/home/vagrant/tmp/wand/law.pdf[0]")
img.format='png'
img.resize(220,220)
img.save(filename="/home/vagrant/tmp/wand/law_2.png")

Related

prevent black and white image auto convert to grayscale image

Start from ImageMagick 6.8, it will auto convert a black and white image(RGB) to be grayscale, which will make the altered image to be lighter than the original one. In ImageMagick 6.9, we can turn this feature off by using the command "-set colorspace:auto-grayscale=false". However, this option is not available in Wand latest version, is it a way I can turn off this feature by using Wand?
In addition, the command "-type truecolor" can also prevent Imagemagick convert the image to be grayscale. I try to set the image type to be "truecolor" by using "img.type = 'truecolor'" but the image is still converted to be grayscale, which is different behavior than the ImageMagick. Just want to know that is there something I did wrong or is there a way I can use to prevent black and white image to be converted into grayscale by using Wand?
In case anyone has the same issue.
You can use turn off auto-grayscale in wand.
with Image(filename='input.jpg') as img:
img.metadata['colorspace:auto-grayscale'] = 'false'

opencv is changing color of image after imread and imwrite

import cv2;
input_path = 'input.JPG';
output_path = 'output.jpg';
input_image = cv2.imread(input_path,cv2.IMREAD_UNCHANGED)
cv2.imwrite(output_path,input_image)
Check the input output image comparision here
This is the original image - https://imgur.com/a/iRAS9NY
There is a color change between input and output image. Please help me modify the code so the input and output images are exactly matching. Thanks!
The image is in AdobeRGB format. That is the issue. We are using GIMP to convert into SRGB and then using it in our sfotware. It is working fine now.

cv2.imshow only show partial image with big images

Image is too large for uploading, size is 2160*2880.
here is the code:
cv.imshow only show partial content of this image. but if I save image with cv.imwrite and open the saved image with an application. image have all the content.
Is this a bug or cv2 or I miss something?
import cv2
cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
img = cv2.imread("0.png")
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.imwrite("xxx.png", img)
cv2.WINDOW_AUTOSIZE fix the window dimentions , so the image will fill the screen if too big
Try cv2.WINDOW_NORMAL which gives you the ability to change the window size.
after the save you maybe only use imread without namedWindow! (there isn't any other clear explanation)
Suggestion: You can use matplotlib for the show job, it has many parameters to use..

Resize blob image without creating tmp file

I have a scenario where i need to resize thousands of images. I am using MiniMagick to do this.
image = MiniMagick::Image.read(<blob>)
image.resize "100x100"
Image.create(:img => image.to_blob)
But the above code takes too long to process large number of images since it creates a tmp image file for each image it processes.
Is there a way to resize the image without creating the tmp file? I am also open to suggestions on other libraries that can speed up the processing.
Try using convert command available from imagemagic directly on the image as :
`convert source.jpg -resize 120x120 thumbnail.jpg`
Hope this helps you :)

How to merge a jpg with a gif without lossing the animation of gif file?

I am trying to merge a splash screen image which is in jpg format with an ajaxloader which is in gif format.
I tried the following command using ImageMagik:
convert -delay 50 splashimage.jpg ajaxloader.gif final.gif
This command does merge the gif file with the jpg image but the loader appear at the top-left corner.
I was wondering if there is an option available to place it at a specfic (x,y) point on the jpg image?
If anybody has had done a similar thing or has an idea as to how a similar thing can be achieved, please guide me through.
Thanks.
Why would you want to do this? You'd be turning a nice 24bit JPG into a crappy quality single frame on an 8bit gif. You could just use some CSS tricks to overlay the loader .gif on top of the .jpg on a webpage, which would not sacrifice any image quality to achieve the same effect.
I solved the problem by designing individual frames of images with the desired location of my text and merged them using Imagemagik.

Resources