Reuse inline image in IPython notebook - image

Is it possible to create an inline png image, e.g. using matplotlib, and re-using it in a markdown cell (via html) without saving it on hard disk first?
IPython notebook saves inline images in the ipynb file, so the data is available, I wonder if it is also accessible?
One idea is to generate images for pretty slides (cell mode) and to to suppress the slides for image creation.

No it is not possible without saving the image. Using slide mode you can exclude some cells. is is also possible to use slide-mode and not to show the code if you are using custom templates.

Related

Merge Image with Jupyter Notebook To Be One File

When we load an image in jupyter notebook , in actual we have two files. Is there a way to merge the both files in single note book file ?
Thank you .
In code cells
When you use code in code cell like below to show an image from an image file inside a notebook, it will actually get encoded within the notebook in the form of base64:
#use code like below to show images as output line because encodes as Base64 code saved within the notebook file
from IPython.display import Image
Image("<myimage_file>.png")
The easiest way to see that is to run such a cell, save the notebook, and inspect the underlying text of the notebook within a text editor. (This is possible right in Jupyter.)
For the corresponding code, you should now see something like this:
"cell_type": "code",
"execution_count": 1,
"id": "72bb5e3d-85c4-4c82-bb18-b466e06b98e7",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUg ...
The ... represents a long string with the image data encoded in base64. Importantly, if you now take that saved notebook and open a new Jupyter session somewhere else where you don't have that image file and you open up the Jupyter notebook, you'll see the image stays visible even without the image file, as long as you don't try to run the code in the cell. If you don't happen to have another computer with Jupyter handy, you can test this by going here and clicking launch binder to spin up a temporary notebook session accessible in your browser from a remote system served by MyBinder.org then dragging and dropping your saved notebook file in the file browser pane on the left side. Double-click to open the notebook and you should see your notebook open and display the image even though the remote system doesn't have your file. If you run the cell though, it isn't going to like the absence of the file, and so the best practice in data management with Jupyter notebook work is to place the notebook(s) and associated files a project folder and transport the entire directory.
There are ways to go back from base64 to the file, so as long as you have the notebook with the encoding you can regenerate the image, see here which is based on this stackoverflow post to convert the string text to bytes. However, I found that replacing the str.encode(string) part of my suggested code with string.encode(), and according to here that may perhaps be better.
In Markdown cells
Unfortunately, last I knew Markdown cells don't do this behavior and so you have to do additional steps to get an image encoded into the markdown cell of a notebook. It can be done though as discussed at the bottom of this post entitled 'Embedding image in ipython notebook for distribution'. (IPython notebooks are the previous version of what evolved into Jupyter notebooks by adding support for many kernel languages.)
I stored a notebook demonstrating using the outcome of following that process to embed images into markdown code within a notebook here, with some links to what I used to put it together. Looking at the text underlying that notebook in your text editor, you'll see the image code is embedded and by opening it elsewhere, you'll conform that it is portable.
Process to generate such base64 encoded image code I adapted from this answer here to 'embedding image into jupyter notebook and exporting to HTML':
%pip install pillow
image_to_convert = "<your_img_file_name_here>.png"
import base64, io, IPython
from PIL import Image as PILImage
image = PILImage.open(image_to_convert )
output = io.BytesIO()
image.save(output, format='PNG')
encoded_string = base64.b64encode(output.getvalue()).decode()
html = '<img src="data:image/png;base64,{}"/>'.format(encoded_string)
print("COPY THE TEXT BELOW INTO A MARKDOWN CELL:")
print(html)
In relation to placing the image encoded as Base64 into the img tag, I found this very helpful.
Someone posted code here to take base64-encoded images from notebooks and embed them in markdown; however, I haven't tried it yet. I also have tried the early part of this answer under 'How to embed image or picture in jupyter notebook, either from a local machine or from a web resource?' which is another take on this; however, I did test that resulting code there make a smiley when in a markdown cell in a notebook. (In fact you can take the part between parentheses that begins data and paste into the URL bar of your browser and see an image too.)

Images turning green-blackish when passing them to dataloaders

I'm working with fastai, trying to pass some images to a dataloader. The original images are kind of pinkish, but after passing them they appear mostly as green-black (see image in link below):
Original pinkish image (up) and example images (down) after passing them to dataloader, and the code.
The code I've used for the datablock and to show the images is:
example = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
splitter=GrandparentSplitter(),
get_y=parent_label,
item_tfms=Resize(128)) #already tried it without item_tfms just in case, still black-green
dls = example.dataloaders(path)
dls.show_batch(nrows=1, ncols=3)
I tried with .tif and .jpeg images, and both show the same problem. The only thing that comes to my mind is that somewhat somewhere is not reading correctly the color format (RGB according to my original files), or maybe transforming it; but I'm not able to figure it out.
Just in case it's important, I'm working in a Jupyter notebook with a MackBook Air M1.
Thanks!
Irene

Drupal resize image on the fly

Is it possible to resize images on the fly and cache the result with Drupal?
I have some big images (e.g. 2000x2000px) and I want to display a preview of the e.g. 100x100px.
I know there is a theme_image_style function. But it seams to only create the <img> with the right size and not effectively resize the image.
I look at modules/images/image.admin.inc and they used the function [image_style_create_derivative][2].
Yes, you should use Drupal's Image styles (Configuration -> Media -> Image styles). There you should create your style.
Then, on front-end, when ever you want to display image with that style (in that resolution) you can use image_style_url() function:
https://api.drupal.org/api/drupal/modules!image!image.module/function/image_style_url/7
It accepts 2 parameters - one is image style machine name and other is image URI, which you can get if you print out all image field properties.
You can also select image styles from back-end interface...i.e. when creating a view for some image you can select to be displayed in specific image style.
In both cases those image styles are generated the first time image is used.
In response to your comment on MilanG's answer, using image_style_url() is the best option on the backend. There is also
https://www.drupal.org/project/resp_img
which may be something worth looking into. From a UX perspective, you don't want to force the user to load a 2000x2000 px image every time they load the page. Regardless of the outputted size, the image is still going to render as a 2000x2000 px image with a large size. image_style_url() or using image styles in the GUI create a new file that will load much quicker and is the preferred method.

Any CLI tool to perform 3d texture mapping on the fly

I'm currently looking for a way to create a 'configurator' for a upholsters, similar to http://digitaldraping.com/configurator/furniture-sofa/?Cushions_Plain-Cream.png,Sofa_Stripe-Orange.png - you select your fabrics and they are 'drawn' on the sofa automatically.
Unfortunately, all the sites I've looked at seem to use pre-rendered transparent PNGs that are overlaid over each other to build up the full picture. The problem here is that we've figured out that we'd require over 120,000 different images to cover all models, fabrics etc!!
I've looked at a few 3d texture tools such as http://www.arahne.si/products/arah-drape.html, hoping that one of them would have a CLI option where you give it a pre-created wireframe, and a fabric to overlay, and it generates the required image on the fly, but so far everything seems to require real-time use of the GUI to use it.
So, is there a CLI tool that would do what I'm after, or can anyone suggest a way to manipulate the GUI automatically? (from a tech point of view, I'm comfortable with C, Bash, Python or PHP as a solution!)
Thanks!
ArahDrape 2.2 can now work from a command line without any GUI interface. You can also call ArahDrape as a C library. In this way, it can be used in a web server to create texture mapped images on the fly. The command line options are explained below.
ArahDrape 2.2j command line version, ©2015 Arahne
usage:
adCommand -o /tmp/outputImage.png -tN /home/user/texture.png [-hidemodel] [-divide 2] [-filterPNG] [-compressPNG 2] [-m /home/user/model.png] -owner name -activation 174b3cfb49e9 /home/user/project.drape
Input and output images can have png, .tif or .jpg extensions
-o output_image_file
-tN texture_image_file [N goes from 0 to 199]
-hidemodel will render all areas not in region as white
-divide N [N goes from 2 to 5] divide resulting image pixel size
-filterPNG if you do not filter it, rendering is faster
-compressPNG N [N goes from 0 to 9] lower number saves faster, but bigger files
-m model_image_file use this if you want to replace model image from the project; must have same pixel size
-owner owner_name pass the given owner name
-activation activation_code pass the given activation code
last parameter should be ArahDrape project file
All files should be entered with full path.
If you need spaces in filenames, use quotes "" around the filename.
If you provide only Owner name, without activation code, program returns registration code.
ArahDrape supports batch export.
Open ArahDrape project, click on texture you wish to replace, put all your texture in a directory, select from menu
Textures > Browse textures, and as you click the texture to load it, program will save the draped picture. If you have thousands of images, use keyboard shortcut = and program will automatically do them all.
Alpha channel transparency is supported in loading model images or textures, and saving the draped images, as long as you use PNG or TIFF.
Please check this video to see how
ArahDrape works in batch mode.
we (http://digitaldraping.com/) can do just what you are asking. We have two options creating images and rendering a meshed image on the fly. Just get in touch if you still need this solution.

Extract Images and Words with coordinates and sizes from PDF

I've read much about PDF extractions and libraries (as iText) but i just haven't found a solution to extract images and text (with coordinates) from a PDF.
The task is to scan PDF with catalog of products and extract each image. There is an image code printed next to each image and also a list of product codes for products that are shown on the image.
I know that there is no way to extract structured info from a PDF like this but with coordinates of all image and text objects I could write code to identify linked text by its distance from the image. Then I could split text using a RegExp and find out what is a product code, what is an image code etc.
Could you recommend a good and working solution for the task?
Use XPDF (http://www.foolabs.com/xpdf/)
It can extract all the characters in the PDF with co-ordinates (pdftotext -bbox [sourcefile] [outputfile]) and also all the images and SVGs in the PDF.
It's open source (GPLv2) and supports a lot of additional extraction functionalities as well.
Several Java libraries can do this. Have you looked at JPedal or PdfBox?
If a commercial library is an option for you, you could try Amyuni PDF Creator .Net or Amyuni PDF Creator ActiveX. You could use the method IacDocument.GetObjectsInRectangle to retrieve all the "graphic objects" of your interest, then use the ObjectType attribute to separate images from text. The library already provides an algorithm for putting close text together. From the documentation:
IacDocument.GetObjectsInRectangle Method
The GetObjectsInRectangle method gets all the objects that are in the specified rectangle.
Usual disclaimer applies.

Resources