Mspaint in commandline - python

I converted images (using imagemagick) from png to bmp.
Trying to analyze the bmp with Python Simplecv
img=Image('im.bmp')
returns an error from PIL:
.....IOError: Unsupported BMP header type (124)
When I convert the image with mspaint, no error is returned.
Consequently, as I have a lot of images to convert, does anybody knows how to save an image with mspaint.
I can open paint but how to save the image ?
Thanks a lot in advance for any clue
Dom

Just working on something similar, below code has worked for me but I'm not sure how to close paint now I've done:
import os
import ImageGrab
SaveDirectory=r'C:\Users\????\Desktop'
sName = "Name of File"
ImageEditorPath=r'C:\WINDOWS\system32\mspaint.exe'
img=ImageGrab.grab()
saveas=os.path.join(SaveDirectory,sName + '.bmp')
img.save(saveas)
editorstring='""%s" "%s"'% (ImageEditorPath,saveas)
os.system(editorstring)

Related

Can not open .tif image

I want to open a .tif image but I always get error for every library I tried to use.
I tried with PIL:
from PIL import Image
img = Image.open('filepath/img_name.tif')
but I get the following error:
UnidentifiedImageError: cannot identify image file 'filepath/img_name.tif'
(This error does not mean that I can not find the file so the directory should be good)
I tried with tifffile:
import tifffile
img = tifffile.imread('filepath/img_name.tif')
I got the following error:
NotImplementedError: unpacking 14-bit integers to uint16 not supported.
I am pretty sure the problem is that the picture because I tried to open a tif image on the internet and it work just by doing this: this is the picture
from PIL import Image
im = Image.open('a_image.tif')
Is there a way to convert my 14-bit picture to a 16-bit picture?
(I know that I could multiply by 4 to get to 16-bit but I do not know how)
I installed imagedecodecs and tifffile has been able to open it
import tifffile
img = tifffile.imread(tif_name)
The problem was that my image was in 14bits.
Perhaps your TIF file has more than one frame. That could be a problem. Try:
from PIL import Image
image = Image.open("animation.tif")
image.seek(1) # skip to the second frame
try:
while 1:
image.seek(image.tell()+1)
# do something to im
except EOFError:
pass # end of sequence
From the documentation.

How can I convert a .ps file into a .png file?

I need to convert .ps files to .png files as part of an image recognition program I am making. I know I can use Ghostscript or other programs, but could someone give a specific example of how to write something like this:
def ps_to_png(ps_file):
file = ghostscript.read(ps_file)
png_file = ghostscript.save(file, "png")
return png_file
(This code is pseudo code- I want to know how to write something that actually does what this code looks like it will do.)
Thanks in advance! Stack is a great community and I appreciate it.
EDIT (Attempted solutions): When running this line:
os.system("ghostscript file.ps file.png")
I get the following Error:
'ghostscript' is not recognized as an internal or external command, operable program or batch file.
When attempting to use Pillow:
from PIL import Image
def convert_to_png(ps_file):
img = Image.open(ps_file)
img.save("img.png")
I get the following error:
OSError: Unable to locate Ghostscript on paths
You can use Pillow.
from PIL import Image
psimage=Image.open('myImage.ps')
psimage.save('myImage.png')
If you want to wrap it to a function:
from PIL import Image
def convert_to_png(path):
img = Image.open(path)
img.save("img.png")
path='/path_to_your_file'
convert_to_png(path)

Generate barcode image from PIL.EPSImageFile instance

I want to generate a barcode image. So, I used elaphe package. It works correctly but it returns PIL.EPSImageFile instance. I don't know how I can convert it to image format like SVG, PNG or JPG.
The code I have written is:
barcode('code128', 'barcodetest')
And it returns:
<PIL.EpsImagePlugin.EpsImageFile image mode=RGB size=145x72 at 0x9AA47AC>
How can I convert this instance to image?
Actually I think my question is wrong but I don't know how to explain it well!
Simply save that file object to something with a .png or .jpg filename:
bc = barcode('qrcode',
'Hello Barcode Writer In Pure PostScript.',
options=dict(version=9, eclevel='M'),
margin=10, data_mode='8bits')
bc.save('yourfile.jpg')
or state the format explicitly:
bc.save('yourfile.jpg', 'JPEG')
PIL will then convert the image to the correct format.
Note that the PIL EPS module uses the gs command from the Ghostscript project to do it's conversions, you'll need to have it installed for this to work.

Python: Cannot open converted bitmap image file, converted using PIL

How do I go about converting a JPG file to BMP using PIL? Tried to use Image.open('ifile.jpg').save('ofile.bmp', 'BMP') but I get a 'bogus header data' when attempting to open the file.
Tried the copy / paste method as described in Convert RGBA PNG to RGB with PIL but it does not work too.
there is no problem in the code.(i have tried the following code and worked well.) it converts your file to desired format.
import Image
im = Image.open("test.bmp")
im.save("test.jpg", "JPEG")
i think the problem is about Ubuntu. I also met a few times and I think it is a bug. try to open with gimp...

DICOM to TIFF inverted LUT ... Python & PIL

I am using the frombuffer command to save DICOM image data as TIFF images. But somehwere throughout this process, the image intensities are inverted (inverted LUT). Any idea on how to overcome this?
I have tried using the ImageOps.invert function from PIL, but if gives me "not supported for this image mode" error.
This is the code I'm using:
import dicom
import Image
import PIL.ImageOps
meta=dicom.read_file("DicomImage.dcm")
imHeight=meta.Rows
imWidth=meta.Columns
imSize=(imWidth,imHeight)
TT=Image.frombuffer("L",imSize,meta.PixelData,"raw","L",0,1)
TT.save("testOUTPUT.tiff","TIFF",compression="none")
Any guidance is appreciated ...
Python 2.7
PIL 1.1.7
Pydicom 0.9.6
Rather than "" for the raw mode, you should be using one of the mode strings from the documentation. Try "L" or "L;I", one or the other should be correct.

Categories