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.
Related
I have to add an image to a database, so I open the image as binary and it stores it this way:
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x01\x03\x00\x00\x00%\xdbV\xca\x00\x00\x00\x03PLTE\x00\x00\x00\xa7z=\xda\x00\x00\x00\x01tRNS\x00#\xe6\xd8f\x00\x00\x00\nIDAT\x08\xd7c`\x00\x00\x00\x02\x00\x01\xe2!\xbc3\x00\x00\x00\x00IEND\xaeB`\x82'
However I need it to be strored this way:
0x89504e470d0a1a0a0000000d494844520000000100000001010300000025db56ca00000003504c5445000000a77a3dda0000000174524e530040e6d8660000000a4944415408d76360000000020001e221bc330000000049454e44ae426082
It is my first ever time working with binary files so there is probably something basic I'm not understanding.
This is my code for opening the image in python:
with open("1x1.jpg", 'rb') as File:
binaryData=File.read()
print(binaryData)
This is the image: (1x1 empty pixel, I changed the extension from png to jpg, the original image is from https://upload.wikimedia.org/wikipedia/commons/c/ca/1x1.png)
binaryData is bytes and you need to convert it to the hex format.
binaryData.hex()
returns
'89504e470d0a1a0a0000000d494844520000000100000001010300000025db56ca00000003504c5445000000a77a3dda0000000174524e530040e6d8660000000a4944415408d76360000000020001e221bc330000000049454e44ae426082'
How can I open an image in pillow that I already opened using open('image','r')
I have an image that I opened using the open() function, but i want to use the image in pillow.
Actually, I encoded it using base64, then the program decodes it,then gives you a variable that is in the same format as the open() function has. Then I just want to show the image, if there is another way to show the image without saving it, please let me know.
Here is the code that I use to decode it, just so you know:
import base64
image_64_encode = 'this-string-is-big-so'
image_64_decode = base64.decodebytes(image_64_encode)
I just want to show the image.
Like this:
from base64 import b64decode
from PIL import Image
import io
# Load useful-looking base64 string of a PNG
b64 = 'iVBORw0KGgoAAAANSUhEUgAAAEAAAABAEAIAAAB1mzrKAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0T///////8JWPfcAAAAB3RJTUUH5QsDDgY2fEahewAAANhJREFUeNrt3LENg0AQRcFFOue4/yIhN9K5jAl4UwHS02flAB977z1h1sxzr1M/xnsVAFszz7W++jHeqwVgBcAKgHUDsBaAFQArANYNwFoAVgCsAFg3AFszv/tz6sd4r15BWAGwbgDWArACYAXACoB1hLEWgBUAKwDWDcBaAFYArADYmvldn24A0wKwAmAFwPodgLUArABYAbBuANYCsAJgBcAKgHWEsRaAFQDr+wCsG4D1CsIKgBUA6wZgLQArAFYArBuAtQCsAFgBsG4A1gKwY++Z/sDe+QP/bITzNFwkpgAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAyMS0xMS0wM1QxNDowNjo1NCswMDowMGz+3A4AAAAldEVYdGRhdGU6bW9kaWZ5ADIwMjEtMTEtMDNUMTQ6MDY6NTQrMDA6MDAdo2SyAAAAAElFTkSuQmCC'
# Open it with PIL - no disk access required
im = Image.open(io.BytesIO(b64decode(b64)))
print(im)
# prints: <PIL.PngImagePlugin.PngImageFile image mode=RGB size=64x64 at 0x7FF3B90251C0>
The clue is here in the Pillow documentation where it says:
fp – A filename (string), pathlib.Path object or a file object.
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
When i open an image in image viewer the displayed image name is wrong (not the same as loaded image). Orginal image = 'image.PNG', name in image viewer='tmpy4uvijg0.BMP' (the new name always changeds, see in image below)
from PIL import Image
imName='image.PNG'
try:
with Image.open(imName) as im:
print(imName)
im.show()
except IOError:
pass
image.png
new image
What do i wrong? Why is the name not the same?
It is because the show method save the image to a temporary file, as say in the documentation:
Displays this image. This method is mainly intended for
debugging purposes.
On Unix platforms, this method saves the image to a temporary
PPM file, and calls the xv utility.
On Windows, it saves the image to a temporary BMP file, and uses
the standard BMP display utility to show it (usually Paint).
:param title: Optional title to use for the image window,
where possible.
:param command: command used to show the image
You can try to change the title by passing a string in parameter to show.
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...