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...
Related
I am trying to convert an imgkit image into a PIL image to modify it. imgkit successfully converted the html to image when I tried to use a file. When I use BytesIO and try to convert to a PIL image, im getting an error.
Here is my code:
img = imgkit.from_string(template.render(a=elements, r=range(len(elements))), False, config=config)
bytesImg = BytesIO(img)
bytesImg.seek(0)
image = Image.open(bytesImg) #error here
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x102082680>
I already saw this and this.
Am I incorrectly converting the imgkit image to bytes or is there some other error?
Using Pillow 8.1 Python 3.9 and imgkit 1.0.2
Am I incorrectly converting the imgkit image to bytes or is there some other error?
I would start from checking if your bytes represents image understand by your Pillow. Built-in module imghdr should suffice if you are excepting one of format known by it (see table in docs). Usage in this case:
import imghdr
...
print(imghdr.what(None, h=img))
If it does identify format then check if it is supported by your Pillow, else you would need to manually check file signature (few starting bytes).
imgkit was converting the html to pdf because the config variable was messed up.
use
which wkhtmltoimage
to find path to wkhtmltoimage and set
config = imgkit.config(wkhtmltoimage="path found")
I am trying to save a image using tiles and image_slicer. The default format is PNG, but I need to save as JPG and whent I try that I receive the error below.
KeyError: 'JPG'
I followed the documentation
This is my code:
tiles = image_slicer.slice(path+image_file_name, 4, save=False)
image_slicer.save_tiles(tiles, prefix='slice_'+names_images, format='JPG', directory='../images/mar2020/selected/resized/')
Looks like the documentation is wrong. Try to use jpeg instead of jpg (case insensitive).
source
I want to convert .raw file to .jpg or .png using some plain python code or any module that is supported in python 2.7 in windows environment.
I tried rawpy, PIL modules.
But I am getting some attribute error(frombytes not found); because it is supported in Python3. Let me know if i am wrong..
In rawpy the RAW format is not supported.
I need some module or some code that will change .raw to either png or jpeg.
use PIL library
from PIL import Image
im = Image.open("img.raw")
rgb_im = im.convert('RGB')
rgb_im.save('img.jpg')
You can use the convert utility which comes with image magic.
convert -size 640x360 -depth 8 bgr:input.raw out.jpeg
-size -> WxH
bgr is the format of data. it can be rgb also.
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)
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.