I am attempting to run this code:
# -*- coding: utf-8 -*-
"""
Created on Wed May 13 12:43:07 2020
#author: Matthew Farnham
"""
import gdal
import os.path
import rasterio
from rasterio.merge import merge
from rasterio.plot import show
import glob
import os
import numpy as np
with rasterio.Env(GDAL_TIFF_INTERNAL_MASK=True):
PIXELTYPE = 'DEFAULT',
TFW='YES'
directory = r"H:\Projects\Countries\USA\State\Washington_DC\DTM\LIDAR\Datagateway\Unzipped\elevation"
Output = r"E:\Previsico\PERM\Matthew\Previsico\7_Model_Workings\DEM_Mosaic\Data_Testing_Keep_empty\Trial1\Test3.tif"
search_criteria = "*.tif"
q = os.path.join(directory, search_criteria)
print(q)
dem_fps = glob.glob(q)
print(dem_fps)
Mosaic_files = []
for i in dem_fps:
src = rasterio.open(i)
Mosaic_files.append(src)
out_meta = src.meta.copy()
mosaic, out_trans = merge(Mosaic_files)
out_meta = src.meta.copy()
out_meta.update({"driver": "GTiff",
"height": mosaic.shape[1],
"width": mosaic.shape[2],
"transform": out_trans,
"crs":"+proj= unknown",
"BigTIFF" : "yes",
}
)
with rasterio.open(Output, "w", BigTIFF = 'YES', **out_meta) as dest:
dest.write(mosaic)
I am running it in Python 3.7 in a 64-bit environment. The error is:"MemoryError: Unable to allocate 17.4 GiB for an array with shape (1, 70544, 66261) and data type float32"
I am trying to mosaic (merge) raster files (.tif) to create a raster dataset comprising all of the individual raster files. Is there a way to get Python to write it first as opposed to storing everything to memory?
Thank you all for your help!
Related
I've generated a barcode PNG image with the following code:
import barcode
from barcode.writer import ImageWriter
image_writer = ImageWriter()
ean = barcode.get('ean13', '123456789102', writer=image_writer)
filename = ean.save('ean13')
But I want to change the human readable text. I want to put it closer to the barcode and change the font size.
So. according to the library docs: https://python-barcode.readthedocs.io/en/stable/writers/index.html:
All writer take the following options (specified as keyword arguments
to Barcode.save(filename, options) or set via
Writer.set_options(options), where options is a dictionary where keys
are option names and values are option values to be set).
If I do this:
import barcode
from barcode.writer import ImageWriter
options = {
'font_size': 20,
'text_distance': 1.0,
}
image_writer = ImageWriter()
ean = barcode.get('ean13', '123456789102', writer=image_writer)
filename = ean.save('ean13', options)
It works as expected. The problem is that in the future I won't want to generate an image file stored on disk, and write method does not accept options.
So I'm trying the other way, using set_options, which theoretically works for every kind of Writer as they inherit from BaseWriter, but if I do this:
import barcode
from barcode.writer import ImageWriter
options = {
'format': 'PNG',
'font_size': 20,
'text_distance': 1.0,
}
image_writer = ImageWriter().set_options(options)
ean = barcode.get('ean13', '123456789102', writer=image_writer)
filename = ean.save('ean13')
Not only I don't get what I want but the generated image file is a SVG.
Why is this happening? Any ideas of how can I achieve my purpose?
did you try using render?
from barcode import Code128
from barcode.writer import ImageWriter
render_options = {
"module_width": 0.25,
"module_height": 6,
"write_text": False,
"module_width": 0.25,
"quiet_zone": 0.1,
}
barcode=Code128("123456789",writer=ImageWriter()).render(render_options)
I am aware of adding URL to PPT, but due to certain privacy issues that cannot be done.
Is there any other way to import plotly charts to ppt? or any equivalent open source app that can be used?
Thank you in advance!
it's pretty straight forward to save as a static image https://plotly.com/python/static-image-export/
create a powerpoint https://python-pptx.readthedocs.io/en/latest/user/quickstart.html#
I don't have powerpoint or a Windows o/s. It's not clear that powerpoint works with active HTML
import plotly.express as px
import pandas as pd
import numpy as np
from pathlib import Path
df = pd.DataFrame({
"time": pd.date_range("1-Jul-2021", periods=200, freq="1H"),
"desired_light": np.random.choice(["ON", "OFF"], 200),
})
# build a figure
fig = px.bar(df, x="time", color="desired_light", height=400, title="Restaurant bills")
# export fingure as a static file: https://plotly.com/python/static-image-export/
fname = Path.cwd().joinpath("SO.png")
fig.write_image(fname)
# create a powerpoint from an image: https://python-pptx.readthedocs.io/en/latest/user/quickstart.html
from pptx import Presentation
from pptx.util import Inches
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = Inches(1)
pic = slide.shapes.add_picture(str(fname), left, top)
prs.save(Path.cwd().joinpath('SO.pptx'))
I just want to add a shadow to the shapes that I am creating while using python-pptx.
I have read as many documents about using shadows in python-pptx as I can find but I can not figure out how to actually do it.
I tried shadow = shape.shadow to create a 'ShadowFormat' object but when I try to do shadow.visible I get the error AttributeError: 'ShadowFormat' object has no attribute 'visible'
If anyone could explain how this is done and give an example it would be much appreciated!
Extra info:
This is the page linking to the topic: https://python-pptx.readthedocs.io/en/latest/dev/analysis/shp-shadow.html however there is no example on how to create a shadow for a shape in powerpoint.
I have imported the following modules:
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE
from pptx.enum.action import PP_ACTION
from pptx.util import Cm
from pptx.enum.dml import MSO_THEME_COLOR_INDEX
from pptx.enum.text import MSO_AUTO_SIZE
from pptx.util import Pt
I am using python-pptx v0.6.18 and python v3.8
Edit
Example that creates the shape but no shadow appears:
#Import modules
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE
from pptx.util import Cm
from pptx.enum.dml import MSO_THEME_COLOR_INDEX
from pptx.util import Pt
#Open powerpoint file
prs = Presentation('filename.pptx')
#Create a slide
slidelayout = prs.slide_layouts[0]
slide = prs.slides.add_slide(slidelayout)
shapes = slide.shapes
#Add a shape
shape = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE, Cm(10), Cm(10), Cm(10), Cm(10))
#Create a shadow
shadow = shape.shadow
shadow.inherit = False
shadow.visible = True
shadow.distance = Pt(10)
shadow.shadow_type = 'outer'
shadow.angle = 45
shadow.blur_radius = Pt(5)
shadow.color = MSO_THEME_COLOR_INDEX.ACCENT_5
shadow.transparency = '50'
shadow.distance = Pt(5)
shape.shadow.style = 'outer'
#Save the powerpoint file
prs.save('filename2.pptx')
Example that creates the error message:
#Import modules
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE
from pptx.util import Cm
from pptx.enum.dml import MSO_THEME_COLOR_INDEX
from pptx.util import Pt
#Open powerpoint file
prs = Presentation('filename.pptx')
#Create a slide
slidelayout = prs.slide_layouts[0]
slide = prs.slides.add_slide(slidelayout)
shapes = slide.shapes
#Add a shape
shape = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE, Cm(10), Cm(10), Cm(10), Cm(10))
#Create a shadow
shadow = shape.shadow
shadow.visible
#Save the powerpoint file
prs.save('filename2.pptx')
The feature <ShadowFormat.visible - applies a reasonable standard shadow override.> is currently out of the scope of pptx.
The command <shadow.inherit = False> is used to remove the default setting with the shadow.
By default, the shadow visibility is set to true. If you want to show the shadow, you can either:
set <shadow.inherit = True>
remove <shadow.inherit = False>
You can use Aspose.Slides for Python to manipulate the shapes. This is a paid library, but you can get a temporary license to evaluate it. The following code example shows you how to add a shape with a shadow to a presentation:
import aspose.slides as slides
# Create a new presentation.
with slides.Presentation() as presentation:
# Create a shape.
shape = presentation.slides[0].shapes.add_auto_shape(slides.ShapeType.ROUND_CORNER_RECTANGLE, 10, 10, 20, 20)
# Set a shadow for the shape.
shape.effect_format.enable_outer_shadow_effect()
shape.effect_format.outer_shadow_effect.distance = 10
shape.effect_format.outer_shadow_effect.direction = 45
shape.effect_format.outer_shadow_effect.blur_radius = 5
shape.effect_format.outer_shadow_effect.shadow_color.color = presentation.master_theme.color_scheme.accent5.color
# Save the presentation.
presentation.save("example.pptx", slides.export.SaveFormat.PPTX)
The result:
Alternatively, you can use Aspose.Slides Cloud SDK for Python. This product provides a REST-based API for presentation processing. It is also a paid product, but you can make 150 free API calls per month for experimentation, learning, and any other purpose. The following code example creates the same shape with the shadow using Aspose.Slides Cloud:
import asposeslidescloud
from asposeslidescloud.apis.slides_api import SlidesApi
from asposeslidescloud.models.shape import Shape
from asposeslidescloud.models.effect_format import EffectFormat
from asposeslidescloud.models.outer_shadow_effect import OuterShadowEffect
slides_api = SlidesApi(None, "my_client_id", "my_client_secret")
# Let's a presentation exists in a storage.
file_name = "example.pptx"
slide_index = 1
color_scheme = slides_api.get_color_scheme(file_name, slide_index)
# Prepare DTO for a shape with the shadow.
shape = Shape()
shape.shape_type = "RoundCornerRectangle"
shape.x = 10
shape.y = 10
shape.width = 20
shape.height = 20
shape.effect_format = EffectFormat()
shape.effect_format.outer_shadow = OuterShadowEffect()
shape.effect_format.outer_shadow.distance = 10
shape.effect_format.outer_shadow.direction = 45
shape.effect_format.outer_shadow.blur_radius = 5
shape.effect_format.outer_shadow.shadow_color = color_scheme.accent5
# Create the shape.
slides_api.create_shape(file_name, slide_index, shape)
I work as a Support Developer at Aspose.
I'm trying to do some segmentation on a pointcloud from a kinect one in ROS. As of now i have this:
import rospy
import pcl
from sensor_msgs.msg import PointCloud2
import sensor_msgs.point_cloud2 as pc2
def on_new_point_cloud(data):
pc = pc2.read_points(data, skip_nans=True, field_names=("x", "y", "z"))
pc_list = []
for p in pc:
pc_list.append( [p[0],p[1],p[2]] )
p = pcl.PointCloud()
p.from_list(pc_list)
seg = p.make_segmenter()
seg.set_model_type(pcl.SACMODEL_PLANE)
seg.set_method_type(pcl.SAC_RANSAC)
indices, model = seg.segment()
rospy.init_node('listener', anonymous=True)
rospy.Subscriber("/kinect2/hd/points", PointCloud2, on_new_point_cloud)
rospy.spin()
This seems to work but is very slow because of the for loop.
My questions are:
1) How do i effeciently convert from the PointCloud2 message to a pcl pointcloud
2) How do i visualize the clouds.
import rospy
import pcl
from sensor_msgs.msg import PointCloud2
import sensor_msgs.point_cloud2 as pc2
import ros_numpy
def callback(data):
pc = ros_numpy.numpify(data)
points=np.zeros((pc.shape[0],3))
points[:,0]=pc['x']
points[:,1]=pc['y']
points[:,2]=pc['z']
p = pcl.PointCloud(np.array(points, dtype=np.float32))
rospy.init_node('listener', anonymous=True)
rospy.Subscriber("/velodyne_points", PointCloud2, callback)
rospy.spin()
I would prefer using ros_numpy module to first convert to numpy array and initialize Point Cloud from that array.
On Ubuntu 20.04 with Python3 I use the following:
import numpy as np
import pcl # pip3 install python-pcl
import ros_numpy # apt install ros-noetic-ros-numpy
import rosbag
import sensor_msgs
def convert_pc_msg_to_np(pc_msg):
# Fix rosbag issues, see: https://github.com/eric-wieser/ros_numpy/issues/23
pc_msg.__class__ = sensor_msgs.msg._PointCloud2.PointCloud2
offset_sorted = {f.offset: f for f in pc_msg.fields}
pc_msg.fields = [f for (_, f) in sorted(offset_sorted.items())]
# Conversion from PointCloud2 msg to np array.
pc_np = ros_numpy.point_cloud2.pointcloud2_to_xyz_array(pc_msg, remove_nans=True)
pc_pcl = pcl.PointCloud(np.array(pc_np, dtype=np.float32))
return pc_np, pc_pcl # point cloud in numpy and pcl format
# Use a ros subscriber as you already suggested or is shown in the other
# answers to run it online :)
# To run it offline on a rosbag use:
for topic, msg, t in rosbag.Bag('/my/rosbag.bag').read_messages():
if topic == "/my/cloud":
pc_np, pc_pcl = convert_pc_msg_to_np(msg)
This works for me. I just resize the point cloud since mine is an ordered pc (512x x 512px). My code is adapted from #Abdulbaki Aybakan - thanks for this!
My code:
pc = ros_numpy.numpify(pointcloud)
height = pc.shape[0]
width = pc.shape[1]
np_points = np.zeros((height * width, 3), dtype=np.float32)
np_points[:, 0] = np.resize(pc['x'], height * width)
np_points[:, 1] = np.resize(pc['y'], height * width)
np_points[:, 2] = np.resize(pc['z'], height * width)
To use ros_numpy one has to clone the repo: http://wiki.ros.org/ros_numpy
this is what i am getting when i execute the following code.
Traceback (most recent call last):
File "usrp.py",line 100 in <module>
tb.set_freq(i)
File "usrp.py",line 77 in set_freq
self.wxgui_fftsink2_0.set_baseband_freq(self.freq)
File "usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/common.py",line 131 in set
def set(value): controller[key]=value
File "usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/pubsub.py",line 44 in _setitem_
elif self._proxies[key] is not None:
AttributeError: 'fft_window' object has no attribute '_proxies'
i have seen this kind of error in cyclic dependency.i have earlier solved cyclic dependency by just importing the package instead of using from keyword.i had tried import gnuradio in ths case but of no use.Following is the code on which i am working on.it would be great help if this could be resolved.i haven't come across this kind of an error.
#!/usr/bin/env python
##################################################
# Gnuradio Python Flow Graph
# Title: Usrp
# Generated: Sat Feb 21 11:26:17 2015
##################################################
#################################################
# Gnuradio Python Flow Graph
# Title: Usrp
# Generated: Sat Feb 21 11:26:17 2015
##################################################
from gnuradio import analog
from gnuradio import blocks
from gnuradio import eng_notation
from gnuradio import gr
from gnuradio import wxgui
from gnuradio.eng_option import eng_option
from gnuradio.fft import window
from gnuradio.filter import firdes
from gnuradio.wxgui import fftsink2
from grc_gnuradio import wxgui as grc_wxgui
from optparse import OptionParser
import wx,time,random
class usrp(grc_wxgui.top_block_gui):
def __init__(self):
grc_wxgui.top_block_gui.__init__(self, title="Usrp")
##################################################
# Variables
##################################################
self.samp_rate = samp_rate = 32000
self.freq = freq = 900e6
##################################################
# Blocks
##################################################
self.wxgui_fftsink2_0 = fftsink2.fft_sink_c(
self.GetWin(),
baseband_freq=freq,
y_per_div=10,
y_divs=10,
ref_level=0,
ref_scale=2.0,
sample_rate=samp_rate
fft_size=1024,
fft_rate=15,
average=False,
avg_alpha=None,
title="FFT Plot",
peak_hold=False,
)
self.Add(self.wxgui_fftsink2_0.win)
self.blocks_throttle_0 = blocks.throttle(gr.sizeof_gr_complex*1, samp_rate,True)
self.analog_sig_source_x_0 = analog.sig_source_c(samp_rate, analog.GR_SIN_WAVE, 100e3,1, 0)
##################################################
# Connections
##################################################
self.connect((self.analog_sig_source_x_0, 0), (self.blocks_throttle_0, 0))
self.connect((self.blocks_throttle_0, 0), (self.wxgui_fftsink2_0, 0))
def get_samp_rate(self):
return self.samp_rate
def set_samp_rate(self, samp_rate):
self.samp_rate = samp_rate
self.wxgui_fftsink2_0.set_sample_rate(self.samp_rate)
self.analog_sig_source_x_0.set_sampling_freq(self.samp_rate)
self.blocks_throttle_0.set_sample_rate(self.samp_rate)
def get_freq(self):
return self.freq
def set_freq(self, freq):
self.freq = freq
self.wxgui_fftsink2_0.set_baseband_freq(self.freq)
if __name__ == '__main__':
import ctypes
import sys
if sys.platform.startswith('linux'):
try:
x11 = ctypes.cdll.LoadLibrary('libX11.so')
x11.XInitThreads()
except:
print "Warning: failed to XInitThreads()"
parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
(options, args) = parser.parse_args()
tb = usrp()
def t_range(beg,end,incr):
while beg<=end:
yield beg
beg+=incr
j=2
for i in t_range(910e6,1010e6,10e6):
tb.set_freq(i)
#time.sleep(j)
tb.Start(True)
time.sleep(j)
tb.Wait()
This is most likely not a cyclic dependency issue.
I'm however a bit concerned about that error. You see, pubsub's __init__ generates a _proxies attribute, so if a class is a subclass of pubsub, it should have _proxies; if it's not, it shouldn't see that call.
class pubsub(dict):
def __init__(self):
self._publishers = { }
self._subscribers = { }
self._proxies = { }
....
So, interestingly, your script works for me (after fixing a missing , in line 45), which might indicate you're mixing sources of different GNU Radio versions:
As a solution, I recommend making sure you're really using one version of GNU Radio, preferably the latest, and installed from source (debian's packages are rather up-to-date, too, thanks to Maitland), which can happen automagically using PyBOMBS.