Plone.api.create create Images with image from url

Is it possible to use plone api (or restAPI) to create an Image (directly) with content from an URL?

(basically, I want to create Images in site 1 with image from site 2

The image would be the download url
( http:// site2/folder/someimage/@@download )

You mean go through a list of image URLs on site1 using urllib or so and creating an image content instance on site2 for each item in your list using the filename and data from your urllib output?

I want to run a script on plone site1 that looks for images and then adds images from plone site 2. Something happened with my images when I moved the site and installed imagecropping and I can not edit the images anymore (I get an error, tag something)

In my experience, plone.app.imagecropping does not make irreversible changes to your images. There was a transition period where you had to make sure to pin the right version(s).

For instance:

Plone 5.0.10

#plone.app.imagecropping last version for 5.0.10 Plone
# 1.4 gets me ImportError: cannot import name IImagingSchema
plone.app.imagecropping = 1.3

Plone 5.2

# newer versions handle webp,
# have better image quality and
# smarter handling of scales
Pillow = 9.3.0
plone.namedfile = 6.0.0b5
plone.scale = 4.0.0b5

# this is the plone 5 compatible branch
# we use a local version with patched dx.py and editor.py
# to support the newer plone.scale "mode" argument
plone.app.imagecropping => 2.2.2

There has been a lot of discussion here on removing old scales of images. You might want to look into doing that first, clear and rebuild your catalog and fix your otherwise working instance of Plone.

As always, back up your work before tinkering...

Thanks. Tried but still get error. I have a working site at the office, but have made lots of changes to (only) the pages in the copy I brought home for the easter vacation. I will check the versions in that setup when I am back at Tuesday, but I am pretty sure they are the same.

PS: Plone 5.2.11, if that matters

	Traceback (innermost last):
	  Module ZPublisher.WSGIPublisher, line 176, in transaction_pubevents
	  Module ZPublisher.WSGIPublisher, line 385, in publish_module
	  Module ZPublisher.WSGIPublisher, line 280, in publish
	  Module ZPublisher.mapply, line 85, in mapply
	  Module ZPublisher.WSGIPublisher, line 63, in call_object
	  Module plone.z3cform.layout, line 63, in __call__
	  Module plone.z3cform.layout, line 57, in update
	  Module z3c.form.form, line 162, in render
	  Module zope.browserpage.viewpagetemplatefile, line 46, in __call__
	  Module zope.pagetemplate.pagetemplate, line 133, in pt_render
	  Module Products.PageTemplates.engine, line 378, in __call__
	  Module z3c.pt.pagetemplate, line 176, in render
	  Module chameleon.zpt.template, line 302, in render
	  Module chameleon.template, line 192, in render
	  Module b9f31f19eef8b2faaeae507d242f4d96, line 115, in render
	  Module 7316736844189b9f2d2a303f587a7add, line 936, in render_titlelessform
	  Module 7316736844189b9f2d2a303f587a7add, line 1242, in render_fields
	  Module 7316736844189b9f2d2a303f587a7add, line 1779, in render_widget_rendering
	  Module 7316736844189b9f2d2a303f587a7add, line 1886, in render_field
	  Module zope.tales.expressions, line 250, in __call__
	  Module Products.PageTemplates.Expressions, line 225, in _eval
	  Module Products.PageTemplates.Expressions, line 155, in render
	  Module zope.browserpage.simpleviewclass, line 41, in __call__
	  Module zope.browserpage.viewpagetemplatefile, line 81, in __call__
	  Module zope.browserpage.viewpagetemplatefile, line 46, in __call__
	  Module zope.pagetemplate.pagetemplate, line 133, in pt_render
	  Module Products.PageTemplates.engine, line 378, in __call__
	  Module z3c.pt.pagetemplate, line 176, in render
	  Module chameleon.zpt.template, line 302, in render
	  Module chameleon.template, line 192, in render
	  Module 532224864bbeba555cb394bfb6de57d5, line 586, in render
	  Module 532224864bbeba555cb394bfb6de57d5, line 456, in render_widget_wrapper
	  Module zope.tales.expressions, line 250, in __call__
	  Module Products.PageTemplates.Expressions, line 225, in _eval
	  Module Products.PageTemplates.Expressions, line 155, in render
	  Module z3c.form.widget, line 154, in render
	  Module zope.browserpage.viewpagetemplatefile, line 46, in __call__
	  Module zope.pagetemplate.pagetemplate, line 133, in pt_render
	  Module Products.PageTemplates.engine, line 378, in __call__
	  Module z3c.pt.pagetemplate, line 176, in render
	  Module chameleon.zpt.template, line 302, in render
	  Module chameleon.template, line 215, in render
	  Module chameleon.utils, line 53, in raise_with_traceback
	  Module chameleon.template, line 192, in render
	  Module a38257a638c0620497de9307952fdce8, line 740, in render
	  Module zope.tales.expressions, line 250, in __call__
	  Module Products.PageTemplates.Expressions, line 217, in _eval
	  Module zope.tales.expressions, line 153, in _eval
	  Module Products.PageTemplates.Expressions, line 133, in trustedBoboAwareZopeTraverse
	  Module zope.traversing.adapters, line 156, in traversePathElement
	   - __traceback_info__: (<NamedImageWidget 'form.widgets.image'>, 'thumb_tag')
	  Module zope.traversing.adapters, line 47, in traverse
	   - __traceback_info__: (<NamedImageWidget 'form.widgets.image'>, 'thumb_tag', [])
	  Module plone.formwidget.namedfile.widget, line 368, in thumb_tag
	  Module plone.namedfile.scaling, line 105, in tag
	TypeError: 'str' object is not callable

	 - Expression: "view/thumb_tag"
	 - Filename:   ... .1.3-py3.8.egg/plone/formwidget/namedfile/image_input.pt
	 - Location:   (line 40: col 38)
	 - Source:     ... mg tal:replace="structure view/thumb_tag"/>
												 ^^^^^^^^^^^^^^
	 - Expression: "widget/render"
	 - Filename:   ... rm-3.2.4-py3.8.egg/plone/app/z3cform/templates/widget.pt
	 - Location:   (line 39: col 46)
	 - Source:     ... xt" tal:replace="structure widget/render"
												  ^^^^^^^^^^^^^
	 - Expression: "widget/@@ploneform-render-widget"
	 - Filename:   ... rm-3.2.4-py3.8.egg/plone/app/z3cform/templates/macros.pt
	 - Location:   (line 100: col 81)
	 - Source:     ... place="structure widget/@@ploneform-render-widget"/>
										^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	 - Expression: "context/@@ploneform-macros/titlelessform"
	 - Filename:   ... 1.3-py3.8.egg/plone/z3cform/pagetemplates/wrappedform.pt
	 - Location:   (line 1: col 22)
	 - Source:     ... e use-macro="context/@@ploneform-macros/titlelessform" />
									^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	 - Arguments:  template: <zope.browserpage.viewpagetemplatefile.ViewPageTemplateFile object at 0x7fb844a79a30>
				   options: {}
				   args: ()
				   nothing: None
				   modules: <zope.pagetemplate.engine.TraversableModuleImporter object at 0x7fb84959a8e0>
				   request: <WSGIRequest, URL=http://myserver:9996/skipshistorie/tonsberg/tbg125-nils-bull-1/tbg12519300120003-siak.png/@@edit>
				   view: <NamedImageWidget 'form.widgets.image'>
				   context: <Image at /skipshistorie/tonsberg/tbg125-nils-bull-1/tbg12519300120003-siak.png>
				   views: <zope.browserpage.viewpagetemplatefile.ViewMapper object at 0x7fb8386fec40>
				   default: <DEFAULT>
				   repeat: <Products.PageTemplates.engine.RepeatDictWrapper object at 0x7fb83871d740>
				   loop: {}
				   target_language: None
				   translate: <function BaseTemplate.render.<locals>.translate at 0x7fb838aca940>
				   attrs: {}
				   download_url: 'http://myserver:9996/skipshistorie/tonsberg/tbg125-nils-bull-1/tbg12519300120003-siak.png/edit/++widget++form.widgets.image/@@download/TBG12519300120003+Siak.png'
				   exists: True
				   action: 'nochange'
				   allow_nochange: True
				   doc_type: 'PNG image'
				   icon: 'http://myserver:9996/skipshistorie/++resource++mimetype.icons/png.png'
				   filename: 'TBG12519300120003 Siak.png'

Presumably, you are running plone.namedfile 5.xxx in line 105, it tries to get your image title and fails.

See plone.namedfile/scaling.py at 5.x · plone/plone.namedfile · GitHub

I would put a try and except there, and see what exactly is returning a string for self.context instead of your image object.

Hi. I did, but not find out why, but in the meantime:

I have downgraded the site to 5.2.9 and it works. I have not figured out why yet.

For reference: These versions does not work with Plone 5.11 or 5.12 (an error and 'Image can not be shown)

you need to patch plone app imagecropping to handle the new 'mode' scaling argument