1

In a IJulia notebook, I have a cell that load an image from my filesystem then split it into few other images but when they are displayed, they are too small.

using Images
using FileIO

path = "samples/201478670932639746-3.png"
img = load(path)

height, width = size(img)
print((height, width))
lines = [
    (@view img[20:55, :]),
    (@view img[70:105, :]),
    (@view img[120:155, :]),
    (@view img[155:end, :])
]

The result look like this:

I've looked into the html code of and image:

<img style="max-width: 100px; max-height:100px;display:inline" src="data:image/png;base64,some_base64_data">

So i read the documentation of JuliaImages for looking how images are displayed on notebook: https://juliaimages.org/latest/quickstart/#Display-1

We can read here the package ImageShow is used for display image on notebook So i jump on the source code of the package on github: https://github.com/JuliaImages/ImageShow.jl/blob/master/src/showmime.jl#L114

    w,h=get(io, :thumbnailsize, (100,100))
    im_resized = downsize_for_thumbnail(img, w, h)
    thumbnail_style = get(io, :thumbnail, false) ? "max-width: $(w)px; max-height:$(h)px;" : ""

we can see here they are looking for :thumbnail in io, and the default value is (100,100) I suppose io is a IOContext given by the notebook

So, how can i write into the IOContext of an IJulia notebook for change thumbnail size ?

aret
  • 126
  • 2
  • 6

1 Answers1

0

In general changing default behavior of internal functions of an external package can cause problems when the versions change, so watch out for breakage on ImageShow.jl update! But if you must, you can overload the function, keeping the code the same except for changing what is hardcoded:

using IJulia
using ImageShow

import ImageShow.show_element

const mydim = [(100, 100)]
function ImageShow.show_element(io::IOContext, img)
    io2=IOBuffer()
    w,h=get(io, :thumbnailsize, mydim[1])
    im_resized = ImageShow.downsize_for_thumbnail(img, w, h)
    thumbnail_style = get(io, :thumbnail, false) ? "max-width: $(w)px; max-height:$(h)px;" : ""
    b64pipe=ImageShow.Base64EncodePipe(io2)
    write(io,"<img style='$(thumbnail_style)display:inline' src=\"data:image/png;base64,")
    show(b64pipe, MIME"image/png"(), im_resized)
    write(io, read(seekstart(io2)))
    write(io,"\">")
end

mydim[1] = (500, 500)
Bill
  • 5,600
  • 15
  • 27