Is there a way to load a image, rotate it and load it to MXNet model (e.g: yolov3).
I use the below method but I don't think it's efficient:
1/ Load the image and rotate it with pillow
image = Image.open(img_path)
image = image.rotate(90)
2/ Save the image and then load it with gluoncv (I use yolo here so I used yolo.load_test):
#Save image
image.save(name +".png")
#Load image with gluoncv
imgs_for_inference, imgs_for_plot = gluoncv.data.transforms.presets.yolo.load_test(
images_names,
short=512)
After trying some libraries, I found a more simple method for the problem without the need to save image and then load it (which is very inefficent). I used pillow to load the image and rotate it, then use transform_test to get MXNet tensor so as to feed to the model:
from PIL import Image
image = Image.open(path_to_image)
image = image.rotate(90)
tensor, _ = gluoncv.data.transforms.presets.yolo.transform_test(
images,
short=512
)
prediction = model(tensor)
Related
Given a set of N images, where each image can be described as
image = baseImage + uniqueOverlay
and the "uniqueOverlay" was added onto the "baseImage" using an unknown alpha value, which is constant for all images, how can I extract the unknown "baseImage"?
I have a feeling an algorithm for this already exists, but I am not able to find it as I am not very experience in image processing.
I've got the following image:
And I'm doing the following
First I read an image img = skimage.io.imread('original.jpg') that has the following histogram:
Then, after applying he_img = skimage.exposure.equalize_hist(a), I get the following histogram:
but when I save, then load and see the histogram of that image I get the following:
skimage.io.imsave(fname = 'he.jpg', arr= he_img)
saved = skimage.io.imread('he.jpg')
What else do I need to add to my process in order to being able to save the equalized image?
The problem with this was that I was using JPG. JPG uses lossy compression to save images, adding noise to the image affecting to the equalization and increassing the images entropy.
To solve this I tried PNG that is a lossless compression format for storing images. The code for this is:
img = skimage.io.imread('original.jpg')
hist_equalized_img = skimage.exposure.equalize_hist(img)
skimage.io.imsave(fname = 'he.png', arr= hist_equalized_img)
I have an image of 2061x2533 which represent an area of 429x349mm. When I open this saved image in Adobe Illustrator I would like the size to be exactly 429x349mm.
So I have a dpi value associated to my opencv image, but when I use cv.imwrite I did not find a way to set the dpi parameter to be considered by Photoshop or Illustrator.
Any idea?
As Micka said in his comment, opencv is for computer vision not for image editing. So you need another library to handle this.
You could for instance use pillow:
from PIL import Image
dpi = 150
cim = cv.imread(str(filename))
...
im = Image.fromarray(cim)
im.save('foobar.jpg', dpi=(dpi, dpi))
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..
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")