👻 See our latest reviews to choose the best laptop for Machine Learning and Deep learning tasks!
By default the ipython notebook ouput is limited to a small sub window at the bottom. This makes us force to use separate scroll bar that comes with the output window, when the output is big.
Any configuration option to make it not limited in size, instead run as high as the actual output is? Or option to resize it once it gets created?
👻 Read also: what is the best laptop for engineering students in 2022?
resize ipython notebook output window resize: Questions
How do I resize an image using PIL and maintain its aspect ratio?
5 answers
Is there an obvious way to do this that I"m missing? I"m just trying to make thumbnails.
Answer #1
Define a maximum size.
Then, compute a resize ratio by taking min(maxwidth/width, maxheight/height)
.
The proper size is oldsize*ratio
.
There is of course also a library method to do this: the method Image.thumbnail
.
Below is an (edited) example from the PIL documentation.
import os, sys
import Image
size = 128, 128
for infile in sys.argv[1:]:
outfile = os.path.splitext(infile)[0] + ".thumbnail"
if infile != outfile:
try:
im = Image.open(infile)
im.thumbnail(size, Image.ANTIALIAS)
im.save(outfile, "JPEG")
except IOError:
print "cannot create thumbnail for "%s"" % infile
Answer #2
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 import Image
basewidth = 300
img = Image.open("somepic.jpg")
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
img.save("somepic.jpg")
resize ipython notebook output window resize: Questions
How to resize an image with OpenCV2.0 and Python2.6
5 answers
I want to use OpenCV2.0 and Python2.6 to show resized images. I used and adopted this example but unfortunately, this code is for OpenCV2.1 and does not seem to be working on 2.0. Here my code:
import os, glob
import cv
ulpath = "exampleshq/"
for infile in glob.glob( os.path.join(ulpath, "*.jpg") ):
im = cv.LoadImage(infile)
thumbnail = cv.CreateMat(im.rows/10, im.cols/10, cv.CV_8UC3)
cv.Resize(im, thumbnail)
cv.NamedWindow(infile)
cv.ShowImage(infile, thumbnail)
cv.WaitKey(0)
cv.DestroyWindow(name)
Since I cannot use
cv.LoadImageM
I used
cv.LoadImage
instead, which was no problem in other applications. Nevertheless, cv.iplimage has no attribute rows, cols or size. Can anyone give me a hint, how to solve this problem?
Answer #1
If you wish to use CV2, you need to use the resize
function.
For example, this will resize both axes by half:
small = cv2.resize(image, (0,0), fx=0.5, fy=0.5)
and this will resize the image to have 100 cols (width) and 50 rows (height):
resized_image = cv2.resize(image, (100, 50))
Another option is to use scipy
module, by using:
small = scipy.misc.imresize(image, 0.5)
There are obviously more options you can read in the documentation of those functions (cv2.resize, scipy.misc.imresize).
Update:
According to the SciPy documentation:
imresize
is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Useskimage.transform.resize
instead.
Note that if you"re looking to resize by a factor, you may actually want skimage.transform.rescale
.
How to print number with commas as thousands separators?
5 answers
I am trying to print an integer in Python 2.6.1 with commas as thousands separators. For example, I want to show the number 1234567
as 1,234,567
. How would I go about doing this? I have seen many examples on Google, but I am looking for the simplest practical way.
It does not need to be locale-specific to decide between periods and commas. I would prefer something as simple as reasonably possible.
Answer #1
Locale unaware
"{:,}".format(value) # For Python ≥2.7
f"{value:,}" # For Python ≥3.6
Locale aware
import locale
locale.setlocale(locale.LC_ALL, "") # Use "" for auto, or force e.g. to "en_US.UTF-8"
"{:n}".format(value) # For Python ≥2.7
f"{value:n}" # For Python ≥3.6
Reference
Per Format Specification Mini-Language,
The
","
option signals the use of a comma for a thousands separator. For a locale aware separator, use the"n"
integer presentation type instead.
Answer #2
I got this to work:
>>> import locale
>>> locale.setlocale(locale.LC_ALL, "en_US")
"en_US"
>>> locale.format("%d", 1255000, grouping=True)
"1,255,000"
Sure, you don"t need internationalization support, but it"s clear, concise, and uses a built-in library.
P.S. That "%d" is the usual %-style formatter. You can have only one formatter, but it can be whatever you need in terms of field width and precision settings.
P.P.S. If you can"t get locale
to work, I"d suggest a modified version of Mark"s answer:
def intWithCommas(x):
if type(x) not in [type(0), type(0L)]:
raise TypeError("Parameter must be an integer.")
if x < 0:
return "-" + intWithCommas(-x)
result = ""
while x >= 1000:
x, r = divmod(x, 1000)
result = ",%03d%s" % (r, result)
return "%d%s" % (x, result)
Recursion is useful for the negative case, but one recursion per comma seems a bit excessive to me.
How would you make a comma-separated string from a list of strings?
5 answers
What would be your preferred way to concatenate strings from a sequence such that between every two consecutive pairs a comma is added. That is, how do you map, for instance, ["a", "b", "c"]
to "a,b,c"
? (The cases ["s"]
and []
should be mapped to "s"
and ""
, respectively.)
I usually end up using something like "".join(map(lambda x: x+",",l))[:-1]
, but also feeling somewhat unsatisfied.
Answer #1
my_list = ["a", "b", "c", "d"]
my_string = ",".join(my_list)
"a,b,c,d"
This won"t work if the list contains integers
And if the list contains non-string types (such as integers, floats, bools, None) then do:
my_string = ",".join(map(str, my_list))