I cannot get the colorbar on imshow graphs like this one to be the same height as the graph, short of using Photoshop after the fact. How do I get the heights to match?
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
plt.figure()
ax = plt.gca()
im = ax.imshow(np.arange(100).reshape((10,10)))# create an axes on the right side of ax. The width of cax will be 5%# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(im, cax=cax)
You can do this easily with a matplotlib AxisDivider.
The example from the linked page also works without using subplots:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
plt.figure()
ax = plt.gca()
im = ax.imshow(np.arange(100).reshape((10,10)))
# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(im, cax=cax)
@bogatron already gave the answer suggested by the matplotlib docs, which produces the right height, but it introduces a different problem.
Now the width of the colorbar (as well as the space between colorbar and plot) changes with the width of the plot.
In other words, the aspect ratio of the colorbar is not fixed anymore.
To get both the right height and a given aspect ratio, you have to dig a bit deeper into the mysterious axes_grid1 module.
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size
import numpy as np
aspect = 20
pad_fraction = 0.5
ax = plt.gca()
im = ax.imshow(np.arange(200).reshape((20, 10)))
divider = make_axes_locatable(ax)
width = axes_size.AxesY(ax, aspect=1./aspect)
pad = axes_size.Fraction(pad_fraction, width)
cax = divider.append_axes("right", size=width, pad=pad)
plt.colorbar(im, cax=cax)
Note that this specifies the width of the colorbar w.r.t. the height of the plot (in contrast to the width of the figure, as it was before).
The spacing between colorbar and plot can now be specified as a fraction of the width of the colorbar, which is IMHO a much more meaningful number than a fraction of the figure width.
import matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()
ax = plt.axes()
im = ax.imshow(np.arange(100).reshape((10,10)))# Create an axes for colorbar. The position of the axes is calculated based on the position of ax.# You can change 0.01 to adjust the distance between the main image and the colorbar.# You can change 0.02 to adjust the width of the colorbar.# This practice is universal for both subplots and GeoAxes.
cax = fig.add_axes([ax.get_position().x1+0.01,ax.get_position().y0,0.02,ax.get_position().height])
plt.colorbar(im, cax=cax)# Similar to fig.colorbar(im, cax = cax)
I appreciate all the answers above. However, like some answers and comments pointed out, the axes_grid1 module cannot address GeoAxes, whereas adjusting fraction, pad, shrink, and other similar parameters cannot necessarily give the very precise order, which really bothers me. I believe that giving the colorbar its own axes might be a better solution to address all the issues that have been mentioned.
Code
import matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()
ax = plt.axes()
im = ax.imshow(np.arange(100).reshape((10,10)))
# Create an axes for colorbar. The position of the axes is calculated based on the position of ax.
# You can change 0.01 to adjust the distance between the main image and the colorbar.
# You can change 0.02 to adjust the width of the colorbar.
# This practice is universal for both subplots and GeoAxes.
cax = fig.add_axes([ax.get_position().x1+0.01,ax.get_position().y0,0.02,ax.get_position().height])
plt.colorbar(im, cax=cax) # Similar to fig.colorbar(im, cax = cax)
Result
Later on, I find matplotlib.pyplot.colorbar official documentation also gives ax option, which are existing axes that will provide room for the colorbar. Therefore, it is useful for multiple subplots, see following.
Therefore, the only universal way of dealing colorbar size with all types of axes is:
ax.colorbar(im, fraction=0.046, pad=0.04)
Work with fraction from 0.035 to 0.046 to get your best size. However, the values for the fraction and paddig will need to be adjusted to get the best fit for your plot and will differ depending if the orientation of the colorbar is in vertical position or horizontal.
Qt works very well with graphics. In my opinion it is more versatile than PIL.
You get all the features you want for graphics manipulation, but there’s also vector graphics and even support for real printers. And all of that in one uniform API, QPainter.
To use Qt you need a Python binding for it: PySide or PyQt4.
They both support Python 3.
Here is a simple example that loads a JPG image, draws an antialiased circle of radius 10 at coordinates (20, 20) with the color of the pixel that was at those coordinates and saves the modified image as a PNG file:
from PySide.QtCore import *
from PySide.QtGui import *
app = QCoreApplication([])
img = QImage('input.jpg')
g = QPainter(img)
g.setRenderHint(QPainter.Antialiasing)
g.setBrush(QColor(img.pixel(20, 20)))
g.drawEllipse(QPoint(20, 20), 10, 10)
g.end()
img.save('output.png')
But please note that this solution is quite ‘heavyweight’, because Qt is a large framework for making GUI applications.
As of March 30, 2012, I have tried and failed to get the sloonz fork on GitHub to open images. I got it to compile ok, but it didn’t actually work. I also tried building gohlke’s library, and it compiled also but failed to open any images. Someone mentioned PythonMagick above, but it only compiles on Windows. See PythonMagick on the wxPython wiki.
PIL was last updated in 2009, and while it’s website says they are working on a Python 3 port, it’s been 3 years, and the mailing list has gone cold.
To solve my Python 3 image manipulation problem, I am using subprocess.call() to execute ImageMagick shell commands. This method works.
Depending on what is needed, scikit-image may be the best choice, with manipulations going way beyond PIL and the current version of Pillow. Very well-maintained, at least as much as Pillow. Also, the underlying data structures are from Numpy and Scipy, which makes its code incredibly interoperable. Examples that pillow can’t handle:
You can see its power in the gallery. This paper provides a great intro to it. Good luck!
If you are on Python3 you can also use the library PILasOPENCV which works in Python 2 and 3. Function api calls are the same as in PIL or pillow but internally it works with OpenCV and numpy to load, save and manipulate images. Have a look at https://github.com/bunkahle/PILasOPENCV or install it with pip install PILasOPENCV. Not all PIL functions have been simulated but the most common functions work.
First parameter to .paste() is the image to paste. Second are coordinates, and the secret sauce is the third parameter. It indicates a mask that will be used to paste the image. If you pass a image with transparency, then the alpha channel is used as mask.
EDIT: Both images need to be of the type RGBA. So you need to call convert('RGBA') if they are paletted, etc.. If the background does not have an alpha channel, then you can use the regular paste method (which should be faster).
produces the following image (the alpha part of the overlayed red pixels is completely taken from the 2nd layer. The pixels are not blended correctly):
Compositing image using Image.alpha_composite like so:
Had a similar question and had difficulty finding an answer. The following function allows you to paste an image with a transparency parameter over another image at a specific offset.
# Assuming you named the file frame.py in the same directoryfrom frame importFrame
background =Frame()
overlay =Frame()
background.load_from_path("your path here")
overlay.load_from_path("your path here")
background.overlay_transparent(overlay.frame, x=300, y=200)
I ended up coding myself the suggestion of this comment made by the user @P.Melch and suggested by @Mithril on a project I’m working on.
I coded out of bounds safety as well, here’s the code for it. (I linked a specific commit because things can change in the future of this repository)
Note: I expect numpy arrays from the images like so np.array(Image.open(...)) as the inputs A and B from copy_from and this linked function overlay arguments.
The dependencies are the function right before it, the copy_from method, and numpy arrays as the PIL Image content for slicing.
Though the file is very class oriented, if you want to use that function overlay_transparent, be sure to rename the self.frame to your background image numpy array.
Or you can just copy the whole file (probably remove some imports and the Utils class) and interact with this Frame class like so:
# Assuming you named the file frame.py in the same directory
from frame import Frame
background = Frame()
overlay = Frame()
background.load_from_path("your path here")
overlay.load_from_path("your path here")
background.overlay_transparent(overlay.frame, x=300, y=200)
Then you have your background.frame as the overlayed and alpha composited array, you can get a PIL image from it with overlayed = Image.fromarray(background.frame) or something like:
Or just background.save("save path") as that takes directly from the alpha composited internal self.frame variable.
You can read the file and find some other nice functions with this implementation I coded like the methods get_rgb_frame_array, resize_by_ratio, resize_to_resolution, rotate, gaussian_blur, transparency, vignetting :)
You’d probably want to remove the resolve_pending method as that is specific for that project.
Glad if I helped you, be sure to check out the repo of the project I’m talking about, this question and thread helped me a lot on the development :)
Alright, I’m toying around with converting a PIL image object back and forth to a numpy array so I can do some faster pixel by pixel transformations than PIL’s PixelAccess object would allow. I’ve figured out how to place the pixel information in a useful 3D numpy array by way of:
But I can’t seem to figure out how to load it back into the PIL object after I’ve done all my awesome transforms. I’m aware of the putdata() method, but can’t quite seem to get it to behave.
回答 0
您并不是在说putdata()行为方式到底有多精确。我假设你在做
>>> pic.putdata(a)Traceback(most recent call last):File"...blablabla.../PIL/Image.py", line 1185,in putdata
self.im.putdata(data, scale, offset)SystemError: new style getargs format but argument isnot a tuple
这是因为putdata需要一个元组序列,并且您要给它一个numpy数组。这个
>>> data = list(tuple(pixel)for pixel in pix)>>> pic.putdata(data)
You’re not saying how exactly putdata() is not behaving. I’m assuming you’re doing
>>> pic.putdata(a)
Traceback (most recent call last):
File "...blablabla.../PIL/Image.py", line 1185, in putdata
self.im.putdata(data, scale, offset)
SystemError: new style getargs format but argument is not a tuple
This is because putdata expects a sequence of tuples and you’re giving it a numpy array. This
>>> data = list(tuple(pixel) for pixel in pix)
>>> pic.putdata(data)
from PIL importImageimport numpy as np
im =Image.open('1.jpg')
im2arr = np.array(im)# im2arr.shape: height x width x channel
arr2im =Image.fromarray(im2arr)
I am using Pillow 4.1.1 (the successor of PIL) in Python 3.5. The conversion between Pillow and numpy is straightforward.
from PIL import Image
import numpy as np
im = Image.open('1.jpg')
im2arr = np.array(im) # im2arr.shape: height x width x channel
arr2im = Image.fromarray(im2arr)
One thing that needs noticing is that Pillow-style im is column-major while numpy-style im2arr is row-major. However, the function Image.fromarray already takes this into consideration. That is, arr2im.size == im.size and arr2im.mode == im.mode in the above example.
We should take care of the HxWxC data format when processing the transformed numpy arrays, e.g. do the transform im2arr = np.rollaxis(im2arr, 2, 0) or im2arr = np.transpose(im2arr, (2, 0, 1)) into CxHxW format.
def select_all_X_values(conn):
cur = conn.cursor()
cur.execute("SELECT ImageData from PiecesTable")
rows = cur.fetchall()return rows
然后,我创建了一个辅助函数,将我的数据集更改为np.array:
X_dataset = select_all_X_values(conn)
imagesList = convertToByteIO(np.array(X_dataset))def convertToByteIO(imagesArray):"""
# Converts an array of images into an array of Bytes
"""
imagesList =[]for i in range(len(imagesArray)):
img =Image.open(BytesIO(imagesArray[i])).convert("RGB")
imagesList.insert(i, np.array(img))return imagesList
If your image is stored in a Blob format (i.e. in a database) you can use the same technique explained by Billal Begueradj to convert your image from Blobs to a byte array.
In my case, I needed my images where stored in a blob column in a db table:
def select_all_X_values(conn):
cur = conn.cursor()
cur.execute("SELECT ImageData from PiecesTable")
rows = cur.fetchall()
return rows
I then created a helper function to change my dataset into np.array:
X_dataset = select_all_X_values(conn)
imagesList = convertToByteIO(np.array(X_dataset))
def convertToByteIO(imagesArray):
"""
# Converts an array of images into an array of Bytes
"""
imagesList = []
for i in range(len(imagesArray)):
img = Image.open(BytesIO(imagesArray[i])).convert("RGB")
imagesList.insert(i, np.array(img))
return imagesList
After this, I was able to use the byteArrays in my Neural Network.
plt.imshow(imagesList[0])
回答 6
转换Numpy to PIL图像并PIL to Numpy
import numpy as np
from PIL importImagedef pilToNumpy(img):return np.array(img)defNumpyToPil(img):returnImage.fromarray(img)
import numpy as np
data = np.zeros( (512,512,3), dtype=np.uint8)
data[256,256] = [255,0,0]
What I want this to do is display a single red dot in the center of a 512×512 image. (At least to begin with… I think I can figure out the rest from there)
回答 0
您可以使用PIL创建(并显示)图像:
from PIL importImageimport numpy as np
w, h =512,512
data = np.zeros((h, w,3), dtype=np.uint8)
data[0:256,0:256]=[255,0,0]# red patch in upper left
img =Image.fromarray(data,'RGB')
img.save('my.png')
img.show()
Using pygame, you can open a window, get the surface as an array of pixels, and manipulate as you want from there. You’ll need to copy your numpy array into the surface array, however, which will be much slower than doing actual graphics operations on the pygame surfaces themselves.
回答 4
如何使用示例显示存储在numpy数组中的图像(在Jupyter笔记本中有效)
我知道有更简单的答案,但是这一答案将使您了解如何从numpy数组中淹没图像。
加载示例
from sklearn.datasets import load_digits
digits = load_digits()
digits.images.shape #this will give you (1797, 8, 8). 1797 images, each 8 x 8 in size
from matplotlib import pyplot as plot
import numpy as np
fig = plot.figure()
ax = fig.add_subplot(1,1,1)# make sure your data is in H W C, otherwise you can change it by# data = data.transpose((_, _, _))
data = np.zeros((512,512,3), dtype=np.int32)
data[256,256]=[255,0,0]
ax.imshow(data.astype(np.uint8))
Supplement for doing so with matplotlib. I found it handy doing computer vision tasks. Let’s say you got data with dtype = int32
from matplotlib import pyplot as plot
import numpy as np
fig = plot.figure()
ax = fig.add_subplot(1, 1, 1)
# make sure your data is in H W C, otherwise you can change it by
# data = data.transpose((_, _, _))
data = np.zeros((512,512,3), dtype=np.int32)
data[256,256] = [255,0,0]
ax.imshow(data.astype(np.uint8))
from PIL import Image
with Image.open(filepath) as img:
width, height = img.size
Speed
This needed 3.21 seconds for 30336 images (JPGs from 31×21 to 424×428, training data from National Data Science Bowl on Kaggle)
This is probably the most important reason to use Pillow instead of something self-written. And you should use Pillow instead of PIL (python-imaging), because it works with Python 3.
Alternative #1: Numpy (deprecated)
I keep scipy.ndimage.imread as the information is still out there, but keep in mind:
imread is deprecated! imread is deprecated in SciPy 1.0.0, and [was] removed in 1.2.0.
This is a complete example loading image from URL, creating with PIL, printing the size and resizing…
import requests
h = { 'User-Agent': 'Neo'}
r = requests.get("https://images.freeimages.com/images/large-previews/85c/football-1442407.jpg", headers=h)
from PIL import Image
from io import BytesIO
# create image from binary content
i = Image.open(BytesIO(r.content))
width, height = i.size
print(width, height)
i = i.resize((100,100))
display(i)
回答 4
这是从Python 3中的给定URL获取图像大小的方法:
from PIL importImageimport urllib.request
from io importBytesIO
file =BytesIO(urllib.request.urlopen('http://getwallpapers.com/wallpaper/full/b/8/d/32803.jpg').read())
im =Image.open(file)
width, height = im.size
I have a matrix in the type of a Numpy array. How would I write it to disk it as an image? Any format works (png, jpeg, bmp…). One important constraint is that PIL is not present.
EDIT: The current scipy version started to normalize all images so that min(data) become black and max(data) become white. This is unwanted if the data should be exact grey levels or exact RGB channels. The solution:
Arguments:*fname*:
A string containing a path to a filename,or a Python file-like object.If*format*is*None*and*fname*is a string, the output
format is deduced from the extension of the filename.*arr*:AnMxN(luminance),MxNx3(RGB)orMxNx4(RGBA) array.
Works with matplotlib 1.3.1, I don’t know about lower version. From the docstring:
Arguments:
*fname*:
A string containing a path to a filename, or a Python file-like object.
If *format* is *None* and *fname* is a string, the output
format is deduced from the extension of the filename.
*arr*:
An MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA) array.
回答 4
纯Python(2和3),无第三方依赖的代码段。
此函数写入压缩的真彩色(每个像素4个字节)RGBAPNG。
def write_png(buf, width, height):""" buf: must be bytes or a bytearray in Python3.x,
a regular string in Python2.x.
"""import zlib, struct
# reverse the vertical line order and add null bytes at the start
width_byte_4 = width *4
raw_data = b''.join(
b'\x00'+ buf[span:span + width_byte_4]for span in range((height -1)* width_byte_4,-1,- width_byte_4))def png_pack(png_tag, data):
chunk_head = png_tag + data
return(struct.pack("!I", len(data))+
chunk_head +
struct.pack("!I",0xFFFFFFFF& zlib.crc32(chunk_head)))return b''.join([
b'\x89PNG\r\n\x1a\n',
png_pack(b'IHDR', struct.pack("!2I5B", width, height,8,6,0,0,0)),
png_pack(b'IDAT', zlib.compress(raw_data,9)),
png_pack(b'IEND', b'')])
…数据应直接写入以二进制格式打开的文件,如:
data = write_png(buf,64,64)with open("my_image.png",'wb')as fh:
fh.write(data)
def saveAsPNG(array, filename):import struct
if any([len(row)!= len(array[0])for row in array]):raiseValueError,"Array should have elements of equal size"#First row becomes top row of image.
flat =[]; map(flat.extend, reversed(array))#Big-endian, unsigned 32-byte integer.
buf = b''.join([struct.pack('>I',((0xffFFff& i32)<<8)|(i32>>24))for i32 in flat])#Rotate from ARGB to RGBA.
data = write_png(buf, len(array[0]), len(array))
f = open(filename,'wb')
f.write(data)
f.close()
def saveAsPNG(array, filename):
import struct
if any([len(row) != len(array[0]) for row in array]):
raise ValueError, "Array should have elements of equal size"
#First row becomes top row of image.
flat = []; map(flat.extend, reversed(array))
#Big-endian, unsigned 32-byte integer.
buf = b''.join([struct.pack('>I', ((0xffFFff & i32)<<8)|(i32>>24) )
for i32 in flat]) #Rotate from ARGB to RGBA.
data = write_png(buf, len(array[0]), len(array))
f = open(filename, 'wb')
f.write(data)
f.close()
(Transparency also works, by reducing the high byte from 0xff.)
回答 10
对于那些希望直接工作的示例:
from PIL importImageimport numpy
w,h =200,100
img = numpy.zeros((h,w,3),dtype=numpy.uint8)# has to be unsigned bytes
img[:]=(0,0,255)# fill blue
x,y =40,20
img[y:y+30, x:x+50]=(255,0,0)# 50x30 red boxImage.fromarray(img).convert("RGB").save("art.png")# don't need to convert
For those looking for a direct fully working example:
from PIL import Image
import numpy
w,h = 200,100
img = numpy.zeros((h,w,3),dtype=numpy.uint8) # has to be unsigned bytes
img[:] = (0,0,255) # fill blue
x,y = 40,20
img[y:y+30, x:x+50] = (255,0,0) # 50x30 red box
Image.fromarray(img).convert("RGB").save("art.png") # don't need to convert
also, if you want high quality jpeg’s .save(file, subsampling=0, quality=100)
matplotlib svn has a new function to save images as just an image — no axes etc. it’s a very simple function to backport too, if you don’t want to install svn (copied straight from image.py in matplotlib svn, removed the docstring for brevity):
import numpy as np
from numpngw import write_png
# Example 1## Create an 8-bit RGB image.
img = np.zeros((80,128,3), dtype=np.uint8)
grad = np.linspace(0,255, img.shape[1])
img[:16,:,:]=127
img[16:32,:,0]= grad
img[32:48,:,1]= grad[::-1]
img[48:64,:,2]= grad
img[64:,:,:]=127
write_png('example1.png', img)
The world probably doesn’t need yet another package for writing a numpy array to a PNG file, but for those who can’t get enough, I recently put up numpngw on github:
import numpy as np
import imageio
# data is numpy array with grayscale value for each pixel.
data = np.array([70,80,82,72,58,58,60,63,54,58,60,48,89,115,121,119])# 16 pixels can be converted into square of 4x4 or 2x8 or 8x2
data = data.reshape((4,4)).astype('uint8')# save image
imageio.imwrite('pic.jpg', data)
Imageio is a Python library that provides an easy interface to read and write a wide range of image data, including animated images, video, volumetric data, and scientific formats. It is cross-platform, runs on Python 2.7 and 3.4+, and is easy to install.
This is example for grayscale image:
import numpy as np
import imageio
# data is numpy array with grayscale value for each pixel.
data = np.array([70,80,82,72,58,58,60,63,54,58,60,48,89,115,121,119])
# 16 pixels can be converted into square of 4x4 or 2x8 or 8x2
data = data.reshape((4, 4)).astype('uint8')
# save image
imageio.imwrite('pic.jpg', data)
If you happen to use [Py]Qt already, you may be interested in qimage2ndarray. Starting with version 1.4 (just released), PySide is supported as well, and there will be a tiny imsave(filename, array) function similar to scipy’s, but using Qt instead of PIL. With 1.3, just use something like the following:
qImage = array2qimage(image, normalize = False) # create QImage from ndarray
success = qImage.save(filename) # use Qt's image IO functions for saving PNG/JPG/..
(Another advantage of 1.4 is that it is a pure python solution, which makes this even more lightweight.)
If you are working in python environment Spyder, then it cannot get more easier than to just right click the array in variable explorer, and then choose Show Image option.
This will ask you to save image to dsik, mostly in PNG format.
PIL library will not be needed in this case.
回答 17
使用cv2.imwrite。
import cv2
assert mat.shape[2]==1or mat.shape[2]==3,'the third dim should be channel'
cv2.imwrite(path, mat)# note the form of data should be height - width - channel
import cv2
assert mat.shape[2] == 1 or mat.shape[2] == 3, 'the third dim should be channel'
cv2.imwrite(path, mat) # note the form of data should be height - width - channel
回答 18
为了将一个numpy数组另存为图像,U有几种选择:
1)其他最佳:OpenCV
import cv2
cv2.imwrite('file name with extension(like .jpg)', numpy_array)
2)Matplotlib
from matplotlib import pyplot as plt
plt.imsave('file name with extension(like .jpg)', numpy_array)
3)PIL
from PIL importImage
image =Image.fromarray(numpy_array)
image.save('file name with extension(like .jpg)')
You mustn’t use quotation marks around the name of the image files in markdown!
If you carefully read your error message, you will see the two %22 parts in the link. That is the html encoded quotation mark.
You have to change the line
![title]("img/picture.png")
to
![title](img/picture.png)
UPDATE
It is assumed, that you have the following file structure and that you run the jupyter notebook command in the directory where the file example.ipynb (<– contains the markdown for the image) is stored:
/
+-- example.ipynb
+-- img
+-- picture.png
回答 1
有几种方法可以在Jupyter笔记本中发布图像:
通过HTML:
fromIPython.display importImagefromIPython.core.display import HTML
Image(url="http://my_site.com/my_picture.jpg")
if the image it wider than the display settings:thanks
use unconfined=True to disable max-width confinement of the image
from IPython.core.display import Image, display
display(Image('https://i.ytimg.com/vi/j22DmsZEv30/maxresdefault.jpg', width=1900, unconfined=True))
or via markdown:
make sure the cell is a markdown cell, and not a code cell, thanks @游凯超 in the comments)
Please note that on some systems, the markdown does not allow white space in the filenames. Thanks to @CoffeeTableEspresso and @zebralamy in the comments)
(On macos, as long as you are on a markdown cell you would do like this: ![title](../image 1.png), and not worry about the white space).
for a web image:
![Image of Yaktocat](https://octodex.github.com/images/yaktocat.png)
as shown by @cristianmtr
Paying attention not to use either these quotes "" or those '' around the url.
I know this is not fully relevant, but since this answer is ranked first many a times when you search ‘how to display images in Jupyter‘, please consider this answer as well.
You could use matplotlib to show an image as follows.
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread("your_image.png")
plt.imshow(image)
plt.show()
In addition to the other answers using HTML (either in Markdown or using the %%HTML magic:
If you need to specify the image height, this will not work:
<img src="image.png" height=50> <-- will not work
That is because the CSS styling in Jupyter uses height: auto per default for the img tags, which overrides the HTML height attribute. You need need to overwrite the CSS height attribute instead:
<img src="image.png" style="height:50px"> <-- works
Insert the image directly in the Jupyter notebook.
Note: You should have a local copy of the image on your computer
You can insert the image in the Jupyter notebook itself. This way you don’t need to keep the image separately in the folder.
Steps:
Convert the cell to markdown by:
pressing M on the selected cell
OR
From menu bar, Cell > Cell Type > Markdown.
(Note: It’s important to convert the cell to Markdown, otherwise the “Insert Image” option in Step 2 will not be active)
Now go to menu bar and select Edit -> Insert Image.
Select image from your disk and upload.
Press Ctrl+Enter or Shift+Enter.
This will make the image as part of the notebook and you don’t need to upload in the directory or Github. I feel this looks more clean and not prone to broken URL issue.
回答 7
使用Markdown的方法如下:
![Image of Yaktocat](https://octodex.github.com/images/yaktocat.png)
Drag and drop your image into the cell. The following command will be created:
![image.png](attachment:image.png)
Execute/Run the cell and the image shows up.
The image is actually embedded in the ipynb Notebook and you don’t need to mess around with separate files. This is unfortunately not working with Jupyter-Lab (v 1.1.4) yet.
If you want to use the Jupyter Notebook API (and not the IPython one anymore), I find the ipywidgets Jupyter’s sub-project. You have an Image widget. Docstring specifies that you have a value parameter which is a bytes. So you can do:
import requests
from ipywidgets import Image
Image(value=requests.get('https://octodex.github.com/images/yaktocat.png').content)
I agree, it’s simpler to use the Markdown style. But it shows you the Image display Notebook API. You can also resize the image with the width and height parameters.
One thing I found is the path of your image must be relative to wherever the notebook was originally loaded from. if you cd to a different directory, such as Pictures your Markdown path is still relative to the original loading directory.
回答 13
同意,我遇到了同样的问题,这是可行的,而没有奏效的:
WORKED:<img src="Docs/pinoutDOIT32devkitv1.png" width="800"/>*DOES NOT WORK:<img src="/Docs/pinoutDOIT32devkitv1.png" width="800"/>
DOES NOT WORK:<img src="./Docs/pinoutDOIT32devkitv1.png" width="800"/>*
要在浏览器中查看图像,您可以访问data:image/png;base64,**image data here**以base64编码的PNG图像或data:image/jpg;base64,**image data here**以base64编码的JPG图像的链接。在此答案的末尾可以找到一个示例链接。
While a lot of the above answers give ways to embed an image using a file or with Python code, there is a way to embed an image in the jupyter notebook itself using only markdown and base64!
To view an image in the browser, you can visit the link data:image/png;base64,**image data here** for a base64-encoded PNG image, or data:image/jpg;base64,**image data here** for a base64-encoded JPG image. An example link can be found at the end of this answer.
To embed this into a markdown page, simply use a similar construct as the file answers, but with a base64 link instead: ![**description**](data:image/**type**;base64,**base64 data**). Now your image is 100% embedded into your Jupyter Notebook file!
Example link: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABHNCSVQICAgIfAhkiAAAAD9JREFUGJW1jzEOADAIAqHx/1+mE4ltNXEpI3eJQknCIGsiHSLJB+aO/06PxOo/x2wBgKR2jCeEy0rOO6MDdzYQJRcVkl1NggAAAABJRU5ErkJggg==
Example markdown:
![smile](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABHNCSVQICAgIfAhkiAAAAD9JREFUGJW1jzEOADAIAqHx/1+mE4ltNXEpI3eJQknCIGsiHSLJB+aO/06PxOo/x2wBgKR2jCeEy0rOO6MDdzYQJRcVkl1NggAAAABJRU5ErkJggg==)
This script will resize an image (somepic.jpg) using PIL (Python Imaging Library) to a width of 300 pixels and a height proportional to the new width. It does this by determining what percentage 300 pixels is of the original width (img.size[0]) and then multiplying the original height (img.size[1]) by that percentage. Change “basewidth” to any other number to change the default width of your images.
from PIL importImagedef get_resized_img(img_path, video_size):
img =Image.open(img_path)
width, height = video_size # these are the MAX dimensions
video_ratio = width / height
img_ratio = img.size[0]/ img.size[1]if video_ratio >=1:# the video is wideif img_ratio <= video_ratio:# image is not wide enough
width_new = int(height * img_ratio)
size_new = width_new, height
else:# image is wider than video
height_new = int(width / img_ratio)
size_new = width, height_new
else:# the video is tallif img_ratio >= video_ratio:# image is not tall enough
height_new = int(width / img_ratio)
size_new = width, height_new
else:# image is taller than video
width_new = int(height * img_ratio)
size_new = width_new, height
return img.resize(size_new, resample=Image.LANCZOS)
I was trying to resize some images for a slideshow video and because of that, I wanted not just one max dimension, but a max width and a max height (the size of the video frame).
And there was always the possibility of a portrait video…
The Image.thumbnail method was promising, but I could not make it upscale a smaller image.
So after I couldn’t find an obvious way to do that here (or at some other places), I wrote this function and put it here for the ones to come:
from PIL import Image
def get_resized_img(img_path, video_size):
img = Image.open(img_path)
width, height = video_size # these are the MAX dimensions
video_ratio = width / height
img_ratio = img.size[0] / img.size[1]
if video_ratio >= 1: # the video is wide
if img_ratio <= video_ratio: # image is not wide enough
width_new = int(height * img_ratio)
size_new = width_new, height
else: # image is wider than video
height_new = int(width / img_ratio)
size_new = width, height_new
else: # the video is tall
if img_ratio >= video_ratio: # image is not tall enough
height_new = int(width / img_ratio)
size_new = width, height_new
else: # image is taller than video
width_new = int(height * img_ratio)
size_new = width_new, height
return img.resize(size_new, resample=Image.LANCZOS)
Function get file like: “pic[0-9a-z].[extension]”, resize them to 120×120, moves section to center and save to “ico[0-9a-z].[extension]”, works with portrait and landscape:
from PIL importImage
img_path ="filename.png";
img =Image.open(img_path);# puts our image to the buffer of the PIL.Image object
width, height = img.size;
asp_rat = width/height;# Enter new width (in pixels)
new_width =50;# Enter new height (in pixels)
new_height =54;
new_rat = new_width/new_height;if(new_rat == asp_rat):
img = img.resize((new_width, new_height),Image.ANTIALIAS);# adjusts the height to match the width# NOTE: if you want to adjust the width to the height, instead -> # uncomment the second line (new_width) and comment the first one (new_height)else:
new_height = round(new_width / asp_rat);#new_width = round(new_height * asp_rat);
img = img.resize((new_width, new_height),Image.ANTIALIAS);# usage: resize((x,y), resample)# resample filter -> PIL.Image.BILINEAR, PIL.Image.NEAREST (default), PIL.Image.BICUBIC, etc..# https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.resize# Enter the name under which you would like to save the new image
img.save("outputname.png");
I will also add a version of the resize that keeps the aspect ratio fixed.
In this case, it will adjust the height to match the width of the new image, based on the initial aspect ratio, asp_rat, which is float (!).
But, to adjust the width to the height, instead, you just need to comment one line and uncomment the other in the else loop. You will see, where.
You do not need the semicolons (;), I keep them just to remind myself of syntax of languages I use more often.
from PIL import Image
img_path = "filename.png";
img = Image.open(img_path); # puts our image to the buffer of the PIL.Image object
width, height = img.size;
asp_rat = width/height;
# Enter new width (in pixels)
new_width = 50;
# Enter new height (in pixels)
new_height = 54;
new_rat = new_width/new_height;
if (new_rat == asp_rat):
img = img.resize((new_width, new_height), Image.ANTIALIAS);
# adjusts the height to match the width
# NOTE: if you want to adjust the width to the height, instead ->
# uncomment the second line (new_width) and comment the first one (new_height)
else:
new_height = round(new_width / asp_rat);
#new_width = round(new_height * asp_rat);
img = img.resize((new_width, new_height), Image.ANTIALIAS);
# usage: resize((x,y), resample)
# resample filter -> PIL.Image.BILINEAR, PIL.Image.NEAREST (default), PIL.Image.BICUBIC, etc..
# https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#PIL.Image.Image.resize
# Enter the name under which you would like to save the new image
img.save("outputname.png");
And, it is done. I tried to document it as much as I can, so it is clear.