Hello i need help with a very simple Python 3 script. In the script i try open a png image file from a folder with this:
png = Image.open('img/image.png', + 'r')
It's work good, but now i need get random .png images from same folder, but after some unsuccessful try, I'll like know how do it, actually i use in my script
from random import randint
import uuid
from PIL import Image
Any help will be appreciated, thank you all
import glob
import random
from PIL import Image
img = random.choice(glob.glob('img/*.png'))
png = Image.open(img, + 'r')
import os,random
from PIL import Image
random_image = random.choice(os.listdir("img"))
Image.open(random_image)
Related
These are frameworks that I use:
from pdf2image import convert_from_path
from PIL import Image, ImageChops
import cv2
import numpy as np
from shapely.geometry import Polygon
import svgwrite
import json
from defcon import Font
import ufo2ft
from fontTools import svgLib
I have a svg file looks like the image below this sentence(The image is upside-down).
SVG file
I load this file and add to Defcon.Font like this:
glyph = font.newGlyph(name=korean_unicode_list[int(key)])
glyph.unicode = int(korean_unicode_list[int(key)], 16)
pen = glyph.getPen()
svg = svgLib.SVGPath("svg_files/" + str(key) + ".svg")
svg.draw(pen)
But after I export this Font object with ufo2ft, The glyph's hole has disappeared :(
ttf = ufo2ft.compileTTF(font)
ttf.save('myFont.ttf')
After Export
I don't know how to make holes at glyphs. I'm guessing that there is a solution at pen object and its methods, but there is no example about glyph with holes. How can I get a glyph with holes?
I got a solution myself :)
I tested with Adobe Illustrator. I made '0'-shaped object and saved in svg file. When I export a font file with the svg file, there was a hole.
So I opened the svg file at PyCharm, there's a option name 'xml:space'. If its option set to "preserve", svgPath can draw a path with holes.
I hope this article help you.
I want to pick a random image from the desktop and pass that image into imread() function. Currently, I have added a predefined image into imread(). How can I enter a randomly selected image into that function?
img = cv2.imread("floor_plan_02.png", 0)
If you are running with the Desktop as your current directory, it is as simple as this:
import random
from glob import glob
# Get name of a random PNG file
filename = random.choice(glob('*.png'))
If you are running with a different current directory, you can simply change directory to the desktop first. Or you can use the rather nice pathlib to load a random PNG from your Desktop:
from pathlib import Path
import random
import cv2
# Choose random filename from all PNG files on Desktop
filename = random.choice( list((Path.home() / 'Desktop').glob('*.png')) )
# Open that file with OpenCV
im = cv2.imread(str(filename))
The easiest way to do this is to use glob and numpy
The code below will search your directory for png images & put them in a list called img_files. Then using numpy.random.randint() an index for an image in the list of images is chosen at random.
import glob
import cv2
import numpy as np
# Read in all *.png file names
img_files = []
for file in glob.glob("*.png"):
img_files.append(file)
print(img_files)
# Choose an image at random & read it in
random_image = img_files[np.random.randint(0, len(img_files))]
print(random_image)
img = cv2.imread(random_image, cv2.IMREAD_COLOR)
# Display image
cv2.namedWindow("Random image")
cv2.imshow("Random image", img)
cv2.waitKey()
cv2.destroyAllWindows()
I'm trying to extract a set of photos from a zip file using python
and then saving those pictures to an image list to do some work on each.
I tried a lot, but nothing was useful to me.
Try this:
import zipfile
path = 'path_to_zip.zip'
input_zip = zipfile.ZipFile(path)
l = [input_zip.read(name) for name in input_zip.namelist()]
To Display one of the images you can do:
import io
import matplotlib.pyplot as plt
from PIL import Image
image = Image.open(io.BytesIO(l[0]))
plt.imshow(image)
I wanted read a image using PIL.Image.open().But I've image in different path.
The following is the path I've the python script
"D:\YY_Aadhi\holy-edge-master\hed\test.py"
The following is the path I've the image file.
"D:\YY_Aadhi\HED-BSDS\test\2018.jpg"
from PIL import Image
'''some code here'''
image = Image.open(????)
How should I fill the question mark to access the image file.
you can simply do
from PIL import Image
image = Image.open("D:\\YY_Aadhi\\HED-BSDS\\test\\2018.jpg")
or
from PIL import Image
directory = "D:\\YY_Aadhi\\HED-BSDS\\test\\2018.jpg"
image = Image.open(directory)
like this.
you have to write escape sequence twice in windows, when you want to define as directory. and It will be great if you try some stupid code. It helps you a lot.
Does this image = Image.open("D:\YY_Aadhi\HED-BSDS\test\2018.jpg") not do the trick?
You can use this to read an online image
from urllib.request import urlopen
url = 'https://somewebsite/images/logo.png'
msg_image = urlopen(url).read()
I'm trying to blur an image using PIL:
from PIL import Image
from PIL import ImageFilter
im = Image.open("plot.png")
im = im.filter(ImageFilter.BLUR)
When I do im.show() and save it to my hard drive, it saves as a BMP file, which is incompatible with the place where I'm trying to upload it. How do I change the file format from BMP to something else that is compatible?
Just use the save() function directly:
from PIL import Image
from PIL import ImageFilter
im = Image.open("plot.png")
im = im.filter(ImageFilter.BLUR)
im.save("saved.jpg")
This function supports many formats, as explained in the documentation.