convert base64 encoded png pic using PIL - python-imaging-library

all. Recently I am using PIL to complete some code. As a default demo shows me how to convert a jpg picture file like this:
img = Image.open(filePath).convert('RGB')
Then I also try to convert a base64 encoded picture to finish the same thing. The only difference is the picture is obtained from url which is base64 encoded.
codec = 'base64 encoded picture'
base64_data = re.sub('^data:image/.+;base64,', '', codec)
byte_data = base64.b64decode(base64_data)
image_data = BytesIO(byte_data)
img = Image.open(image_data)
img = img.convert('RGB')
which shows "broken stream whille reading the picture" at the and of the last line. Is there any thing I misunderstand??

For some debugging work, I find that python2 and python3 use different lib(IOString and ByteIO). Just make a mismatch and correct it will make this code work.

Related

Convert base64 String into PDF file in Flutter

I am trying to show a PDF file. But PDF file I am receiving from server in Base64 String format. Is there any way I can directly show Base64 String into PDF viewer or WebView without saving it into File.
Check this : https://stackoverflow.com/a/55599926/305135
(Following code is copied from link above)
This should convert base64 encoded pdf data into a byte array.
import 'packages:dart/convert.dart';
List<int> pdfDataBytes = base64.decode(pdfBase64)
.map((number) => int.parse(number));
The pdf and the image plugins seems to suit your needs for displaying pdf.
The code should be roughly like so:
import 'package:pdf/pdf.dart';
import 'package:image/image.dart';
...
Image img = decodeImage(pdfDataBytes);
PdfImage image = PdfImage(
pdf,
image: img.data.buffer.asUint8List(),
width: img.width,
height: img.height);
// Display it somehow
...
At first i was doing the same thing like you. But i didnt file any appropriate solution to convert the base64 String into a pdf file.
I think you can get the BufferArray and then convert it into a pdf file.
I have answered how to parse blob data to pdf in this question :
How to convert ByteBuffer to pdf

Is it possible to set the PNG physical pixel dimensions using FabricJS?

The PNG specification includes physical pixel dimensions (see here). Does Fabric.js support setting these values? If so, how can these be set?
Edit: I should mention that I am trying to do this entirely in the browser. I'm not using NodeJS.
fabricJS does not support DPI values on output, but lucky enough i wrote a small library that will let you do that!
It works on blob and dataurl:
https://www.npmjs.com/package/changedpi
I know seems pure advertising but i think this piece of code will do that and it does just that, it works for both PNG and JPEG.
// create the dataUrl at standard 72dpi
var dataUrl = canvas.toDataURL('image/jpeg', 0.92);
var daurl150dpi = changeDpiDataUrl(dataUrl, 150);
or
// create the blob at standard 72dpi
canvas.toBlob(function(blob) {
changeDpiBlob(blob, 300).then(function(blob){
// use your changed blob
})
},'image/jpeg', 0.92);
The method it uses here are not the one from fabricJS, for the dataUrl there is no problem since you can generate a dataurl and pass it to this function here, while for the blob you have to use
fabricCanvas.toCanvasElement(...) then move from canvasElement to blob if for any reason you prefer the blob advantages over dataUrls.

Handling multiple encodings in a single file

I'm encountering some weird encoding issues. I need to parse an HTML document from the web, and I'm using the 'Content-Type' charset meta-data to determine the encoding type.
One page has been giving me trouble and is encoded by 'Shift_jis' (Japanese) - The parser result contains some garbled characters.
When I parse the same document using UTF-8 the characters that were garbled before are parsed correctly but everything else is now garbled.
I'm assuming the document contains text in two different encoding types.
I there anyway I could parse this document correctly ?
Also, I don't how, but all the browsers seem to deal well with the issue and are presenting the page nicely.
Would really appreciate any thoughts on this.
The page that I need to parse : http://ao.recruit.co.jp/form.html
First of all, what the browser sees is:
莨夂、セ讎りヲ
What is shown in rendered html is not the same because of the CSS text-indent: -9999px and the background image laid over it. But it's there. Removing them will show the text browser is seeing.
Out of the box, decoding as Shift-Jis should give you 莨夂、セ讎りヲ?, but if you want same results as in a browser, you should use a custom CharsetDecoder with IGNORE:
URL url = new URL( "http://ao.recruit.co.jp/form.html");
BufferedInputStream bis = new BufferedInputStream(url.openStream());
CharsetDecoder decoder = Charset.forName("Shift-Jis").newDecoder();
decoder.onMalformedInput(CodingErrorAction.IGNORE);
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
Reader inputReader = new InputStreamReader(bis, decoder);
String result = IOUtils.toString(inputReader);
System.out.print(result);
This will give you same result as with browsers. Of course, it won't parse the text from the image file.

PDF file stored as BLOB, view in a webpage perl

I have a code that handles displaying a blob from a local Oracle database. I store both JPG and PDF files as blob. I could view the JPG file, but not the PDF. I have checked these
$self->content_type('image/jpg')
to
$self->content_type('application/pdf').
And the Blob does have data. I checked the length and it has "184546".
All I get when I click the link for the pdf file is a blank page with the title GETIMAGPAGE(application/pdf).
Any help or pointers would be greatly appreciated.
Also, How can we have the content_type to enable two different mime_types? For example in my case both image as well as pdf, depending on what we get?
File::MMagic can recognize the type of data using magic numbers.
use File::MMagic;
$magic = File::MMagic->new;
$self->content($blob);
$self->content_type($magic->checktype_contents($blob));
If you don't want to require a native/plugin PDF reader, perhaps FlexPaper might fit your needs.

Zend Mail - Pdf Attachment - imbedded png gets scrambled

I've got my Zend_Mail setup and working fine, but the png images in my pdf attachment get scrambled.
This is the bit responsible for the attachment:
$filecontents = file_get_contents($attachment);
$att = $this->mail->createAttachment($filecontents);
$att->disposition = 'Zend_Mime::DISPOSITION_INLINE';
$att->encoding = 'Zend_Mime::ENCODING_BASE64';
$att->filename = 'result.pdf';
$att->type = 'application/pdf';
The Pdf is created with Zend_Pdf
The PNG is added like this:
$image = Zend_Pdf_Image::imageWithPath("path/to/png.png");
$this->page->drawImage($image, $left, $bottom, $right, $top);
Any ideas?
Thanks in advance!
Peter
Do you actually want the PDF to be inline in the email? I don't think email clients even support that. Try Zend_Mime::DISPOSITION_ATTACHMENT.
Also, the constants should not be in quotes. It's just $at->disposition = Zend_Mime::DISPOSITION_ATTACHEMENT.
Why do you set inline disposition for a PDF attachment? The mail client can't display it inline anyway (unlike the GIF in the Zend example).
The PNG format contains a \r\n in its signature, so I'd check for corruption there.. possibly triggered by the disposition:INLINE setting, but perhaps you've enabled some stream wrappers you're not telling us about?